From b06a7a291e35add2dfc89313d226c5efd1bae3b3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 10 Jan 2019 21:28:06 +0000 Subject: [PATCH] multiple profile list fixes show fab upon activity start (for example after editing/deleting a profile) fix the flow of events when the current profile is changed or deleted --- .../model/MobileLedgerProfile.java | 2 +- .../ui/activity/MainActivity.java | 20 +++----- .../ui/activity/ProfileListActivity.java | 49 ++++++++++--------- .../ui/profiles/ProfileDetailActivity.java | 28 +++++------ .../ui/profiles/ProfileDetailFragment.java | 12 +++-- .../mobileledger/utils/ObservableList.java | 21 ++++++-- 6 files changed, 68 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index bbcf57fa..36e317af 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -273,7 +273,7 @@ public final class MobileLedgerProfile { } public void removeFromDB() { SQLiteDatabase db = MLDB.getWritableDatabase(); - Log.d("db", String.format("removinf progile %s from DB", uuid)); + Log.d("db", String.format("removing progile %s from DB", uuid)); db.execSQL("delete from profiles where uuid=?", new Object[]{uuid}); } } 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 7e9469a2..0991a694 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 @@ -54,8 +54,6 @@ import java.text.DateFormat; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; -import java.util.Observable; -import java.util.Observer; public class MainActivity extends AppCompatActivity { public MobileLedgerListFragment currentFragment = null; @@ -96,15 +94,12 @@ public class MainActivity extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - Data.profile.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - MobileLedgerProfile profile = Data.profile.get(); - runOnUiThread(() -> { - if (profile == null) toolbar.setSubtitle(""); - else toolbar.setSubtitle(profile.getName()); - }); - } + Data.profile.addObserver((o, arg) -> { + MobileLedgerProfile profile = Data.profile.get(); + runOnUiThread(() -> { + if (profile == null) toolbar.setSubtitle(""); + else toolbar.setSubtitle(profile.getName()); + }); }); setupProfile(); @@ -215,8 +210,7 @@ public class MainActivity extends AppCompatActivity { if (profile == null) throw new AssertionError("profile must have a value"); - Data.profile.set(profile); - MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid()); + Data.setCurrentProfile(profile); if (profile.getUrl().isEmpty()) { Intent intent = new Intent(this, ProfileListActivity.class); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java index 5ba6714d..cb37f5f1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java @@ -39,10 +39,10 @@ import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity; import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment; -import net.ktnx.mobileledger.utils.MLDB; import java.util.Collections; -import java.util.List; +import java.util.Observable; +import java.util.Observer; /** * An activity representing a list of Profiles. This activity @@ -98,12 +98,10 @@ public class ProfileListActivity extends AppCompatActivity { Log.d("profiles", "got edit profile action"); int index = getIntent().getIntExtra(ARG_PROFILE_INDEX, -1); if (index >= 0) { - List list = MobileLedgerProfile.loadAllFromDB(); - if (index < list.size()) { - ProfilesRecyclerViewAdapter adapter = - (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); - if (adapter != null) adapter.editProfile(recyclerView, list.get(index)); - } + MobileLedgerProfile profile = Data.profiles.get(index); + ProfilesRecyclerViewAdapter adapter = + (ProfilesRecyclerViewAdapter) recyclerView.getAdapter(); + if (adapter != null) adapter.editProfile(recyclerView, profile); } } } @@ -168,21 +166,23 @@ public class ProfileListActivity extends AppCompatActivity { private final ProfileListActivity mParentActivity; private final boolean mTwoPane; private final View.OnClickListener mOnClickListener = view -> { - MobileLedgerProfile item = (MobileLedgerProfile) ((View) view.getParent()).getTag(); - editProfile(view, item); + MobileLedgerProfile profile = (MobileLedgerProfile) ((View) view.getParent()).getTag(); + editProfile(view, profile); }; ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) { mParentActivity = parent; mTwoPane = twoPane; Data.profiles.addObserver((o, arg) -> { Log.d("profiles", "profile list changed"); - notifyDataSetChanged(); + if (arg == null) notifyDataSetChanged(); + else notifyItemChanged((int) arg); }); } - private void editProfile(View view, MobileLedgerProfile item) { + private void editProfile(View view, MobileLedgerProfile profile) { + int index = Data.profiles.indexOf(profile); if (mTwoPane) { Bundle arguments = new Bundle(); - arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, item.getUuid()); + arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index); ProfileDetailFragment fragment = new ProfileDetailFragment(); fragment.setArguments(arguments); mParentActivity.getSupportFragmentManager().beginTransaction() @@ -191,8 +191,7 @@ public class ProfileListActivity extends AppCompatActivity { else { Context context = view.getContext(); Intent intent = new Intent(context, ProfileDetailActivity.class); - intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, - (item == null) ? null : item.getUuid()); + if (index != -1) intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index); context.startActivity(intent); } @@ -206,8 +205,7 @@ public class ProfileListActivity extends AppCompatActivity { Data.profile.addObserver((o, arg) -> { MobileLedgerProfile newProfile = Data.profile.get(); MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag(); - holder.mRadioView.setChecked( - newProfile != null && newProfile.getUuid().equals(profile.getUuid())); + holder.mRadioView.setChecked(profile.equals(newProfile)); }); return holder; } @@ -217,21 +215,24 @@ public class ProfileListActivity extends AppCompatActivity { final MobileLedgerProfile currentProfile = Data.profile.get(); Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(), currentProfile.getUuid())); - View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true); + View.OnClickListener profileSelector = v -> { + holder.mRadioView.setChecked(true); + Data.setCurrentProfile(profile); + }; + Data.profile.addObserver(new Observer() { + @Override + public void update(Observable o, Object arg) { + holder.mRadioView.setChecked(Data.profile.get().equals(profile)); + } + }); holder.mTitle.setText(profile.getName()); holder.mTitle.setOnClickListener(profileSelector); holder.mSubTitle.setText(profile.getUrl()); holder.mSubTitle.setOnClickListener(profileSelector); holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid())); - holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> { - if (!isChecked) return; - MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid()); - Data.profile.set(profile); - }); holder.itemView.setTag(profile); holder.mEditButton.setOnClickListener(mOnClickListener); - } @Override public int getItemCount() { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java index 982fccfa..1db1429a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java @@ -74,28 +74,19 @@ public class ProfileDetailActivity extends AppCompatActivity { // http://developer.android.com/guide/components/fragments.html // if (savedInstanceState == null) { - final String profileUUID = - getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID); + final int index = getIntent().getIntExtra(ProfileDetailFragment.ARG_ITEM_ID, -1); - if (profileUUID != null) { - int i = 0; - for (MobileLedgerProfile p : Data.profiles.getList()) { - if (p.getUuid().equals(profileUUID)) { - Log.d("profiles", String.format("found profile %s at %d", profileUUID, i)); - profile = p; - break; - } - i++; - } + if (index != -1) { + profile = Data.profiles.get(index); if (profile == null) throw new AssertionError( - String.format("Can't get profile " + "(uuid:%s) from the " + "global list", - profileUUID)); + String.format("Can't get profile " + "(index:%d) from the global list", + index)); } // Create the detail fragment and add it to the activity // using a fragment transaction. Bundle arguments = new Bundle(); - arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID); + arguments.putInt(ProfileDetailFragment.ARG_ITEM_ID, index); ProfileDetailFragment fragment = new ProfileDetailFragment(); fragment.setArguments(arguments); getSupportFragmentManager().beginTransaction() @@ -109,10 +100,13 @@ public class ProfileDetailActivity extends AppCompatActivity { getMenuInflater().inflate(R.menu.profile_details, menu); MenuItem menuDeleteProfile = menu.findItem(R.id.menuDelete); menuDeleteProfile.setOnMenuItemClickListener(item -> { - Log.d("profiles", String.format("deleting profile %s", profile.getUuid())); + Log.d("profiles", String.format("[activity] deleting profile %s", profile.getUuid())); profile.removeFromDB(); Data.profiles.remove(profile); - Data.profile.set(Data.profiles.get(0)); + if (Data.profile.get().equals(profile)) { + Log.d("profiles", "[activity] selecting profile 0"); + Data.setCurrentProfile(Data.profiles.get(0)); + } finish(); return true; }); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index 5a1b729a..bf2affd0 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -81,8 +81,9 @@ public class ProfileDetailFragment extends Fragment { Log.d("profiles", String.format("[fragment] removing profile %s", mProfile.getUuid())); mProfile.removeFromDB(); Data.profiles.remove(mProfile); - if (Data.profile.get().getUuid().equals(mProfile.getUuid())) { - Data.profile.set(Data.profiles.get(0)); + if (Data.profile.get().equals(mProfile)) { + Log.d("profiles", "[fragment] setting current profile to 0"); + Data.setCurrentProfile(Data.profiles.get(0)); } return false; }); @@ -96,9 +97,8 @@ public class ProfileDetailFragment extends Fragment { // Load the dummy content specified by the fragment // arguments. In a real-world scenario, use a Loader // to load content from a content provider. - String uuid = getArguments().getString(ARG_ITEM_ID); - if (uuid != null) mProfile = - MobileLedgerProfile.loadUUIDFromDB(getArguments().getString(ARG_ITEM_ID)); + int index = getArguments().getInt(ARG_ITEM_ID, -1); + if (index != -1) mProfile = Data.profiles.get(index); Activity activity = this.getActivity(); if (activity == null) throw new AssertionError(); @@ -122,6 +122,8 @@ public class ProfileDetailFragment extends Fragment { mProfile.setAuthUserName(userName.getText()); mProfile.setAuthPassword(password.getText()); mProfile.storeInDB(); + Log.d("profiles", "profile stored in DB"); + Data.profiles.triggerItemChangedNotification(mProfile); if (mProfile.getUuid().equals(Data.profile.get().getUuid())) { diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java index 6d40d858..383148fa 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/ObservableList.java @@ -21,6 +21,7 @@ import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; +import android.util.Log; import java.util.Collection; import java.util.Comparator; @@ -43,9 +44,9 @@ public class ObservableList extends Observable { setChanged(); notifyObservers(); } - private void forceNotify(Object arg) { + private void forceNotify(int index) { setChanged(); - notifyObservers(arg); + notifyObservers(index); } public int size() { return list.size(); @@ -167,11 +168,23 @@ public class ObservableList extends Observable { public void forEach(Consumer action) { list.forEach(action); } + public List getList() { + return list; + } public void setList(List aList) { list = aList; forceNotify(); } - public List getList() { - return list; + public void triggerItemChangedNotification(T item) { + int index = list.indexOf(item); + if (index == -1) { + Log.d("ObList", "??? not sending notifications for item not found in the list"); + return; + } + Log.d("ObList", "Notifying item change observers"); + triggerItemChangedNotification(index); + } + public void triggerItemChangedNotification(int index) { + forceNotify(index); } } \ No newline at end of file -- 2.39.2