]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fix merging of account lists so that changes are detected
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 16 Aug 2020 13:56:30 +0000 (16:56 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 16 Aug 2020 13:57:47 +0000 (13:57 +0000)
the old code modified objects in-place, changing the existing object,
which avoids (wrongly) propagation of the change to the UI

the new code ensures a new object is used in that case (the one from
newList) which guarantees that the change will be caught by the list
differ

this fixes a problem with amounts not being updated after a refresh from
the hledger-web backend

app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java

index 6b847ec103c1cb91aa5a2ea34e0a155632dae751..7ae6b5a91b8d96a9c196fc52e362bee2d7e7e6bf 100644 (file)
@@ -214,10 +214,12 @@ public final class MobileLedgerProfile {
                 continue;
             }
 
-            // two items with same account names; merge UI-controlled fields
-            oldAcc.setExpanded(newAcc.isExpanded());
-            oldAcc.setAmountsExpanded(newAcc.amountsExpanded());
-            merged.add(oldAcc);
+            // two items with same account names; forward-merge UI-controlled fields
+            // it is important that the result list contains a new LedgerAccount instance
+            // so that the change is propagated to the UI
+            newAcc.setExpanded(oldAcc.isExpanded());
+            newAcc.setAmountsExpanded(oldAcc.amountsExpanded());
+            merged.add(newAcc);
         }
 
         return merged;
index 5ffc62f544389cee71151b65aeb1dbf7a3e65c57..2d7ea6ed6e3a8404749a781077b49d1ae4fd19de 100644 (file)
@@ -39,10 +39,12 @@ import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.Locker;
+import net.ktnx.mobileledger.utils.Logger;
 
 import org.jetbrains.annotations.NotNull;
 
 import java.util.List;
+import java.util.Locale;
 
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
@@ -187,6 +189,7 @@ public class AccountSummaryAdapter
             return true;
         }
         public void bindToAccount(LedgerAccount acc) {
+            Logger.debug("accounts", String.format(Locale.US, "Binding to '%s'", acc.getName()));
             Context ctx = row.getContext();
             Resources rm = ctx.getResources();
             mAccount = acc;
index 8416c1015097a6225e4456f0e0903b3f46b830c1..edbd5a7407123ee07aab9326cef0e96eaf33dd63 100644 (file)
@@ -37,14 +37,16 @@ public class MobileLedgerProfileTest {
     }
     private void aTest(LedgerAccount[] oldList, LedgerAccount[] newList,
                        LedgerAccount[] expectedResult) {
-        List<LedgerAccount> result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList),
-                listFromArray(newList));
+        List<LedgerAccount> result =
+                MobileLedgerProfile.mergeAccountListsFromWeb(listFromArray(oldList),
+                        listFromArray(newList));
         assertArrayEquals(expectedResult, result.toArray());
     }
     private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList,
                          LedgerAccount[] expectedResult) {
-        List<LedgerAccount> result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList),
-                listFromArray(newList));
+        List<LedgerAccount> result =
+                MobileLedgerProfile.mergeAccountListsFromWeb(listFromArray(oldList),
+                        listFromArray(newList));
         assertThrows(ArrayComparisonFailure.class,
                 () -> assertArrayEquals(expectedResult, result.toArray()));
     }
@@ -76,9 +78,9 @@ public class MobileLedgerProfileTest {
         LedgerAccount acc1b = new LedgerAccount(null, "Acc1", null);
         acc1b.setExpanded(true);
         acc1b.setAmountsExpanded(true);
-        List<LedgerAccount> merged =
-                MobileLedgerProfile.mergeAccountLists(listFromArray(new LedgerAccount[]{acc1a}),
-                        listFromArray(new LedgerAccount[]{acc1b}));
+        List<LedgerAccount> merged = MobileLedgerProfile.mergeAccountListsFromWeb(
+                listFromArray(new LedgerAccount[]{acc1a}),
+                listFromArray(new LedgerAccount[]{acc1b}));
         assertArrayEquals(new LedgerAccount[]{acc1b}, merged.toArray());
         assertSame(merged.get(0), acc1a);
         // restore original values, modified by the merge