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