From e809f9bacb61890374d1b9c8c46edeaed8a838b0 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 19 Jan 2019 18:25:31 +0000 Subject: [PATCH] profiles: ensure single instance per profile, fix havoc on add/removal of a profile havoc == multiple profiles selected, spontaneous change of the current profile --- .../model/MobileLedgerProfile.java | 15 ++++++++--- .../ui/activity/MainActivity.java | 8 +++--- .../ui/activity/ProfileListActivity.java | 25 +++++++++++-------- .../ui/profiles/ProfileDetailFragment.java | 1 + 4 files changed, 31 insertions(+), 18 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 70c12ab9..2b587c8e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -53,18 +53,25 @@ public final class MobileLedgerProfile { this.authUserName = String.valueOf(authUserName); this.authPassword = String.valueOf(authPassword); } - public static List loadAllFromDB() { - List result = new ArrayList<>(); + // loads all profiles into Data.profiles + // returns the profile with the given UUID + public static MobileLedgerProfile loadAllFromDB(String currentProfileUUID) { + MobileLedgerProfile result = null; + List list = new ArrayList<>(); SQLiteDatabase db = MLDB.getReadableDatabase(); try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " + "auth_password FROM profiles order by order_no", null)) { while (cursor.moveToNext()) { - result.add(new MobileLedgerProfile(cursor.getString(0), cursor.getString(1), + MobileLedgerProfile item = + new MobileLedgerProfile(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4), - cursor.getString(5))); + cursor.getString(5)); + list.add(item); + if (item.getUuid().equals(currentProfileUUID)) result = item; } } + Data.profiles.setList(list); return result; } public static void storeProfilesOrder() { 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 90f88685..fbb99989 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 @@ -170,10 +170,9 @@ public class MainActivity extends AppCompatActivity { }); } private void setupProfile() { - Data.profiles.setList(MobileLedgerProfile.loadAllFromDB()); - MobileLedgerProfile profile = null; - String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); + MobileLedgerProfile profile; + if (profileUUID == null) { if (Data.profiles.isEmpty()) { Data.profiles.setList(MobileLedgerProfile.createInitialProfileList()); @@ -196,9 +195,10 @@ public class MainActivity extends AppCompatActivity { editor.clear(); editor.apply(); } + else profile = Data.profiles.get(0); } else { - profile = MobileLedgerProfile.loadUUIDFromDB(profileUUID); + profile = MobileLedgerProfile.loadAllFromDB(profileUUID); } if (profile == null) profile = Data.profiles.get(0); 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 07fce288..6932e978 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 @@ -200,23 +200,28 @@ public class ProfileListActivity extends AppCompatActivity { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.profile_list_content, parent, false); ProfileListViewHolder holder = new ProfileListViewHolder(view); - Data.profile.addObserver((o, arg) -> { - MobileLedgerProfile newProfile = Data.profile.get(); - MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag(); - holder.mRadioView.setChecked(profile.equals(newProfile)); - }); holder.mRadioView.setOnCheckedChangeListener((buttonView, isChecked) -> { if (!isChecked) return; + Log.d("profiles", + String.format("Item %d got checked", holder.getAdapterPosition())); MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag(); - if (profile != null) Data.setCurrentProfile(profile); + if (profile != null) { + Log.d("profiles", + String.format("Setting current profile to %s", profile.getUuid())); + Data.setCurrentProfile(profile); + } }); View.OnClickListener profileSelector = v -> holder.mRadioView.setChecked(true); holder.mTitle.setOnClickListener(profileSelector); holder.mSubTitle.setOnClickListener(profileSelector); Data.profile.addObserver((o, arg) -> { - MobileLedgerProfile profile = (MobileLedgerProfile) holder.itemView.getTag(); - holder.mRadioView.setChecked(Data.profile.get().equals(profile)); + MobileLedgerProfile myProfile = (MobileLedgerProfile) holder.itemView.getTag(); + final MobileLedgerProfile currentProfile = Data.profile.get(); + final boolean sameProfile = currentProfile.equals(myProfile); + if (holder.mRadioView.isChecked() != sameProfile) { + holder.mRadioView.setChecked(sameProfile); + } }); return holder; } @@ -226,11 +231,11 @@ 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())); + holder.itemView.setTag(profile); holder.mTitle.setText(profile.getName()); holder.mSubTitle.setText(profile.getUrl()); - holder.mRadioView.setChecked(profile.getUuid().equals(currentProfile.getUuid())); + holder.mRadioView.setChecked(profile.equals(currentProfile)); - holder.itemView.setTag(profile); holder.mEditButton.setOnClickListener(mOnClickListener); } @Override 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 bf2affd0..b66cc053 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 @@ -127,6 +127,7 @@ public class ProfileDetailFragment extends Fragment { if (mProfile.getUuid().equals(Data.profile.get().getUuid())) { + // dummy update to notify the observers of the possibly new name/URL Data.profile.set(mProfile); } } -- 2.39.5