]> git.ktnx.net Git - mobile-ledger.git/commitdiff
migrate editingProfiles to LiveData
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 May 2019 13:25:31 +0000 (16:25 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 May 2019 13:25:31 +0000 (16:25 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java

index 6a49638fcbb6f79a37640c7b81cf24280ca4444c..4d1f07dbd534222d36f194940f5c0b92a5c28fa4 100644 (file)
@@ -66,7 +66,6 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Locale;
-import java.util.Observer;
 
 import androidx.appcompat.app.ActionBarDrawerToggle;
 import androidx.appcompat.widget.Toolbar;
@@ -104,7 +103,6 @@ public class MainActivity extends ProfileThemedActivity {
     private DrawerLayout.SimpleDrawerListener drawerListener;
     private ActionBarDrawerToggle barDrawerToggle;
     private ViewPager.SimpleOnPageChangeListener pageChangeListener;
-    private Observer editingProfilesObserver;
     private MobileLedgerProfile profile;
     @Override
     protected void onStart() {
@@ -133,9 +131,6 @@ public class MainActivity extends ProfileThemedActivity {
         barDrawerToggle = null;
         if (mViewPager != null) mViewPager.removeOnPageChangeListener(pageChangeListener);
         pageChangeListener = null;
-        if (mProfileListAdapter != null)
-            mProfileListAdapter.deleteEditingProfilesObserver(editingProfilesObserver);
-        editingProfilesObserver = null;
         super.onDestroy();
     }
     @Override
@@ -254,33 +249,37 @@ public class MainActivity extends ProfileThemedActivity {
         if (mProfileListAdapter == null) mProfileListAdapter = new ProfilesRecyclerViewAdapter();
         root.setAdapter(mProfileListAdapter);
 
-        if (editingProfilesObserver == null) {
-            editingProfilesObserver = (o, arg) -> {
-                if (mProfileListAdapter.isEditingProfiles()) {
-                    profileListHeadMore.setVisibility(View.GONE);
-                    profileListHeadMore
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
-                    profileListHeadCancel.setVisibility(View.VISIBLE);
-                    profileListHeadCancel
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
-                    profileListHeadAddProfile.setVisibility(View.VISIBLE);
-                    profileListHeadAddProfile
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
-                }
-                else {
-                    profileListHeadCancel.setVisibility(View.GONE);
-                    profileListHeadCancel
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
-                    profileListHeadMore.setVisibility(View.VISIBLE);
-                    profileListHeadMore
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
-                    profileListHeadAddProfile.setVisibility(View.GONE);
-                    profileListHeadAddProfile
-                            .startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
-                }
-            };
-            mProfileListAdapter.addEditingProfilesObserver(editingProfilesObserver);
-        }
+        mProfileListAdapter.editingProfiles
+                .observe(this, newValue -> {
+                    if (newValue) {
+                        profileListHeadMore.setVisibility(View.GONE);
+                        profileListHeadCancel.setVisibility(View.VISIBLE);
+                        profileListHeadAddProfile.setVisibility(View.VISIBLE);
+                        if (drawer.isDrawerOpen(GravityCompat.START)) {
+                            profileListHeadMore.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_out));
+                            profileListHeadCancel.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_in));
+                            profileListHeadAddProfile.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_in));
+                        }
+                    }
+                    else {
+                        profileListHeadCancel.setVisibility(View.GONE);
+                        profileListHeadMore.setVisibility(View.VISIBLE);
+                        profileListHeadAddProfile.setVisibility(View.GONE);
+                        if (drawer.isDrawerOpen(GravityCompat.START)) {
+                            profileListHeadCancel.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_out));
+                            profileListHeadMore.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_in));
+                            profileListHeadAddProfile.startAnimation(AnimationUtils
+                                    .loadAnimation(MainActivity.this, R.anim.fade_out));
+                        }
+                    }
+
+                    mProfileListAdapter.notifyDataSetChanged();
+                });
 
         LinearLayoutManager llm = new LinearLayoutManager(this);
 
index f24f6f0cc10155aa2730f689dff0d491a4afa915..80e0dcdcbcedfd23132012d333701c4eb0d22be7 100644 (file)
@@ -34,15 +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.Locale;
-import java.util.Observer;
 
 import androidx.annotation.NonNull;
 import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.lifecycle.MutableLiveData;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -54,7 +53,7 @@ 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;
     public ProfilesRecyclerViewAdapter() {
@@ -84,12 +83,6 @@ 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);
@@ -100,22 +93,22 @@ 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);
+        if (editingProfiles.getValue()) return;
+        this.editingProfiles.setValue(true);
         notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void stopEditingProfiles() {
-        if (!editingProfiles.get()) return;
-        this.editingProfiles.set(false);
+        if (!editingProfiles.getValue()) return;
+        this.editingProfiles.setValue(false);
         notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(null);
     }
     public void flipEditingProfiles() {
-        if (editingProfiles.get()) stopEditingProfiles();
+        if (editingProfiles.getValue()) stopEditingProfiles();
         else startEditingProfiles();
     }
     private void editProfile(View view, MobileLedgerProfile profile) {
@@ -128,7 +121,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,7 +150,7 @@ public class ProfilesRecyclerViewAdapter
         });
 
         View.OnTouchListener dragStarter = (v, event) -> {
-            if (rearrangeHelper != null && editingProfiles.get()) {
+            if (rearrangeHelper != null && editingProfiles.getValue()) {
                 rearrangeHelper.startDrag(holder);
                 return true;
             }
@@ -190,7 +183,7 @@ 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);
@@ -218,9 +211,6 @@ public class ProfilesRecyclerViewAdapter
         final ArrayList<MobileLedgerProfile> profiles = Data.profiles.getValue();
         return profiles != null ? profiles.size() : 0;
     }
-    public boolean isEditingProfiles() {
-        return editingProfiles.get();
-    }
     class ProfileListViewHolder extends RecyclerView.ViewHolder {
         final TextView mEditButton;
         final TextView mTitle, mColorTag;