]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
migrate a bunch of globals to LiveData
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
index 4531e9fad92681096259a3e8e8c4b53f6d07de19..f24f6f0cc10155aa2730f689dff0d491a4afa915 100644 (file)
@@ -23,6 +23,8 @@ import android.graphics.drawable.ColorDrawable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.animation.Animation;
+import android.view.animation.AnimationUtils;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
@@ -34,7 +36,9 @@ 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.Locale;
 import java.util.Observer;
 
 import androidx.annotation.NonNull;
@@ -66,15 +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
@@ -118,7 +119,7 @@ public class ProfilesRecyclerViewAdapter
         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);
@@ -127,6 +128,7 @@ public class ProfilesRecyclerViewAdapter
         context.startActivity(intent);
     }
     private void onProfileRowClicked(View v) {
+        if (editingProfiles.get()) return;
         MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
         if (profile == null)
             throw new IllegalStateException("Profile row without associated profile");
@@ -153,11 +155,6 @@ 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()) {
@@ -172,9 +169,11 @@ 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(),
+        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);
 
@@ -192,17 +191,32 @@ public class ProfilesRecyclerViewAdapter
         holder.itemView
                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
         if (editingProfiles.get()) {
+            boolean wasHidden = holder.mEditButton.getVisibility() == View.GONE;
             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
             holder.mEditButton.setVisibility(View.VISIBLE);
+            if (wasHidden) {
+                Animation a = AnimationUtils
+                        .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_in);
+                holder.mRearrangeHandle.startAnimation(a);
+                holder.mEditButton.startAnimation(a);
+            }
         }
         else {
+            boolean wasShown = holder.mEditButton.getVisibility() == View.VISIBLE;
             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
             holder.mEditButton.setVisibility(View.GONE);
+            if (wasShown) {
+                Animation a = AnimationUtils
+                        .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out);
+                holder.mRearrangeHandle.startAnimation(a);
+                holder.mEditButton.startAnimation(a);
+            }
         }
     }
     @Override
     public int getItemCount() {
-        return Data.profiles.size();
+        final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
+        return profiles != null ? profiles.size() : 0;
     }
     public boolean isEditingProfiles() {
         return editingProfiles.get();