]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
688cd6d42f69deee4ab5d518d73b5445e0d284e6
[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.getAdapterPosition(),
85                         target.getAdapterPosition());
86                 DB.get()
87                   .getProfileDAO()
88                   .updateOrder(profiles, null);
89 //                notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
90                 return true;
91             }
92             @Override
93             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
94             }
95         };
96         rearrangeHelper = new ItemTouchHelper(cb);
97     }
98     @Override
99     public long getItemId(int position) {
100         return listDiffer.getCurrentList()
101                          .get(position)
102                          .getId();
103     }
104     public void setProfileList(List<Profile> list) {
105         listDiffer.submitList(list);
106     }
107     public void setAnimationsEnabled(boolean animationsEnabled) {
108         this.animationsEnabled = animationsEnabled;
109     }
110     @Override
111     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
112         rearrangeHelper.attachToRecyclerView(null);
113         super.onDetachedFromRecyclerView(recyclerView);
114         this.recyclerView = null;
115     }
116     @Override
117     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
118         super.onAttachedToRecyclerView(recyclerView);
119         this.recyclerView = recyclerView;
120         if (editingProfiles())
121             rearrangeHelper.attachToRecyclerView(recyclerView);
122     }
123     public boolean editingProfiles() {
124         final Boolean b = editingProfiles.getValue();
125         if (b == null)
126             return false;
127         return b;
128     }
129     public void startEditingProfiles() {
130         if (editingProfiles())
131             return;
132         this.editingProfiles.setValue(true);
133         rearrangeHelper.attachToRecyclerView(recyclerView);
134     }
135     public void stopEditingProfiles() {
136         if (!editingProfiles())
137             return;
138         this.editingProfiles.setValue(false);
139         rearrangeHelper.attachToRecyclerView(null);
140     }
141     public void flipEditingProfiles() {
142         if (editingProfiles())
143             stopEditingProfiles();
144         else
145             startEditingProfiles();
146     }
147     @NonNull
148     @Override
149     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
150         View view = LayoutInflater.from(parent.getContext())
151                                   .inflate(R.layout.profile_list_content, parent, false);
152         return new ProfileListViewHolder(view);
153     }
154     @Override
155     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
156         final Profile profile = listDiffer.getCurrentList()
157                                           .get(position);
158         final Profile currentProfile = Data.getProfile();
159 //        debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
160 //                profile.getUuid(), currentProfile.getUuid()));
161
162         int hue = profile.getTheme();
163         if (hue == -1)
164             holder.mColorTag.setBackgroundColor(
165                     Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
166         else
167             holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
168
169         holder.mTitle.setText(profile.getName());
170 //            holder.mSubTitle.setText(profile.getUrl());
171
172         holder.mEditButton.setOnClickListener(view -> {
173             Profile p = listDiffer.getCurrentList()
174                                   .get(holder.getAdapterPosition());
175             ProfileDetailActivity.start(view.getContext(), p);
176         });
177
178         final boolean sameProfile = currentProfile.getId() == profile.getId();
179         holder.itemView.setBackground(
180                 sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
181         if (editingProfiles()) {
182             boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
183             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
184             holder.mEditButton.setVisibility(View.VISIBLE);
185             if (wasHidden && animationsEnabled) {
186                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
187                         R.anim.fade_in);
188                 holder.mRearrangeHandle.startAnimation(a);
189                 holder.mEditButton.startAnimation(a);
190             }
191         }
192         else {
193             boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
194             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
195             holder.mEditButton.setVisibility(View.GONE);
196             if (wasShown && animationsEnabled) {
197                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
198                         R.anim.fade_out);
199                 holder.mRearrangeHandle.startAnimation(a);
200                 holder.mEditButton.startAnimation(a);
201             }
202         }
203     }
204     @Override
205     public int getItemCount() {
206         return listDiffer.getCurrentList()
207                          .size();
208     }
209     class ProfileListViewHolder extends RecyclerView.ViewHolder {
210         final TextView mEditButton;
211         final TextView mTitle, mColorTag;
212         final LinearLayout tagAndHandleLayout;
213         final ImageView mRearrangeHandle;
214         final ConstraintLayout mRow;
215
216         ProfileListViewHolder(View view) {
217             super(view);
218             mEditButton = view.findViewById(R.id.profile_list_edit_button);
219             mTitle = view.findViewById(R.id.title);
220             mColorTag = view.findViewById(R.id.colorTag);
221             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
222             tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
223             mRow = (ConstraintLayout) view;
224
225
226             mRow.setOnClickListener(this::onProfileRowClicked);
227             mTitle.setOnClickListener(v -> {
228                 View row = (View) v.getParent();
229                 onProfileRowClicked(row);
230             });
231             mColorTag.setOnClickListener(v -> {
232                 View row = (View) v.getParent()
233                                    .getParent();
234                 onProfileRowClicked(row);
235             });
236             mTitle.setOnLongClickListener(v -> {
237                 flipEditingProfiles();
238                 return true;
239             });
240
241             View.OnTouchListener dragStarter = (v, event) -> {
242                 if (rearrangeHelper != null && editingProfiles()) {
243                     rearrangeHelper.startDrag(this);
244                     return true;
245                 }
246                 return false;
247             };
248
249             tagAndHandleLayout.setOnTouchListener(dragStarter);
250         }
251         private void onProfileRowClicked(View v) {
252             if (editingProfiles())
253                 return;
254             Profile profile = listDiffer.getCurrentList()
255                                         .get(getAdapterPosition());
256             if (Data.getProfile() != profile) {
257                 debug("profiles", "Setting profile to " + profile.getName());
258                 Data.drawerOpen.setValue(false);
259                 Data.setCurrentProfile(profile);
260             }
261             else
262                 debug("profiles",
263                         "Not setting profile to the current profile " + profile.getName());
264         }
265
266     }
267 }