]> git.ktnx.net Git - mobile-ledger.git/commitdiff
profile list: add a button for cancelling the edit mode
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Feb 2019 21:23:53 +0000 (23:23 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Feb 2019 21:23:53 +0000 (23:23 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java
app/src/main/res/layout/nav_profile_list_head.xml

index 3d53dd50094bf8e8df82f181d48d43b6fb59d4de..3c50331e52c06366e3328c8d13e00376b6a35f19 100644 (file)
@@ -52,6 +52,8 @@ import net.ktnx.mobileledger.utils.MLDB;
 import java.lang.ref.WeakReference;
 import java.text.DateFormat;
 import java.util.Date;
+import java.util.Observable;
+import java.util.Observer;
 
 import androidx.appcompat.app.ActionBarDrawerToggle;
 import androidx.appcompat.widget.Toolbar;
@@ -236,6 +238,25 @@ public class MainActivity extends CrashReportingActivity {
         mProfileListAdapter = new ProfilesRecyclerViewAdapter();
         root.setAdapter(mProfileListAdapter);
 
+        mProfileListAdapter.addEditingProfilesObserver(new Observer() {
+            @Override
+            public void update(Observable o, Object arg) {
+                if (mProfileListAdapter.isEditingProfiles()) {
+                    findViewById(R.id.nav_profiles_arrow).setVisibility(View.GONE);
+                    findViewById(R.id.nav_profiles_arrow).setAlpha(0f);
+                    findViewById(R.id.nav_profiles_cancel_edit).setVisibility(View.VISIBLE);
+                }
+                else {
+                    findViewById(R.id.nav_profiles_arrow).setVisibility(View.VISIBLE);
+                    findViewById(R.id.nav_profiles_arrow).setAlpha(1f);
+                    findViewById(R.id.nav_profiles_cancel_edit).setVisibility(View.GONE);
+                }
+            }
+        });
+
+        findViewById(R.id.nav_profiles_cancel_edit).setOnClickListener((v) -> {
+            mProfileListAdapter.stopEditingProfiles();
+        });
         LinearLayoutManager llm = new LinearLayoutManager(this);
 
         llm.setOrientation(RecyclerView.VERTICAL);
index 013fa1915f349849337c214b051ab16fa9ebfec3..0ee87da1157177718218a5a2636c66a27c577183 100644 (file)
@@ -32,8 +32,10 @@ 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.Collections;
+import java.util.Observer;
 
 import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.ItemTouchHelper;
@@ -45,7 +47,13 @@ public class ProfilesRecyclerViewAdapter
         MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag();
         editProfile(view, profile);
     };
-    private boolean editingProfiles = false;
+    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() {
@@ -87,25 +95,22 @@ public class ProfilesRecyclerViewAdapter
     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
         super.onAttachedToRecyclerView(recyclerView);
         this.recyclerView = recyclerView;
-        if (editingProfiles) rearrangeHelper.attachToRecyclerView(recyclerView);
-    }
-    public boolean editingProfiles() {
-        return this.editingProfiles;
+        if (editingProfiles.get()) rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void startEditingProfiles() {
-        if (editingProfiles) return;
-        this.editingProfiles = true;
+        if (editingProfiles.get()) return;
+        this.editingProfiles.set(true);
         notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(recyclerView);
     }
     public void stopEditingProfiles() {
-        if (!editingProfiles) return;
-        this.editingProfiles = false;
+        if (!editingProfiles.get()) return;
+        this.editingProfiles.set(false);
         notifyDataSetChanged();
         rearrangeHelper.attachToRecyclerView(null);
     }
     public void flipEditingProfiles() {
-        if (editingProfiles) stopEditingProfiles();
+        if (editingProfiles.get()) stopEditingProfiles();
         else startEditingProfiles();
     }
     private void editProfile(View view, MobileLedgerProfile profile) {
@@ -171,7 +176,7 @@ public class ProfilesRecyclerViewAdapter
         holder.itemView.setAlpha(sameProfile ? 1 : 0.5f);
         holder.itemView
                 .setBackground(sameProfile ? new ColorDrawable(Colors.tableRowDarkBG) : null);
-        if (editingProfiles) {
+        if (editingProfiles.get()) {
             holder.mRearrangeHandle.setVisibility(View.VISIBLE);
             holder.mEditButton.setVisibility(View.VISIBLE);
         }
@@ -184,6 +189,9 @@ public class ProfilesRecyclerViewAdapter
     public int getItemCount() {
         return Data.profiles.size();
     }
+    public boolean isEditingProfiles() {
+        return editingProfiles.get();
+    }
     class ProfileListViewHolder extends RecyclerView.ViewHolder {
         final TextView mEditButton;
         final TextView mTitle, mColorTag;
index 540ded50c50a8ea86bacf75c326de5af3c7c99fb..cdd51fa11c446bf117d2e15b0feda00dc6a92262 100644 (file)
     android:layout_height="@dimen/thumb_row_height"
     android:paddingEnd="@dimen/activity_horizontal_margin">
 
-    <TextView
-        android:id="@+id/nav_profiles_arrow"
+    <androidx.appcompat.widget.LinearLayoutCompat
+        android:id="@+id/nav_profile_list_head_buttons"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:drawableStart="@drawable/ic_expand_more_black_24dp"
-        android:gravity="end|center_vertical"
-        android:onClick="navProfilesHeadClicked"
+        android:gravity="center_vertical"
+        android:orientation="horizontal"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
+        app:layout_constraintTop_toTopOf="parent">
+
+        <ImageView
+            android:id="@+id/nav_profiles_arrow"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:background="@drawable/ic_expand_more_black_24dp"
+            android:gravity="end|center_vertical"
+            android:onClick="navProfilesHeadClicked"
+            app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toTopOf="parent" />
+
+        <ImageView
+            android:id="@+id/nav_profiles_cancel_edit"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:background="@drawable/ic_clear_black_24dp"
+            android:gravity="end|center_vertical"
+            android:visibility="gone"
+            app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toTopOf="parent" />
+
+    </androidx.appcompat.widget.LinearLayoutCompat>
 
     <TextView
         android:id="@+id/nav_profiles_label"
         android:layout_marginEnd="8dp"
         android:drawableStart="@drawable/ic_view_list_black_24dp"
         android:gravity="start|center_vertical"
-        android:text="@string/profiles"
         android:onClick="navProfilesHeadClicked"
+        android:text="@string/profiles"
         app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toStartOf="@+id/nav_profiles_arrow"
+        app:layout_constraintEnd_toStartOf="@+id/nav_profile_list_head_buttons"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />