From: Damyan Ivanov Date: Sun, 16 Aug 2020 13:56:30 +0000 (+0300) Subject: fix merging of account lists so that changes are detected X-Git-Tag: v0.15.0~42 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=1aac3d8310057f17400cd194ffcd9352f3021c11;p=mobile-ledger.git fix merging of account lists so that changes are detected 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 --- 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 6b847ec1..7ae6b5a9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -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; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java index 5ffc62f5..2d7ea6ed 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java @@ -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; diff --git a/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java b/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java index 8416c101..edbd5a74 100644 --- a/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java +++ b/app/src/test/java/net/ktnx/mobileledger/model/MobileLedgerProfileTest.java @@ -37,14 +37,16 @@ public class MobileLedgerProfileTest { } private void aTest(LedgerAccount[] oldList, LedgerAccount[] newList, LedgerAccount[] expectedResult) { - List result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList), - listFromArray(newList)); + List result = + MobileLedgerProfile.mergeAccountListsFromWeb(listFromArray(oldList), + listFromArray(newList)); assertArrayEquals(expectedResult, result.toArray()); } private void negTest(LedgerAccount[] oldList, LedgerAccount[] newList, LedgerAccount[] expectedResult) { - List result = MobileLedgerProfile.mergeAccountLists(listFromArray(oldList), - listFromArray(newList)); + List 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 merged = - MobileLedgerProfile.mergeAccountLists(listFromArray(new LedgerAccount[]{acc1a}), - listFromArray(new LedgerAccount[]{acc1b})); + List 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