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=974c979322f79ff0e2f7c74aab3dc07e8cd93d2f;hp=80e0dcdcbcedfd23132012d333701c4eb0d22be7;hb=5fb830346325e20be596a09118d377b26f09779b;hpb=3c8db4ad0cf45ddddd2c9fc141a9874795c1e5ba 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 80e0dcdc..974c9793 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 @@ -29,26 +29,29 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.lifecycle.MutableLiveData; +import androidx.recyclerview.widget.ItemTouchHelper; +import androidx.recyclerview.widget.RecyclerView; + import net.ktnx.mobileledger.R; 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 java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; import java.util.Locale; -import androidx.annotation.NonNull; -import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.lifecycle.MutableLiveData; -import androidx.recyclerview.widget.ItemTouchHelper; -import androidx.recyclerview.widget.RecyclerView; - import static net.ktnx.mobileledger.utils.Logger.debug; public class ProfilesRecyclerViewAdapter extends RecyclerView.Adapter { + private static WeakReference instanceRef; private final View.OnClickListener mOnClickListener = view -> { MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag(); editProfile(view, profile); @@ -56,7 +59,9 @@ public class ProfilesRecyclerViewAdapter public MutableLiveData editingProfiles = new MutableLiveData<>(false); private RecyclerView recyclerView; private ItemTouchHelper rearrangeHelper; + private boolean animationsEnabled = true; public ProfilesRecyclerViewAdapter() { + instanceRef = new WeakReference<>(this); debug("flow", "ProfilesRecyclerViewAdapter.new()"); ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() { @@ -70,7 +75,7 @@ public class ProfilesRecyclerViewAdapter @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) { final ArrayList profiles = Data.profiles.getValue(); - assert profiles != null; + if (profiles == null) throw new AssertionError(); Collections.swap(profiles, viewHolder.getAdapterPosition(), target.getAdapterPosition()); MobileLedgerProfile.storeProfilesOrder(); @@ -83,6 +88,13 @@ public class ProfilesRecyclerViewAdapter }; rearrangeHelper = new ItemTouchHelper(cb); } + public static @Nullable + ProfilesRecyclerViewAdapter getInstance() { + return instanceRef.get(); + } + public void setAnimationsEnabled(boolean animationsEnabled) { + this.animationsEnabled = animationsEnabled; + } @Override public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) { rearrangeHelper.attachToRecyclerView(null); @@ -98,13 +110,11 @@ public class ProfilesRecyclerViewAdapter public void startEditingProfiles() { if (editingProfiles.getValue()) return; this.editingProfiles.setValue(true); - notifyDataSetChanged(); rearrangeHelper.attachToRecyclerView(recyclerView); } public void stopEditingProfiles() { if (!editingProfiles.getValue()) return; this.editingProfiles.setValue(false); - notifyDataSetChanged(); rearrangeHelper.attachToRecyclerView(null); } public void flipEditingProfiles() { @@ -126,6 +136,8 @@ public class ProfilesRecyclerViewAdapter if (profile == null) throw new IllegalStateException("Profile row without associated profile"); debug("profiles", "Setting profile to " + profile.getName()); + if (Data.profile.getValue() != profile ) + Data.drawerOpen.setValue(false); Data.setCurrentProfile(profile); } @NonNull @@ -163,14 +175,14 @@ public class ProfilesRecyclerViewAdapter @Override public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) { final ArrayList profiles = Data.profiles.getValue(); - assert profiles != null; + if (profiles == null) throw new AssertionError(); final MobileLedgerProfile profile = profiles.get(position); final MobileLedgerProfile currentProfile = Data.profile.getValue(); - debug("profiles", String.format(Locale.ENGLISH,"pos %d: %s, current: %s", position, profile.getUuid(), - (currentProfile == null) ? "" : currentProfile.getUuid())); + debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position, + profile.getUuid(), (currentProfile == null) ? "" : currentProfile.getUuid())); holder.itemView.setTag(profile); - int hue = profile.getThemeId(); + int hue = profile.getThemeHue(); if (hue == -1) holder.mColorTag .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG)); else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue)); @@ -187,7 +199,7 @@ public class ProfilesRecyclerViewAdapter boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE; holder.mRearrangeHandle.setVisibility(View.VISIBLE); holder.mEditButton.setVisibility(View.VISIBLE); - if (wasHidden) { + if (wasHidden && animationsEnabled) { Animation a = AnimationUtils .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_in); holder.mRearrangeHandle.startAnimation(a); @@ -198,7 +210,7 @@ public class ProfilesRecyclerViewAdapter boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE; holder.mRearrangeHandle.setVisibility(View.INVISIBLE); holder.mEditButton.setVisibility(View.GONE); - if (wasShown) { + if (wasShown && animationsEnabled) { Animation a = AnimationUtils .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out); holder.mRearrangeHandle.startAnimation(a);