]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
1 /*
2  * Copyright © 2021 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.graphics.drawable.ColorDrawable;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.view.animation.Animation;
25 import android.view.animation.AnimationUtils;
26 import android.widget.ImageView;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29
30 import androidx.annotation.NonNull;
31 import androidx.constraintlayout.widget.ConstraintLayout;
32 import androidx.lifecycle.MutableLiveData;
33 import androidx.recyclerview.widget.AsyncListDiffer;
34 import androidx.recyclerview.widget.DiffUtil;
35 import androidx.recyclerview.widget.ItemTouchHelper;
36 import androidx.recyclerview.widget.RecyclerView;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.db.DB;
40 import net.ktnx.mobileledger.db.Profile;
41 import net.ktnx.mobileledger.model.Data;
42 import net.ktnx.mobileledger.utils.Colors;
43
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47
48 import static net.ktnx.mobileledger.utils.Logger.debug;
49
50 public class ProfilesRecyclerViewAdapter
51         extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
52     public final MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
53     private final ItemTouchHelper rearrangeHelper;
54     private final AsyncListDiffer<Profile> listDiffer;
55     private RecyclerView recyclerView;
56     private boolean animationsEnabled = true;
57
58     public ProfilesRecyclerViewAdapter() {
59         debug("flow", "ProfilesRecyclerViewAdapter.new()");
60
61         setHasStableIds(true);
62         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<Profile>() {
63             @Override
64             public boolean areItemsTheSame(@NonNull Profile oldItem, @NonNull Profile newItem) {
65                 return oldItem.getId() == newItem.getId();
66             }
67             @Override
68             public boolean areContentsTheSame(@NonNull Profile oldItem, @NonNull Profile newItem) {
69                 return oldItem.equals(newItem);
70             }
71         });
72
73         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
74             @Override
75             public int getMovementFlags(@NonNull RecyclerView recyclerView,
76                                         @NonNull RecyclerView.ViewHolder viewHolder) {
77                 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
78             }
79             @Override
80             public boolean onMove(@NonNull RecyclerView recyclerView,
81                                   @NonNull RecyclerView.ViewHolder viewHolder,
82                                   @NonNull RecyclerView.ViewHolder target) {
83                 final List<Profile> profiles = new ArrayList<>(listDiffer.getCurrentList());
84                 Collections.swap(profiles, viewHolder.getBindingAdapterPosition(),
85                         target.getBindingAdapterPosition());
86                 DB.get()
87                   .getProfileDAO()
88                   .updateOrder(profiles, null);
89 //                notifyItemMoved(viewHolder.getBindingAdapterPosition(), target
90 //                .getBindingAdapterPosition());
91                 return true;
92             }
93             @Override
94             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
95             }
96         };
97         rearrangeHelper = new ItemTouchHelper(cb);
98     }
99     @Override
100     public long getItemId(int position) {
101         return listDiffer.getCurrentList()
102                          .get(position)
103                          .getId();
104     }
105     public void setProfileList(List<Profile> list) {
106         listDiffer.submitList(list);
107     }
108     public void setAnimationsEnabled(boolean animationsEnabled) {
109         this.animationsEnabled = animationsEnabled;
110     }
111     @Override
112     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
113         rearrangeHelper.attachToRecyclerView(null);
114         super.onDetachedFromRecyclerView(recyclerView);
115         this.recyclerView = null;
116     }
117     @Override
118     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
119         super.onAttachedToRecyclerView(recyclerView);
120         this.recyclerView = recyclerView;
121         if (editingProfiles())
122             rearrangeHelper.attachToRecyclerView(recyclerView);
123     }
124     public boolean editingProfiles() {
125         final Boolean b = editingProfiles.getValue();
126         if (b == null)
127             return false;
128         return b;
129     }
130     public void startEditingProfiles() {
131         if (editingProfiles())
132             return;
133         this.editingProfiles.setValue(true);
134         rearrangeHelper.attachToRecyclerView(recyclerView);
135     }
136     public void stopEditingProfiles() {
137         if (!editingProfiles())
138             return;
139         this.editingProfiles.setValue(false);
140         rearrangeHelper.attachToRecyclerView(null);
141     }
142     public void flipEditingProfiles() {
143         if (editingProfiles())
144             stopEditingProfiles();
145         else
146             startEditingProfiles();
147     }
148     @NonNull
149     @Override
150     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
151         View view = LayoutInflater.from(parent.getContext())
152                                   .inflate(R.layout.profile_list_content, parent, false);
153         return new ProfileListViewHolder(view);
154     }
155     @Override
156     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
157         final Profile profile = listDiffer.getCurrentList()
158                                           .get(position);
159         final Profile currentProfile = Data.getProfile();
160 //        debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
161 //                profile.getUuid(), currentProfile.getUuid()));
162
163         int hue = profile.getTheme();
164         if (hue == -1)
165             holder.mColorTag.setBackgroundColor(
166                     Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
167         else
168             holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
169
170         holder.mTitle.setText(profile.getName());
171 //            holder.mSubTitle.setText(profile.getUrl());
172
173         holder.mEditButton.setOnClickListener(view -> {
174             Profile p = listDiffer.getCurrentList()
175                                   .get(holder.getBindingAdapterPosition());
176             ProfileDetailActivity.start(view.getContext(), p);
177         });
178
179         final boolean sameProfile =
180                 currentProfile != null && currentProfile.getId() == profile.getId();
181         holder.itemView.setBackground(
182                 sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
183         if (editingProfiles()) {
184             boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
185             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
186             holder.mEditButton.setVisibility(View.VISIBLE);
187             if (wasHidden && animationsEnabled) {
188                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
189                         R.anim.fade_in);
190                 holder.mRearrangeHandle.startAnimation(a);
191                 holder.mEditButton.startAnimation(a);
192             }
193         }
194         else {
195             boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
196             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
197             holder.mEditButton.setVisibility(View.GONE);
198             if (wasShown && animationsEnabled) {
199                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
200                         R.anim.fade_out);
201                 holder.mRearrangeHandle.startAnimation(a);
202                 holder.mEditButton.startAnimation(a);
203             }
204         }
205     }
206     @Override
207     public int getItemCount() {
208         return listDiffer.getCurrentList()
209                          .size();
210     }
211     class ProfileListViewHolder extends RecyclerView.ViewHolder {
212         final TextView mEditButton;
213         final TextView mTitle, mColorTag;
214         final LinearLayout tagAndHandleLayout;
215         final ImageView mRearrangeHandle;
216         final ConstraintLayout mRow;
217
218         ProfileListViewHolder(View view) {
219             super(view);
220             mEditButton = view.findViewById(R.id.profile_list_edit_button);
221             mTitle = view.findViewById(R.id.title);
222             mColorTag = view.findViewById(R.id.colorTag);
223             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
224             tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
225             mRow = (ConstraintLayout) view;
226
227
228             mRow.setOnClickListener(this::onProfileRowClicked);
229             mTitle.setOnClickListener(v -> {
230                 View row = (View) v.getParent();
231                 onProfileRowClicked(row);
232             });
233             mColorTag.setOnClickListener(v -> {
234                 View row = (View) v.getParent()
235                                    .getParent();
236                 onProfileRowClicked(row);
237             });
238             mTitle.setOnLongClickListener(v -> {
239                 flipEditingProfiles();
240                 return true;
241             });
242
243             View.OnTouchListener dragStarter = (v, event) -> {
244                 if (rearrangeHelper != null && editingProfiles()) {
245                     rearrangeHelper.startDrag(this);
246                     return true;
247                 }
248                 return false;
249             };
250
251             tagAndHandleLayout.setOnTouchListener(dragStarter);
252         }
253         private void onProfileRowClicked(View v) {
254             if (editingProfiles())
255                 return;
256             Profile profile = listDiffer.getCurrentList()
257                                         .get(getBindingAdapterPosition());
258             if (Data.getProfile() != profile) {
259                 debug("profiles", "Setting profile to " + profile.getName());
260                 Data.drawerOpen.setValue(false);
261                 Data.setCurrentProfile(profile);
262             }
263             else
264                 debug("profiles",
265                         "Not setting profile to the current profile " + profile.getName());
266         }
267
268     }
269 }