]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
proile editor: fix passing of initial theme hue to the hue ring
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
1 /*
2  * Copyright © 2019 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.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.ColorDrawable;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.animation.Animation;
27 import android.view.animation.AnimationUtils;
28 import android.widget.ImageView;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.constraintlayout.widget.ConstraintLayout;
35 import androidx.lifecycle.MutableLiveData;
36 import androidx.recyclerview.widget.ItemTouchHelper;
37 import androidx.recyclerview.widget.RecyclerView;
38
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.MobileLedgerProfile;
42 import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity;
43 import net.ktnx.mobileledger.utils.Colors;
44
45 import java.lang.ref.WeakReference;
46 import java.util.ArrayList;
47 import java.util.Collections;
48 import java.util.Locale;
49
50 import static net.ktnx.mobileledger.utils.Logger.debug;
51
52 public class ProfilesRecyclerViewAdapter
53         extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
54     private static WeakReference<ProfilesRecyclerViewAdapter> instanceRef;
55     private final View.OnClickListener mOnClickListener = view -> {
56         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
57         editProfile(view, profile);
58     };
59     public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
60     private RecyclerView recyclerView;
61     private ItemTouchHelper rearrangeHelper;
62     private boolean animationsEnabled = true;
63     public ProfilesRecyclerViewAdapter() {
64         instanceRef = new WeakReference<>(this);
65         debug("flow", "ProfilesRecyclerViewAdapter.new()");
66
67         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
68             @Override
69             public int getMovementFlags(@NonNull RecyclerView recyclerView,
70                                         @NonNull RecyclerView.ViewHolder viewHolder) {
71                 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
72             }
73             @Override
74             public boolean onMove(@NonNull RecyclerView recyclerView,
75                                   @NonNull RecyclerView.ViewHolder viewHolder,
76                                   @NonNull RecyclerView.ViewHolder target) {
77                 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
78                 if (profiles == null) throw new AssertionError();
79                 Collections.swap(profiles, viewHolder.getAdapterPosition(),
80                         target.getAdapterPosition());
81                 MobileLedgerProfile.storeProfilesOrder();
82                 notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
83                 return true;
84             }
85             @Override
86             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
87             }
88         };
89         rearrangeHelper = new ItemTouchHelper(cb);
90     }
91     public static @Nullable
92     ProfilesRecyclerViewAdapter getInstance() {
93         return instanceRef.get();
94     }
95     public void setAnimationsEnabled(boolean animationsEnabled) {
96         this.animationsEnabled = animationsEnabled;
97     }
98     @Override
99     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
100         rearrangeHelper.attachToRecyclerView(null);
101         super.onDetachedFromRecyclerView(recyclerView);
102         this.recyclerView = null;
103     }
104     @Override
105     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
106         super.onAttachedToRecyclerView(recyclerView);
107         this.recyclerView = recyclerView;
108         if (editingProfiles.getValue()) rearrangeHelper.attachToRecyclerView(recyclerView);
109     }
110     public void startEditingProfiles() {
111         if (editingProfiles.getValue()) return;
112         this.editingProfiles.setValue(true);
113         rearrangeHelper.attachToRecyclerView(recyclerView);
114     }
115     public void stopEditingProfiles() {
116         if (!editingProfiles.getValue()) return;
117         this.editingProfiles.setValue(false);
118         rearrangeHelper.attachToRecyclerView(null);
119     }
120     public void flipEditingProfiles() {
121         if (editingProfiles.getValue()) stopEditingProfiles();
122         else startEditingProfiles();
123     }
124     private void editProfile(View view, MobileLedgerProfile profile) {
125         int index = Data.getProfileIndex(profile);
126         Context context = view.getContext();
127         Intent intent = new Intent(context, ProfileDetailActivity.class);
128         intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
129         if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
130
131         context.startActivity(intent);
132     }
133     private void onProfileRowClicked(View v) {
134         if (editingProfiles.getValue()) return;
135         MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
136         if (profile == null)
137             throw new IllegalStateException("Profile row without associated profile");
138         debug("profiles", "Setting profile to " + profile.getName());
139         Data.setCurrentProfile(profile);
140     }
141     @NonNull
142     @Override
143     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
144         View view = LayoutInflater.from(parent.getContext())
145                 .inflate(R.layout.profile_list_content, parent, false);
146         ProfileListViewHolder holder = new ProfileListViewHolder(view);
147
148         holder.mRow.setOnClickListener(this::onProfileRowClicked);
149         holder.mTitle.setOnClickListener(v -> {
150             View row = (View) v.getParent();
151             onProfileRowClicked(row);
152         });
153         holder.mColorTag.setOnClickListener(v -> {
154             View row = (View) v.getParent().getParent();
155             onProfileRowClicked(row);
156         });
157         holder.mTitle.setOnLongClickListener(v -> {
158             flipEditingProfiles();
159             return true;
160         });
161
162         View.OnTouchListener dragStarter = (v, event) -> {
163             if (rearrangeHelper != null && editingProfiles.getValue()) {
164                 rearrangeHelper.startDrag(holder);
165                 return true;
166             }
167             return false;
168         };
169
170         holder.tagAndHandleLayout.setOnTouchListener(dragStarter);
171         return holder;
172     }
173     @Override
174     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
175         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
176         if (profiles == null) throw new AssertionError();
177         final MobileLedgerProfile profile = profiles.get(position);
178         final MobileLedgerProfile currentProfile = Data.profile.getValue();
179         debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
180                 profile.getUuid(), (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
181         holder.itemView.setTag(profile);
182
183         int hue = profile.getThemeHue();
184         if (hue == -1) holder.mColorTag
185                 .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
186         else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
187
188         holder.mTitle.setText(profile.getName());
189 //            holder.mSubTitle.setText(profile.getUrl());
190
191         holder.mEditButton.setOnClickListener(mOnClickListener);
192
193         final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
194         holder.itemView
195                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
196         if (editingProfiles.getValue()) {
197             boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
198             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
199             holder.mEditButton.setVisibility(View.VISIBLE);
200             if (wasHidden && animationsEnabled) {
201                 Animation a = AnimationUtils
202                         .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_in);
203                 holder.mRearrangeHandle.startAnimation(a);
204                 holder.mEditButton.startAnimation(a);
205             }
206         }
207         else {
208             boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
209             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
210             holder.mEditButton.setVisibility(View.GONE);
211             if (wasShown && animationsEnabled) {
212                 Animation a = AnimationUtils
213                         .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out);
214                 holder.mRearrangeHandle.startAnimation(a);
215                 holder.mEditButton.startAnimation(a);
216             }
217         }
218     }
219     @Override
220     public int getItemCount() {
221         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
222         return profiles != null ? profiles.size() : 0;
223     }
224     class ProfileListViewHolder extends RecyclerView.ViewHolder {
225         final TextView mEditButton;
226         final TextView mTitle, mColorTag;
227         final LinearLayout tagAndHandleLayout;
228         final ImageView mRearrangeHandle;
229         final ConstraintLayout mRow;
230
231         ProfileListViewHolder(View view) {
232             super(view);
233             mEditButton = view.findViewById(R.id.profile_list_edit_button);
234             mTitle = view.findViewById(R.id.title);
235             mColorTag = view.findViewById(R.id.colorTag);
236             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
237             tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
238             mRow = (ConstraintLayout) view;
239         }
240     }
241 }