profile list: react on clicking on different parts of the profile row
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
1 /*
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.profiles;
19
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.ColorDrawable;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29
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;
35 import net.ktnx.mobileledger.utils.ObservableValue;
36
37 import java.util.Collections;
38 import java.util.Observer;
39
40 import androidx.annotation.NonNull;
41 import androidx.constraintlayout.widget.ConstraintLayout;
42 import androidx.recyclerview.widget.ItemTouchHelper;
43 import androidx.recyclerview.widget.RecyclerView;
44
45 import static net.ktnx.mobileledger.utils.Logger.debug;
46
47 public class ProfilesRecyclerViewAdapter
48         extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
49     private final View.OnClickListener mOnClickListener = view -> {
50         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
51         editProfile(view, profile);
52     };
53     private ObservableValue<Boolean> editingProfiles = new ObservableValue<>(false);
54     private RecyclerView recyclerView;
55     private ItemTouchHelper rearrangeHelper;
56     public ProfilesRecyclerViewAdapter() {
57         debug("flow", "ProfilesRecyclerViewAdapter.new()");
58
59         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
60             @Override
61             public int getMovementFlags(@NonNull RecyclerView recyclerView,
62                                         @NonNull RecyclerView.ViewHolder viewHolder) {
63                 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
64             }
65             @Override
66             public boolean onMove(@NonNull RecyclerView recyclerView,
67                                   @NonNull RecyclerView.ViewHolder viewHolder,
68                                   @NonNull RecyclerView.ViewHolder target) {
69                 Data.profiles.blockNotifications();
70                 try {
71                     Collections.swap(Data.profiles, viewHolder.getAdapterPosition(), target.getAdapterPosition());
72                     MobileLedgerProfile.storeProfilesOrder();
73                     notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
74                 }
75                 finally {
76                     Data.profiles.unblockNotifications();
77                 }
78                 return true;
79             }
80             @Override
81             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
82             }
83         };
84         rearrangeHelper = new ItemTouchHelper(cb);
85     }
86     public void addEditingProfilesObserver(Observer o) {
87         editingProfiles.addObserver(o);
88     }
89     public void deleteEditingProfilesObserver(Observer o) {
90         editingProfiles.deleteObserver(o);
91     }
92     @Override
93     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
94         rearrangeHelper.attachToRecyclerView(null);
95         super.onDetachedFromRecyclerView(recyclerView);
96         this.recyclerView = null;
97     }
98     @Override
99     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
100         super.onAttachedToRecyclerView(recyclerView);
101         this.recyclerView = recyclerView;
102         if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
103     }
104     public void startEditingProfiles() {
105         if (editingProfiles.get()) return;
106         this.editingProfiles.set(true);
107         notifyDataSetChanged();
108         rearrangeHelper.attachToRecyclerView(recyclerView);
109     }
110     public void stopEditingProfiles() {
111         if (!editingProfiles.get()) return;
112         this.editingProfiles.set(false);
113         notifyDataSetChanged();
114         rearrangeHelper.attachToRecyclerView(null);
115     }
116     public void flipEditingProfiles() {
117         if (editingProfiles.get()) stopEditingProfiles();
118         else startEditingProfiles();
119     }
120     private void editProfile(View view, MobileLedgerProfile profile) {
121         int index = Data.profiles.indexOf(profile);
122         Context context = view.getContext();
123         Intent intent = new Intent(context, ProfileDetailActivity.class);
124         intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
125         if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
126
127         context.startActivity(intent);
128     }
129     private void onProfileRowClicked(View v) {
130         MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
131         if (profile == null)
132             throw new IllegalStateException("Profile row without associated profile");
133         debug("profiles", "Setting profile to " + profile.getName());
134         Data.setCurrentProfile(profile);
135     }
136     @NonNull
137     @Override
138     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
139         View view = LayoutInflater.from(parent.getContext())
140                 .inflate(R.layout.profile_list_content, parent, false);
141         ProfileListViewHolder holder = new ProfileListViewHolder(view);
142
143         holder.mRow.setOnClickListener(this::onProfileRowClicked);
144         holder.mTitle.setOnClickListener(v -> {
145             View row = (View) v.getParent();
146             onProfileRowClicked(row);
147         });
148         holder.mColorTag.setOnClickListener(v -> {
149             View row = (View) v.getParent().getParent();
150             onProfileRowClicked(row);
151         });
152         holder.mTitle.setOnLongClickListener(v -> {
153             flipEditingProfiles();
154             return true;
155         });
156         Data.profile.addObserver((o, arg) -> {
157             MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
158             final MobileLedgerProfile currentProfile = Data.profile.get();
159             final boolean sameProfile = currentProfile.equals(myProfile);
160         });
161
162         View.OnTouchListener dragStarter = (v, event) -> {
163             if (rearrangeHelper != null && editingProfiles.get()) {
164                 rearrangeHelper.startDrag(holder);
165                 return true;
166             }
167             return false;
168         };
169
170         holder.tagAndHandleLayout.setOnTouchListener(dragStarter);
171         return holder;
172     }
173     @Override
174     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
175         final MobileLedgerProfile profile = Data.profiles.get(position);
176         final MobileLedgerProfile currentProfile = Data.profile.get();
177         debug("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
178                 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
179         holder.itemView.setTag(profile);
180
181         int hue = profile.getThemeId();
182         if (hue == -1) holder.mColorTag
183                 .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
184         else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
185
186         holder.mTitle.setText(profile.getName());
187 //            holder.mSubTitle.setText(profile.getUrl());
188
189         holder.mEditButton.setOnClickListener(mOnClickListener);
190
191         final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
192         holder.itemView
193                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
194         if (editingProfiles.get()) {
195             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
196             holder.mEditButton.setVisibility(View.VISIBLE);
197         }
198         else {
199             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
200             holder.mEditButton.setVisibility(View.GONE);
201         }
202     }
203     @Override
204     public int getItemCount() {
205         return Data.profiles.size();
206     }
207     public boolean isEditingProfiles() {
208         return editingProfiles.get();
209     }
210     class ProfileListViewHolder extends RecyclerView.ViewHolder {
211         final TextView mEditButton;
212         final TextView mTitle, mColorTag;
213         final LinearLayout tagAndHandleLayout;
214         final ImageView mRearrangeHandle;
215         final ConstraintLayout mRow;
216
217         ProfileListViewHolder(View view) {
218             super(view);
219             mEditButton = view.findViewById(R.id.profile_list_edit_button);
220             mTitle = view.findViewById(R.id.title);
221             mColorTag = view.findViewById(R.id.colorTag);
222             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
223             tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
224             mRow = (ConstraintLayout) view;
225         }
226     }
227 }