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