]> git.ktnx.net Git - mobile-ledger.git/commitdiff
profiles: ensure single instance per profile, fix havoc on add/removal of a profile
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 19 Jan 2019 18:25:31 +0000 (18:25 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 19 Jan 2019 18:25:31 +0000 (18:25 +0000)
havoc == multiple profiles selected, spontaneous change of the current
profile

app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java

index 70c12ab90f6fd7e1a447727cc65c0cc9709278f4..2b587c8ed4ad6d01331577122702e2f8c8ef6eec 100644 (file)
@@ -53,18 +53,25 @@ public final class MobileLedgerProfile {
         this.authUserName = String.valueOf(authUserName);
         this.authPassword = String.valueOf(authPassword);
     }
-    public static List<MobileLedgerProfile> loadAllFromDB() {
-        List<MobileLedgerProfile> 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<MobileLedgerProfile> 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() {
index 90f88685c5a3696ef090aa578a375f110318af0f..fbb99989ebd6de9b91c3528d3388ad5a15e51497 100644 (file)
@@ -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);
index 07fce288bdcc6d51ec09ba92b640150ea9d273d0..6932e97828d84f1d4b06049b259cf0ba332e6287 100644 (file)
@@ -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
index bf2affd0c77ca73d2fcf2ec22af85cd7c027777c..b66cc053a6b51823f4425df257050205b6845376 100644 (file)
@@ -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);
                 }
             }