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.getAdapterPosition(),
85 target.getAdapterPosition());
88 .updateOrder(profiles, null);
89 // notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
93 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
96 rearrangeHelper = new ItemTouchHelper(cb);
99 public long getItemId(int position) {
100 return listDiffer.getCurrentList()
104 public void setProfileList(List<Profile> list) {
105 listDiffer.submitList(list);
107 public void setAnimationsEnabled(boolean animationsEnabled) {
108 this.animationsEnabled = animationsEnabled;
111 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
112 rearrangeHelper.attachToRecyclerView(null);
113 super.onDetachedFromRecyclerView(recyclerView);
114 this.recyclerView = null;
117 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
118 super.onAttachedToRecyclerView(recyclerView);
119 this.recyclerView = recyclerView;
120 if (editingProfiles())
121 rearrangeHelper.attachToRecyclerView(recyclerView);
123 public boolean editingProfiles() {
124 final Boolean b = editingProfiles.getValue();
129 public void startEditingProfiles() {
130 if (editingProfiles())
132 this.editingProfiles.setValue(true);
133 rearrangeHelper.attachToRecyclerView(recyclerView);
135 public void stopEditingProfiles() {
136 if (!editingProfiles())
138 this.editingProfiles.setValue(false);
139 rearrangeHelper.attachToRecyclerView(null);
141 public void flipEditingProfiles() {
142 if (editingProfiles())
143 stopEditingProfiles();
145 startEditingProfiles();
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);
155 public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
156 final Profile profile = listDiffer.getCurrentList()
158 final Profile currentProfile = Data.getProfile();
159 // debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
160 // profile.getUuid(), currentProfile.getUuid()));
162 int hue = profile.getTheme();
164 holder.mColorTag.setBackgroundColor(
165 Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
167 holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
169 holder.mTitle.setText(profile.getName());
170 // holder.mSubTitle.setText(profile.getUrl());
172 holder.mEditButton.setOnClickListener(view -> {
173 Profile p = listDiffer.getCurrentList()
174 .get(holder.getAdapterPosition());
175 ProfileDetailActivity.start(view.getContext(), p);
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(),
189 holder.mRearrangeHandle.startAnimation(a);
190 holder.mEditButton.startAnimation(a);
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(),
200 holder.mRearrangeHandle.startAnimation(a);
201 holder.mEditButton.startAnimation(a);
206 public int getItemCount() {
207 return listDiffer.getCurrentList()
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;
217 ProfileListViewHolder(View 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;
227 mRow.setOnClickListener(this::onProfileRowClicked);
228 mTitle.setOnClickListener(v -> {
229 View row = (View) v.getParent();
230 onProfileRowClicked(row);
232 mColorTag.setOnClickListener(v -> {
233 View row = (View) v.getParent()
235 onProfileRowClicked(row);
237 mTitle.setOnLongClickListener(v -> {
238 flipEditingProfiles();
242 View.OnTouchListener dragStarter = (v, event) -> {
243 if (rearrangeHelper != null && editingProfiles()) {
244 rearrangeHelper.startDrag(this);
250 tagAndHandleLayout.setOnTouchListener(dragStarter);
252 private void onProfileRowClicked(View v) {
253 if (editingProfiles())
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);
264 "Not setting profile to the current profile " + profile.getName());