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