X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fprofiles%2FProfilesRecyclerViewAdapter.java;h=dfffc8f233743553a9d3689d4911d82673f90f64;hp=54386d9225279b4f7a09adca8dbcacc177f68acc;hb=83cac114e375728080194fb09758b49c50a8119b;hpb=59e1c221c18f8dfaf6394cdf0a29e4ba2319151d diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java index 54386d92..dfffc8f2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java @@ -1,31 +1,31 @@ /* * Copyright © 2019 Damyan Ivanov. - * This file is part of MoLe. - * MoLe is free software: you can distribute it and/or modify it - * under the term of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your opinion), any later version. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. * - * MoLe is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License terms for details. + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. * - * You should have received a copy of the GNU General Public License - * along with Mobile-Ledger. If not, see . + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . */ package net.ktnx.mobileledger.ui.profiles; import android.content.Context; import android.content.Intent; -import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import net.ktnx.mobileledger.R; @@ -33,8 +33,10 @@ import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.activity.ProfileDetailActivity; import net.ktnx.mobileledger.utils.Colors; +import net.ktnx.mobileledger.utils.ObservableValue; import java.util.Collections; +import java.util.Observer; import androidx.annotation.NonNull; import androidx.recyclerview.widget.ItemTouchHelper; @@ -46,7 +48,13 @@ public class ProfilesRecyclerViewAdapter MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag(); editProfile(view, profile); }; - private boolean editingProfiles = false; + private ObservableValue editingProfiles = new ObservableValue<>(false); + public void addEditingProfilesObserver(Observer o) { + editingProfiles.addObserver(o); + } + public void deleteEditingProfilesObserver(Observer o) { + editingProfiles.deleteObserver(o); + } private RecyclerView recyclerView; private ItemTouchHelper rearrangeHelper; public ProfilesRecyclerViewAdapter() { @@ -66,7 +74,7 @@ public class ProfilesRecyclerViewAdapter public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { - Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(), + Collections.swap(Data.profiles, viewHolder.getAdapterPosition(), target.getAdapterPosition()); MobileLedgerProfile.storeProfilesOrder(); notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); @@ -88,25 +96,22 @@ public class ProfilesRecyclerViewAdapter public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); this.recyclerView = recyclerView; - rearrangeHelper.attachToRecyclerView(recyclerView); - } - public boolean editingProfiles() { - return this.editingProfiles; + if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView); } public void startEditingProfiles() { - if (editingProfiles) return; - this.editingProfiles = true; + if (editingProfiles.get()) return; + this.editingProfiles.set(true); notifyDataSetChanged(); rearrangeHelper.attachToRecyclerView(recyclerView); } public void stopEditingProfiles() { - if (!editingProfiles) return; - this.editingProfiles = false; + if (!editingProfiles.get()) return; + this.editingProfiles.set(false); notifyDataSetChanged(); rearrangeHelper.attachToRecyclerView(null); } public void flipEditingProfiles() { - if (editingProfiles) stopEditingProfiles(); + if (editingProfiles.get()) stopEditingProfiles(); else startEditingProfiles(); } private void editProfile(View view, MobileLedgerProfile profile) { @@ -141,8 +146,17 @@ public class ProfilesRecyclerViewAdapter MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag(); final MobileLedgerProfile currentProfile = Data.profile.get(); final boolean sameProfile = currentProfile.equals(myProfile); - view.setAlpha(sameProfile ? 1 : 0.5f); }); + + View.OnTouchListener dragStarter = (v, event) -> { + if (rearrangeHelper != null && editingProfiles.get()) { + rearrangeHelper.startDrag(holder); + return true; + } + return false; + }; + + holder.tagAndHandleLayout.setOnTouchListener(dragStarter); return holder; } @Override @@ -154,7 +168,8 @@ public class ProfilesRecyclerViewAdapter holder.itemView.setTag(profile); int hue = profile.getThemeId(); - if (hue == -1) holder.mColorTag.setBackgroundColor(Color.TRANSPARENT); + if (hue == -1) holder.mColorTag + .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG)); else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue)); holder.mTitle.setText(profile.getName()); @@ -162,16 +177,15 @@ public class ProfilesRecyclerViewAdapter holder.mEditButton.setOnClickListener(mOnClickListener); - final boolean sameProfile = currentProfile.equals(profile); - holder.itemView.setAlpha(sameProfile ? 1 : 0.5f); + final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile); holder.itemView - .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowLightBG) : null); - if (editingProfiles) { + .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null); + if (editingProfiles.get()) { holder.mRearrangeHandle.setVisibility(View.VISIBLE); holder.mEditButton.setVisibility(View.VISIBLE); } else { - holder.mRearrangeHandle.setVisibility(View.GONE); + holder.mRearrangeHandle.setVisibility(View.INVISIBLE); holder.mEditButton.setVisibility(View.GONE); } } @@ -179,9 +193,13 @@ public class ProfilesRecyclerViewAdapter public int getItemCount() { return Data.profiles.size(); } + public boolean isEditingProfiles() { + return editingProfiles.get(); + } class ProfileListViewHolder extends RecyclerView.ViewHolder { final TextView mEditButton; final TextView mTitle, mColorTag; + final LinearLayout tagAndHandleLayout; final ImageView mRearrangeHandle; ProfileListViewHolder(View view) { @@ -190,6 +208,7 @@ public class ProfilesRecyclerViewAdapter mTitle = view.findViewById(R.id.title); mColorTag = view.findViewById(R.id.colorTag); mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle); + tagAndHandleLayout = view.findViewById(R.id.handle_and_tag); } } }