]> 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 6f533b01d3abffab5ef4d0a2e1afec955a278bea..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
@@ -39,26 +39,24 @@ 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 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);
@@ -75,7 +73,8 @@ public class ProfilesRecyclerViewAdapter
                                   @NonNull RecyclerView.ViewHolder viewHolder,
                                   @NonNull RecyclerView.ViewHolder target) {
                 final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
-                if (profiles == null) throw new AssertionError();
+                if (profiles == null)
+                    throw new AssertionError();
                 Collections.swap(profiles, viewHolder.getAdapterPosition(),
                         target.getAdapterPosition());
                 MobileLedgerProfile.storeProfilesOrder();
@@ -105,44 +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);
         rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void stopEditingProfiles() {
-        if (!editingProfiles.getValue()) return;
+        if (!editingProfiles())
+            return;
         this.editingProfiles.setValue(false);
         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);
@@ -151,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 -> {
@@ -160,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;
             }
@@ -173,33 +188,36 @@ public class ProfilesRecyclerViewAdapter
     @Override
     public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
-        if (profiles == null) throw new AssertionError();
+        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 && animationsEnabled) {
-                Animation a = AnimationUtils
-                        .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_in);
+                Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
+                        R.anim.fade_in);
                 holder.mRearrangeHandle.startAnimation(a);
                 holder.mEditButton.startAnimation(a);
             }
@@ -209,8 +227,8 @@ public class ProfilesRecyclerViewAdapter
             holder.mRearrangeHandle.setVisibility(View.INVISIBLE);
             holder.mEditButton.setVisibility(View.GONE);
             if (wasShown && animationsEnabled) {
-                Animation a = AnimationUtils
-                        .loadAnimation(holder.mRearrangeHandle.getContext(), R.anim.fade_out);
+                Animation a = AnimationUtils.loadAnimation(holder.mRearrangeHandle.getContext(),
+                        R.anim.fade_out);
                 holder.mRearrangeHandle.startAnimation(a);
                 holder.mEditButton.startAnimation(a);
             }
@@ -221,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;