]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
whitespace
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
index 8ee0dd96c696c5a299a52cdfe38a4b034f7b3a40..4ad4fdbe0d775323f027b5c996c6fc2a8175dc98 100644 (file)
@@ -34,13 +34,14 @@ 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.ArrayList;
 import java.util.Collections;
-import java.util.Observer;
+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;
 
@@ -52,9 +53,10 @@ public class ProfilesRecyclerViewAdapter
         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
         editProfile(view, profile);
     };
-    private ObservableValue<Boolean> editingProfiles = new ObservableValue<>(false);
+    public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
     private RecyclerView recyclerView;
     private ItemTouchHelper rearrangeHelper;
+    private boolean animationsEnabled = true;
     public ProfilesRecyclerViewAdapter() {
         debug("flow", "ProfilesRecyclerViewAdapter.new()");
 
@@ -68,16 +70,12 @@ public class ProfilesRecyclerViewAdapter
             public boolean onMove(@NonNull RecyclerView recyclerView,
                                   @NonNull RecyclerView.ViewHolder viewHolder,
                                   @NonNull RecyclerView.ViewHolder target) {
-                Data.profiles.blockNotifications();
-                try {
-                    Collections.swap(Data.profiles, viewHolder.getAdapterPosition(),
-                            target.getAdapterPosition());
-                    MobileLedgerProfile.storeProfilesOrder();
-                    notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
-                }
-                finally {
-                    Data.profiles.unblockNotifications();
-                }
+                final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+                assert profiles != null;
+                Collections.swap(profiles, viewHolder.getAdapterPosition(),
+                        target.getAdapterPosition());
+                MobileLedgerProfile.storeProfilesOrder();
+                notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                 return true;
             }
             @Override
@@ -86,11 +84,8 @@ public class ProfilesRecyclerViewAdapter
         };
         rearrangeHelper = new ItemTouchHelper(cb);
     }
-    public void addEditingProfilesObserver(Observer o) {
-        editingProfiles.addObserver(o);
-    }
-    public void deleteEditingProfilesObserver(Observer o) {
-        editingProfiles.deleteObserver(o);
+    public void setAnimationsEnabled(boolean animationsEnabled) {
+        this.animationsEnabled = animationsEnabled;
     }
     @Override
     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
@@ -102,26 +97,24 @@ public class ProfilesRecyclerViewAdapter
     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
         super.onAttachedToRecyclerView(recyclerView);
         this.recyclerView = recyclerView;
-        if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
+        if (editingProfiles.getValue()) rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void startEditingProfiles() {
-        if (editingProfiles.get()) return;
-        this.editingProfiles.set(true);
-        notifyDataSetChanged();
+        if (editingProfiles.getValue()) return;
+        this.editingProfiles.setValue(true);
         rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void stopEditingProfiles() {
-        if (!editingProfiles.get()) return;
-        this.editingProfiles.set(false);
-        notifyDataSetChanged();
+        if (!editingProfiles.getValue()) return;
+        this.editingProfiles.setValue(false);
         rearrangeHelper.attachToRecyclerView(null);
     }
     public void flipEditingProfiles() {
-        if (editingProfiles.get()) stopEditingProfiles();
+        if (editingProfiles.getValue()) stopEditingProfiles();
         else startEditingProfiles();
     }
     private void editProfile(View view, MobileLedgerProfile profile) {
-        int index = Data.profiles.indexOf(profile);
+        int index = Data.getProfileIndex(profile);
         Context context = view.getContext();
         Intent intent = new Intent(context, ProfileDetailActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
@@ -130,7 +123,7 @@ public class ProfilesRecyclerViewAdapter
         context.startActivity(intent);
     }
     private void onProfileRowClicked(View v) {
-        if (editingProfiles.get()) return;
+        if (editingProfiles.getValue()) return;
         MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
         if (profile == null)
             throw new IllegalStateException("Profile row without associated profile");
@@ -157,14 +150,9 @@ public class ProfilesRecyclerViewAdapter
             flipEditingProfiles();
             return true;
         });
-        Data.profile.addObserver((o, arg) -> {
-            MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag();
-            final MobileLedgerProfile currentProfile = Data.profile.get();
-            final boolean sameProfile = currentProfile.equals(myProfile);
-        });
 
         View.OnTouchListener dragStarter = (v, event) -> {
-            if (rearrangeHelper != null && editingProfiles.get()) {
+            if (rearrangeHelper != null && editingProfiles.getValue()) {
                 rearrangeHelper.startDrag(holder);
                 return true;
             }
@@ -176,10 +164,12 @@ public class ProfilesRecyclerViewAdapter
     }
     @Override
     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
-        final MobileLedgerProfile profile = Data.profiles.get(position);
-        final MobileLedgerProfile currentProfile = Data.profile.get();
-        debug("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
-                (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
+        final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+        assert profiles != null;
+        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()));
         holder.itemView.setTag(profile);
 
         int hue = profile.getThemeId();
@@ -195,11 +185,11 @@ public class ProfilesRecyclerViewAdapter
         final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
         holder.itemView
                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
-        if (editingProfiles.get()) {
+        if (editingProfiles.getValue()) {
             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);
@@ -210,7 +200,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);
@@ -220,10 +210,8 @@ public class ProfilesRecyclerViewAdapter
     }
     @Override
     public int getItemCount() {
-        return Data.profiles.size();
-    }
-    public boolean isEditingProfiles() {
-        return editingProfiles.get();
+        final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+        return profiles != null ? profiles.size() : 0;
     }
     class ProfileListViewHolder extends RecyclerView.ViewHolder {
         final TextView mEditButton;