]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
more animations when collapsing profile list and when starting/editting list edit
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
index 9f2c48a246e253edc70a527435c3963c2e47b9b0..8ee0dd96c696c5a299a52cdfe38a4b034f7b3a40 100644 (file)
@@ -1,18 +1,18 @@
 /*
  * 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 <https://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
  */
 
 package net.ktnx.mobileledger.ui.profiles;
@@ -20,10 +20,11 @@ package net.ktnx.mobileledger.ui.profiles;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.drawable.ColorDrawable;
-import android.util.Log;
 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;
@@ -39,9 +40,12 @@ import java.util.Collections;
 import java.util.Observer;
 
 import androidx.annotation.NonNull;
+import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
 
+import static net.ktnx.mobileledger.utils.Logger.debug;
+
 public class ProfilesRecyclerViewAdapter
         extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
     private final View.OnClickListener mOnClickListener = view -> {
@@ -49,20 +53,10 @@ public class ProfilesRecyclerViewAdapter
         editProfile(view, profile);
     };
     private ObservableValue<Boolean> 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() {
-        Data.profiles.addObserver((o, arg) -> {
-            Log.d("profiles", "profile list changed");
-            if (arg == null) notifyDataSetChanged();
-            else notifyItemChanged((int) arg);
-        });
+        debug("flow", "ProfilesRecyclerViewAdapter.new()");
 
         ItemTouchHelper.Callback cb = new ItemTouchHelper.Callback() {
             @Override
@@ -74,10 +68,16 @@ public class ProfilesRecyclerViewAdapter
             public boolean onMove(@NonNull RecyclerView recyclerView,
                                   @NonNull RecyclerView.ViewHolder viewHolder,
                                   @NonNull RecyclerView.ViewHolder target) {
-                Collections.swap(Data.profiles.getList(), viewHolder.getAdapterPosition(),
-                        target.getAdapterPosition());
-                MobileLedgerProfile.storeProfilesOrder();
-                notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+                Data.profiles.blockNotifications();
+                try {
+                    Collections.swap(Data.profiles, viewHolder.getAdapterPosition(),
+                            target.getAdapterPosition());
+                    MobileLedgerProfile.storeProfilesOrder();
+                    notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+                }
+                finally {
+                    Data.profiles.unblockNotifications();
+                }
                 return true;
             }
             @Override
@@ -86,6 +86,12 @@ public class ProfilesRecyclerViewAdapter
         };
         rearrangeHelper = new ItemTouchHelper(cb);
     }
+    public void addEditingProfilesObserver(Observer o) {
+        editingProfiles.addObserver(o);
+    }
+    public void deleteEditingProfilesObserver(Observer o) {
+        editingProfiles.deleteObserver(o);
+    }
     @Override
     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
         rearrangeHelper.attachToRecyclerView(null);
@@ -123,6 +129,14 @@ 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");
+        debug("profiles", "Setting profile to " + profile.getName());
+        Data.setCurrentProfile(profile);
+    }
     @NonNull
     @Override
     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@@ -130,13 +144,14 @@ public class ProfilesRecyclerViewAdapter
                 .inflate(R.layout.profile_list_content, parent, false);
         ProfileListViewHolder holder = new ProfileListViewHolder(view);
 
+        holder.mRow.setOnClickListener(this::onProfileRowClicked);
         holder.mTitle.setOnClickListener(v -> {
             View row = (View) v.getParent();
-            MobileLedgerProfile profile = (MobileLedgerProfile) row.getTag();
-            if (profile == null)
-                throw new IllegalStateException("Profile row without associated profile");
-            Log.d("profiles", "Setting profile to " + profile.getName());
-            Data.setCurrentProfile(profile);
+            onProfileRowClicked(row);
+        });
+        holder.mColorTag.setOnClickListener(v -> {
+            View row = (View) v.getParent().getParent();
+            onProfileRowClicked(row);
         });
         holder.mTitle.setOnLongClickListener(v -> {
             flipEditingProfiles();
@@ -163,7 +178,7 @@ public class ProfilesRecyclerViewAdapter
     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
         final MobileLedgerProfile profile = Data.profiles.get(position);
         final MobileLedgerProfile currentProfile = Data.profile.get();
-        Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
+        debug("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
                 (currentProfile == null) ? "<NULL>" : currentProfile.getUuid()));
         holder.itemView.setTag(profile);
 
@@ -177,16 +192,30 @@ public class ProfilesRecyclerViewAdapter
 
         holder.mEditButton.setOnClickListener(mOnClickListener);
 
-        final boolean sameProfile = currentProfile.equals(profile);
+        final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
         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
@@ -201,6 +230,7 @@ public class ProfilesRecyclerViewAdapter
         final TextView mTitle, mColorTag;
         final LinearLayout tagAndHandleLayout;
         final ImageView mRearrangeHandle;
+        final ConstraintLayout mRow;
 
         ProfileListViewHolder(View view) {
             super(view);
@@ -209,6 +239,7 @@ public class ProfilesRecyclerViewAdapter
             mColorTag = view.findViewById(R.id.colorTag);
             mRearrangeHandle = view.findViewById(R.id.profile_list_rearrange_handle);
             tagAndHandleLayout = view.findViewById(R.id.handle_and_tag);
+            mRow = (ConstraintLayout) view;
         }
     }
 }