]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
ProfilesRecyclerViewAdapter as a weak singleton
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
index 80e0dcdcbcedfd23132012d333701c4eb0d22be7..bd5a3fec5996417aa4dd165f845f92537ba7be7d 100644 (file)
@@ -35,11 +35,13 @@ 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.annotation.Nullable;
 import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.lifecycle.MutableLiveData;
 import androidx.recyclerview.widget.ItemTouchHelper;
@@ -54,9 +56,12 @@ public class ProfilesRecyclerViewAdapter
         editProfile(view, profile);
     };
     public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
+    private static WeakReference<ProfilesRecyclerViewAdapter> instanceRef;
     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<MobileLedgerProfile> 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,12 @@ 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 +109,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() {
@@ -163,11 +172,11 @@ public class ProfilesRecyclerViewAdapter
     @Override
     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
         final ArrayList<MobileLedgerProfile> 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) ? "<NULL>" : currentProfile.getUuid()));
+        debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
+                profile.getUuid(), (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
         holder.itemView.setTag(profile);
 
         int hue = profile.getThemeId();
@@ -187,7 +196,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 +207,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);