From 9f687628614baacdf48cecf395041b8826ef76bf Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 27 Feb 2019 23:23:53 +0200 Subject: [PATCH] profile list: add a button for cancelling the edit mode --- .../ui/activity/MainActivity.java | 21 ++++++++++ .../profiles/ProfilesRecyclerViewAdapter.java | 30 ++++++++------ .../main/res/layout/nav_profile_list_head.xml | 39 +++++++++++++++---- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 3d53dd50..3c50331e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java index 013fa191..0ee87da1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfilesRecyclerViewAdapter.java @@ -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 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; diff --git a/app/src/main/res/layout/nav_profile_list_head.xml b/app/src/main/res/layout/nav_profile_list_head.xml index 540ded50..cdd51fa1 100644 --- a/app/src/main/res/layout/nav_profile_list_head.xml +++ b/app/src/main/res/layout/nav_profile_list_head.xml @@ -23,16 +23,39 @@ android:layout_height="@dimen/thumb_row_height" android:paddingEnd="@dimen/activity_horizontal_margin"> - + app:layout_constraintTop_toTopOf="parent"> + + + + + + -- 2.39.5