]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
fix creation of a new profile after Room-y of Profile adoption
[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 =
179                 currentProfile != null && currentProfile.getId() == profile.getId();
180         holder.itemView.setBackground(
181                 sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
182         if (editingProfiles()) {
183             boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
184             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
185             holder.mEditButton.setVisibility(View.VISIBLE);
186             if (wasHidden && animationsEnabled) {
187                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
188                         R.anim.fade_in);
189                 holder.mRearrangeHandle.startAnimation(a);
190                 holder.mEditButton.startAnimation(a);
191             }
192         }
193         else {
194             boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
195             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
196             holder.mEditButton.setVisibility(View.GONE);
197             if (wasShown && animationsEnabled) {
198                 Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
199                         R.anim.fade_out);
200                 holder.mRearrangeHandle.startAnimation(a);
201                 holder.mEditButton.startAnimation(a);
202             }
203         }
204     }
205     @Override
206     public int getItemCount() {
207         return listDiffer.getCurrentList()
208                          .size();
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             mRow.setOnClickListener(this::onProfileRowClicked);
228             mTitle.setOnClickListener(v -> {
229                 View row = (View) v.getParent();
230                 onProfileRowClicked(row);
231             });
232             mColorTag.setOnClickListener(v -> {
233                 View row = (View) v.getParent()
234                                    .getParent();
235                 onProfileRowClicked(row);
236             });
237             mTitle.setOnLongClickListener(v -> {
238                 flipEditingProfiles();
239                 return true;
240             });
241
242             View.OnTouchListener dragStarter = (v, event) -> {
243                 if (rearrangeHelper != null && editingProfiles()) {
244                     rearrangeHelper.startDrag(this);
245                     return true;
246                 }
247                 return false;
248             };
249
250             tagAndHandleLayout.setOnTouchListener(dragStarter);
251         }
252         private void onProfileRowClicked(View v) {
253             if (editingProfiles())
254                 return;
255             Profile profile = listDiffer.getCurrentList()
256                                         .get(getAdapterPosition());
257             if (Data.getProfile() != profile) {
258                 debug("profiles", "Setting profile to " + profile.getName());
259                 Data.drawerOpen.setValue(false);
260                 Data.setCurrentProfile(profile);
261             }
262             else
263                 debug("profiles",
264                         "Not setting profile to the current profile " + profile.getName());
265         }
266
267     }
268 }