]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
profile list: also show the default color tag
[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 Mobile-Ledger. 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.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
36 import java.util.Collections;
37
38 import androidx.annotation.NonNull;
39 import androidx.recyclerview.widget.ItemTouchHelper;
40 import androidx.recyclerview.widget.RecyclerView;
41
42 public class ProfilesRecyclerViewAdapter
43         extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
44     private final View.OnClickListener mOnClickListener = view -> {
45         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
46         editProfile(view, profile);
47     };
48     private boolean editingProfiles = false;
49     private RecyclerView recyclerView;
50     private ItemTouchHelper rearrangeHelper;
51     public ProfilesRecyclerViewAdapter() {
52         Data.profiles.addObserver((o, arg) -> {
53             Log.d("profiles", "profile list changed");
54             if (arg == null) notifyDataSetChanged();
55             else notifyItemChanged((int) arg);
56         });
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                 Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
69                         target.getAdapterPosition());
70                 MobileLedgerProfile.storeProfilesOrder();
71                 notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
72                 return true;
73             }
74             @Override
75             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
76             }
77         };
78         rearrangeHelper = new ItemTouchHelper(cb);
79     }
80     @Override
81     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
82         rearrangeHelper.attachToRecyclerView(null);
83         super.onDetachedFromRecyclerView(recyclerView);
84         this.recyclerView = null;
85     }
86     @Override
87     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
88         super.onAttachedToRecyclerView(recyclerView);
89         this.recyclerView = recyclerView;
90         if (editingProfiles) rearrangeHelper.attachToRecyclerView(recyclerView);
91     }
92     public boolean editingProfiles() {
93         return this.editingProfiles;
94     }
95     public void startEditingProfiles() {
96         if (editingProfiles) return;
97         this.editingProfiles = true;
98         notifyDataSetChanged();
99         rearrangeHelper.attachToRecyclerView(recyclerView);
100     }
101     public void stopEditingProfiles() {
102         if (!editingProfiles) return;
103         this.editingProfiles = false;
104         notifyDataSetChanged();
105         rearrangeHelper.attachToRecyclerView(null);
106     }
107     public void flipEditingProfiles() {
108         if (editingProfiles) stopEditingProfiles();
109         else startEditingProfiles();
110     }
111     private void editProfile(View view, MobileLedgerProfile profile) {
112         int index = Data.profiles.indexOf(profile);
113         Context context = view.getContext();
114         Intent intent = new Intent(context, ProfileDetailActivity.class);
115         intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
116         if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
117
118         context.startActivity(intent);
119     }
120     @NonNull
121     @Override
122     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
123         View view = LayoutInflater.from(parent.getContext())
124                 .inflate(R.layout.profile_list_content, parent, false);
125         ProfileListViewHolder holder = new ProfileListViewHolder(view);
126
127         holder.mTitle.setOnClickListener(v -> {
128             View row = (View) v.getParent();
129             MobileLedgerProfile profile = (MobileLedgerProfile) row.getTag();
130             if (profile == null)
131                 throw new IllegalStateException("Profile row without associated profile");
132             Log.d("profiles", "Setting profile to " + profile.getName());
133             Data.setCurrentProfile(profile);
134         });
135         holder.mTitle.setOnLongClickListener(v -> {
136             flipEditingProfiles();
137             return true;
138         });
139         Data.profile.addObserver((o, arg) -> {
140             MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
141             final MobileLedgerProfile currentProfile = Data.profile.get();
142             final boolean sameProfile = currentProfile.equals(myProfile);
143             view.setAlpha(sameProfile ? 1 : 0.5f);
144         });
145         return holder;
146     }
147     @Override
148     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
149         final MobileLedgerProfile profile = Data.profiles.get(position);
150         final MobileLedgerProfile currentProfile = Data.profile.get();
151         Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
152                 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
153         holder.itemView.setTag(profile);
154
155         int hue = profile.getThemeId();
156         if (hue == -1) holder.mColorTag
157                 .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
158         else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
159
160         holder.mTitle.setText(profile.getName());
161 //            holder.mSubTitle.setText(profile.getUrl());
162
163         holder.mEditButton.setOnClickListener(mOnClickListener);
164
165         final boolean sameProfile = currentProfile.equals(profile);
166         holder.itemView.setAlpha(sameProfile ? 1 : 0.5f);
167         holder.itemView
168                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
169         if (editingProfiles) {
170             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
171             holder.mEditButton.setVisibility(View.VISIBLE);
172         }
173         else {
174             holder.mRearrangeHandle.setVisibility(View.GONE);
175             holder.mEditButton.setVisibility(View.GONE);
176         }
177     }
178     @Override
179     public int getItemCount() {
180         return Data.profiles.size();
181     }
182     class ProfileListViewHolder extends RecyclerView.ViewHolder {
183         final TextView mEditButton;
184         final TextView mTitle, mColorTag;
185         final ImageView mRearrangeHandle;
186
187         ProfileListViewHolder(View view) {
188             super(view);
189             mEditButton = view.findViewById(R.id.profile_list_edit_button);
190             mTitle = view.findViewById(R.id.title);
191             mColorTag = view.findViewById(R.id.colorTag);
192             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
193         }
194     }
195 }