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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.profiles;
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;
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;
38 import java.util.Collections;
39 import java.util.Observer;
41 import androidx.annotation.NonNull;
42 import androidx.recyclerview.widget.ItemTouchHelper;
43 import androidx.recyclerview.widget.RecyclerView;
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);
51 private ObservableValue<Boolean> editingProfiles = new ObservableValue<>(false);
52 public void addEditingProfilesObserver(Observer o) {
53 editingProfiles.addObserver(o);
55 public void deleteEditingProfilesObserver(Observer o) {
56 editingProfiles.deleteObserver(o);
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);
67 ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
69 public int getMovementFlags(@NonNull RecyclerView recyclerView,
70 @NonNull RecyclerView.ViewHolder viewHolder) {
71 return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0);
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());
84 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
87 rearrangeHelper = new ItemTouchHelper(cb);
90 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
91 rearrangeHelper.attachToRecyclerView(null);
92 super.onDetachedFromRecyclerView(recyclerView);
93 this.recyclerView = null;
96 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
97 super.onAttachedToRecyclerView(recyclerView);
98 this.recyclerView = recyclerView;
99 if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
101 public void startEditingProfiles() {
102 if (editingProfiles.get()) return;
103 this.editingProfiles.set(true);
104 notifyDataSetChanged();
105 rearrangeHelper.attachToRecyclerView(recyclerView);
107 public void stopEditingProfiles() {
108 if (!editingProfiles.get()) return;
109 this.editingProfiles.set(false);
110 notifyDataSetChanged();
111 rearrangeHelper.attachToRecyclerView(null);
113 public void flipEditingProfiles() {
114 if (editingProfiles.get()) stopEditingProfiles();
115 else startEditingProfiles();
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);
124 context.startActivity(intent);
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);
133 holder.mTitle.setOnClickListener(v -> {
134 View row = (View) v.getParent();
135 MobileLedgerProfile profile = (MobileLedgerProfile) row.getTag();
137 throw new IllegalStateException("Profile row without associated profile");
138 Log.d("profiles", "Setting profile to " + profile.getName());
139 Data.setCurrentProfile(profile);
141 holder.mTitle.setOnLongClickListener(v -> {
142 flipEditingProfiles();
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);
151 View.OnTouchListener dragStarter = (v, event) -> {
152 if (rearrangeHelper != null && editingProfiles.get()) {
153 rearrangeHelper.startDrag(holder);
159 holder.tagAndHandleLayout.setOnTouchListener(dragStarter);
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);
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));
175 holder.mTitle.setText(profile.getName());
176 // holder.mSubTitle.setText(profile.getUrl());
178 holder.mEditButton.setOnClickListener(mOnClickListener);
180 final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
182 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
183 if (editingProfiles.get()) {
184 holder.mRearrangeHandle.setVisibility(View.VISIBLE);
185 holder.mEditButton.setVisibility(View.VISIBLE);
188 holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
189 holder.mEditButton.setVisibility(View.GONE);
193 public int getItemCount() {
194 return Data.profiles.size();
196 public boolean isEditingProfiles() {
197 return editingProfiles.get();
199 class ProfileListViewHolder extends RecyclerView.ViewHolder {
200 final TextView mEditButton;
201 final TextView mTitle, mColorTag;
202 final LinearLayout tagAndHandleLayout;
203 final ImageView mRearrangeHandle;
205 ProfileListViewHolder(View 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);