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.
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 MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.profiles;
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;
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;
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;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
48 import static net.ktnx.mobileledger.utils.Logger.debug;
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;
58 public ProfilesRecyclerViewAdapter() {
59 debug("flow", "ProfilesRecyclerViewAdapter.new()");
61 setHasStableIds(true);
62 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<Profile>() {
64 public boolean areItemsTheSame(@NonNull Profile oldItem, @NonNull Profile newItem) {
65 return oldItem.getId() == newItem.getId();
68 public boolean areContentsTheSame(@NonNull Profile oldItem, @NonNull Profile newItem) {
69 return oldItem.equals(newItem);
73 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
75 public int getMovementFlags(@NonNull RecyclerView recyclerView,
76 @NonNull RecyclerView.ViewHolder viewHolder) {
77 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
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());
88 .updateOrder(profiles, null);
89 // notifyItemMoved(viewHolder.getBindingAdapterPosition(), target
90 // .getBindingAdapterPosition());
94 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
97 rearrangeHelper = new ItemTouchHelper(cb);
100 public long getItemId(int position) {
101 return listDiffer.getCurrentList()
105 public void setProfileList(List<Profile> list) {
106 listDiffer.submitList(list);
108 public void setAnimationsEnabled(boolean animationsEnabled) {
109 this.animationsEnabled = animationsEnabled;
112 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
113 rearrangeHelper.attachToRecyclerView(null);
114 super.onDetachedFromRecyclerView(recyclerView);
115 this.recyclerView = null;
118 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
119 super.onAttachedToRecyclerView(recyclerView);
120 this.recyclerView = recyclerView;
121 if (editingProfiles())
122 rearrangeHelper.attachToRecyclerView(recyclerView);
124 public boolean editingProfiles() {
125 final Boolean b = editingProfiles.getValue();
130 public void startEditingProfiles() {
131 if (editingProfiles())
133 this.editingProfiles.setValue(true);
134 rearrangeHelper.attachToRecyclerView(recyclerView);
136 public void stopEditingProfiles() {
137 if (!editingProfiles())
139 this.editingProfiles.setValue(false);
140 rearrangeHelper.attachToRecyclerView(null);
142 public void flipEditingProfiles() {
143 if (editingProfiles())
144 stopEditingProfiles();
146 startEditingProfiles();
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);
156 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
157 final Profile profile = listDiffer.getCurrentList()
159 final Profile currentProfile = Data.getProfile();
160 // debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
161 // profile.getUuid(), currentProfile.getUuid()));
163 int hue = profile.getTheme();
165 holder.mColorTag.setBackgroundColor(
166 Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
168 holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
170 holder.mTitle.setText(profile.getName());
171 // holder.mSubTitle.setText(profile.getUrl());
173 holder.mEditButton.setOnClickListener(view -> {
174 Profile p = listDiffer.getCurrentList()
175 .get(holder.getBindingAdapterPosition());
176 ProfileDetailActivity.start(view.getContext(), p);
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(),
190 holder.mRearrangeHandle.startAnimation(a);
191 holder.mEditButton.startAnimation(a);
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(),
201 holder.mRearrangeHandle.startAnimation(a);
202 holder.mEditButton.startAnimation(a);
207 public int getItemCount() {
208 return listDiffer.getCurrentList()
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;
218 ProfileListViewHolder(View 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;
228 mRow.setOnClickListener(this::onProfileRowClicked);
229 mTitle.setOnClickListener(v -> {
230 View row = (View) v.getParent();
231 onProfileRowClicked(row);
233 mColorTag.setOnClickListener(v -> {
234 View row = (View) v.getParent()
236 onProfileRowClicked(row);
238 mTitle.setOnLongClickListener(v -> {
239 flipEditingProfiles();
243 View.OnTouchListener dragStarter = (v, event) -> {
244 if (rearrangeHelper != null && editingProfiles()) {
245 rearrangeHelper.startDrag(this);
251 tagAndHandleLayout.setOnTouchListener(dragStarter);
253 private void onProfileRowClicked(View v) {
254 if (editingProfiles())
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);
265 "Not setting profile to the current profile " + profile.getName());