2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.ColorDrawable;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.TextView;
30 import net.ktnx.mobileledger.R;
31 import net.ktnx.mobileledger.model.Data;
32 import net.ktnx.mobileledger.model.MobileLedgerProfile;
33 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
34 import net.ktnx.mobileledger.utils.Colors;
36 import java.util.Collections;
38 import androidx.annotation.NonNull;
39 import androidx.recyclerview.widget.ItemTouchHelper;
40 import androidx.recyclerview.widget.RecyclerView;
42 public class ProfilesRecyclerViewAdapter
43 extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
44 private final View.OnClickListener mOnClickListener = view -> {
45 MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
46 editProfile(view, profile);
48 private boolean editingProfiles = false;
49 private RecyclerView recyclerView;
50 private ItemTouchHelper rearrangeHelper;
51 public ProfilesRecyclerViewAdapter() {
52 Data.profiles.addObserver((o, arg) -> {
53 Log.d("profiles", "profile list changed");
54 if (arg == null) notifyDataSetChanged();
55 else notifyItemChanged((int) arg);
58 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
60 public int getMovementFlags(@NonNull RecyclerView recyclerView,
61 @NonNull RecyclerView.ViewHolder viewHolder) {
62 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
65 public boolean onMove(@NonNull RecyclerView recyclerView,
66 @NonNull RecyclerView.ViewHolder viewHolder,
67 @NonNull RecyclerView.ViewHolder target) {
68 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
69 target.getAdapterPosition());
70 MobileLedgerProfile.storeProfilesOrder();
71 notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
75 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
78 rearrangeHelper = new ItemTouchHelper(cb);
81 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
82 rearrangeHelper.attachToRecyclerView(null);
83 super.onDetachedFromRecyclerView(recyclerView);
84 this.recyclerView = null;
87 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
88 super.onAttachedToRecyclerView(recyclerView);
89 this.recyclerView = recyclerView;
90 if (editingProfiles) rearrangeHelper.attachToRecyclerView(recyclerView);
92 public boolean editingProfiles() {
93 return this.editingProfiles;
95 public void startEditingProfiles() {
96 if (editingProfiles) return;
97 this.editingProfiles = true;
98 notifyDataSetChanged();
99 rearrangeHelper.attachToRecyclerView(recyclerView);
101 public void stopEditingProfiles() {
102 if (!editingProfiles) return;
103 this.editingProfiles = false;
104 notifyDataSetChanged();
105 rearrangeHelper.attachToRecyclerView(null);
107 public void flipEditingProfiles() {
108 if (editingProfiles) stopEditingProfiles();
109 else startEditingProfiles();
111 private void editProfile(View view, MobileLedgerProfile profile) {
112 int index = Data.profiles.indexOf(profile);
113 Context context = view.getContext();
114 Intent intent = new Intent(context, ProfileDetailActivity.class);
115 intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
116 if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
118 context.startActivity(intent);
122 public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
123 View view = LayoutInflater.from(parent.getContext())
124 .inflate(R.layout.profile_list_content, parent, false);
125 ProfileListViewHolder holder = new ProfileListViewHolder(view);
127 holder.mTitle.setOnClickListener(v -> {
128 View row = (View) v.getParent();
129 MobileLedgerProfile profile = (MobileLedgerProfile) row.getTag();
131 throw new IllegalStateException("Profile row without associated profile");
132 Log.d("profiles", "Setting profile to " + profile.getName());
133 Data.setCurrentProfile(profile);
135 holder.mTitle.setOnLongClickListener(v -> {
136 flipEditingProfiles();
139 Data.profile.addObserver((o, arg) -> {
140 MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
141 final MobileLedgerProfile currentProfile = Data.profile.get();
142 final boolean sameProfile = currentProfile.equals(myProfile);
143 view.setAlpha(sameProfile ? 1 : 0.5f);
148 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
149 final MobileLedgerProfile profile = Data.profiles.get(position);
150 final MobileLedgerProfile currentProfile = Data.profile.get();
151 Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
152 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
153 holder.itemView.setTag(profile);
155 int hue = profile.getThemeId();
156 if (hue == -1) holder.mColorTag
157 .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
158 else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
160 holder.mTitle.setText(profile.getName());
161 // holder.mSubTitle.setText(profile.getUrl());
163 holder.mEditButton.setOnClickListener(mOnClickListener);
165 final boolean sameProfile = currentProfile.equals(profile);
166 holder.itemView.setAlpha(sameProfile ? 1 : 0.5f);
168 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
169 if (editingProfiles) {
170 holder.mRearrangeHandle.setVisibility(View.VISIBLE);
171 holder.mEditButton.setVisibility(View.VISIBLE);
174 holder.mRearrangeHandle.setVisibility(View.GONE);
175 holder.mEditButton.setVisibility(View.GONE);
179 public int getItemCount() {
180 return Data.profiles.size();
182 class ProfileListViewHolder extends RecyclerView.ViewHolder {
183 final TextView mEditButton;
184 final TextView mTitle, mColorTag;
185 final ImageView mRearrangeHandle;
187 ProfileListViewHolder(View view) {
189 mEditButton = view.findViewById(R.id.profile_list_edit_button);
190 mTitle = view.findViewById(R.id.title);
191 mColorTag = view.findViewById(R.id.colorTag);
192 mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);