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