]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
shuffle some classes under proper packages
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfilesRecyclerViewAdapter.java
index 80e0dcdcbcedfd23132012d333701c4eb0d22be7..21984333500a80d4733e684a7f7bd284967eac86 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2021 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
@@ -29,34 +29,37 @@ 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<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
+    private static WeakReference<ProfilesRecyclerViewAdapter> instanceRef;
+    public final MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
     private final View.OnClickListener mOnClickListener = view -> {
         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
         editProfile(view, profile);
     };
-    public MutableLiveData<Boolean> editingProfiles = new MutableLiveData<>(false);
+    private final ItemTouchHelper rearrangeHelper;
     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 +73,8 @@ 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 +87,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);
@@ -93,46 +104,59 @@ public class ProfilesRecyclerViewAdapter
     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
         super.onAttachedToRecyclerView(recyclerView);
         this.recyclerView = recyclerView;
-        if (editingProfiles.getValue()) rearrangeHelper.attachToRecyclerView(recyclerView);
+        if (editingProfiles())
+            rearrangeHelper.attachToRecyclerView(recyclerView);
+    }
+    public boolean editingProfiles() {
+        final Boolean b = editingProfiles.getValue();
+        if (b == null)
+            return false;
+        return b;
     }
     public void startEditingProfiles() {
-        if (editingProfiles.getValue()) return;
+        if (editingProfiles())
+            return;
         this.editingProfiles.setValue(true);
-        notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void stopEditingProfiles() {
-        if (!editingProfiles.getValue()) return;
+        if (!editingProfiles())
+            return;
         this.editingProfiles.setValue(false);
-        notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(null);
     }
     public void flipEditingProfiles() {
-        if (editingProfiles.getValue()) stopEditingProfiles();
-        else startEditingProfiles();
+        if (editingProfiles())
+            stopEditingProfiles();
+        else
+            startEditingProfiles();
     }
     private void editProfile(View view, MobileLedgerProfile 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);
-        if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
+        if (index != -1)
+            intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
 
         context.startActivity(intent);
     }
     private void onProfileRowClicked(View v) {
-        if (editingProfiles.getValue()) return;
+        if (editingProfiles())
+            return;
         MobileLedgerProfile profile = (MobileLedgerProfile) v.getTag();
         if (profile == null)
             throw new IllegalStateException("Profile row without associated profile");
         debug("profiles", "Setting profile to " + profile.getName());
+        if (Data.getProfile() != profile)
+            Data.drawerOpen.setValue(false);
         Data.setCurrentProfile(profile);
     }
     @NonNull
     @Override
     public ProfileListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View view = LayoutInflater.from(parent.getContext())
-                .inflate(R.layout.profile_list_content, parent, false);
+                                  .inflate(R.layout.profile_list_content, parent, false);
         ProfileListViewHolder holder = new ProfileListViewHolder(view);
 
         holder.mRow.setOnClickListener(this::onProfileRowClicked);
@@ -141,7 +165,8 @@ public class ProfilesRecyclerViewAdapter
             onProfileRowClicked(row);
         });
         holder.mColorTag.setOnClickListener(v -> {
-            View row = (View) v.getParent().getParent();
+            View row = (View) v.getParent()
+                               .getParent();
             onProfileRowClicked(row);
         });
         holder.mTitle.setOnLongClickListener(v -> {
@@ -150,7 +175,7 @@ public class ProfilesRecyclerViewAdapter
         });
 
         View.OnTouchListener dragStarter = (v, event) -> {
-            if (rearrangeHelper != null && editingProfiles.getValue()) {
+            if (rearrangeHelper != null && editingProfiles()) {
                 rearrangeHelper.startDrag(holder);
                 return true;
             }
@@ -163,33 +188,36 @@ 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()));
+        final MobileLedgerProfile currentProfile = Data.getProfile();
+//        debug("profiles", String.format(Locale.ENGLISH, "pos %d: %s, current: %s", position,
+//                profile.getUuid(), currentProfile.getUuid()));
         holder.itemView.setTag(profile);
 
-        int hue = profile.getThemeId();
-        if (hue == -1) holder.mColorTag
-                .setBackgroundColor(Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
-        else holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
+        int hue = profile.getThemeHue();
+        if (hue == -1)
+            holder.mColorTag.setBackgroundColor(
+                    Colors.getPrimaryColorForHue(Colors.DEFAULT_HUE_DEG));
+        else
+            holder.mColorTag.setBackgroundColor(Colors.getPrimaryColorForHue(hue));
 
         holder.mTitle.setText(profile.getName());
 //            holder.mSubTitle.setText(profile.getUrl());
 
         holder.mEditButton.setOnClickListener(mOnClickListener);
 
-        final boolean sameProfile = (currentProfile != null) && currentProfile.equals(profile);
-        holder.itemView
-                .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
-        if (editingProfiles.getValue()) {
+        final boolean sameProfile = currentProfile.equals(profile);
+        holder.itemView.setBackground(
+                sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
+        if (editingProfiles()) {
             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);
+            if (wasHidden && animationsEnabled) {
+                Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
+                        R.anim.fade_in);
                 holder.mRearrangeHandle.startAnimation(a);
                 holder.mEditButton.startAnimation(a);
             }
@@ -198,9 +226,9 @@ public class ProfilesRecyclerViewAdapter
             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);
+            if (wasShown && animationsEnabled) {
+                Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
+                        R.anim.fade_out);
                 holder.mRearrangeHandle.startAnimation(a);
                 holder.mEditButton.startAnimation(a);
             }
@@ -211,7 +239,7 @@ public class ProfilesRecyclerViewAdapter
         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
         return profiles != null ? profiles.size() : 0;
     }
-    class ProfileListViewHolder extends RecyclerView.ViewHolder {
+    static class ProfileListViewHolder extends RecyclerView.ViewHolder {
         final TextView mEditButton;
         final TextView mTitle, mColorTag;
         final LinearLayout tagAndHandleLayout;