From: Damyan Ivanov Date: Thu, 16 Jul 2020 06:50:32 +0000 (+0000) Subject: store a weak reference to the profile in the account object X-Git-Tag: v0.15.0~90 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=d58f8f4a9edd8b96005d1900c51b589471424165;p=mobile-ledger.git store a weak reference to the profile in the account object to be used for storing account data to the DB --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 1f4ee972..db8f8de7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -220,7 +220,7 @@ public class RetrieveTransactionsTask prevAccount = lastAccount; lastAccount = profile.tryLoadAccount(db, acct_name); if (lastAccount == null) - lastAccount = new LedgerAccount(acct_name); + lastAccount = new LedgerAccount(profile, acct_name); else lastAccount.removeAmounts(); profile.storeAccount(db, lastAccount); @@ -240,14 +240,14 @@ public class RetrieveTransactionsTask if (accountNames.containsKey(parentName)) break; toAppend.push(parentName); - parentName = new LedgerAccount(parentName).getParentName(); + parentName = new LedgerAccount(profile, parentName).getParentName(); } syntheticAccounts.clear(); while (!toAppend.isEmpty()) { String aName = toAppend.pop(); LedgerAccount acc = profile.tryLoadAccount(db, aName); if (acc == null) { - acc = new LedgerAccount(aName); + acc = new LedgerAccount(profile, aName); acc.setExpanded(!lastAccount.hasSubAccounts() || lastAccount.isExpanded()); } @@ -483,7 +483,7 @@ public class RetrieveTransactionsTask LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); if (acc == null) - acc = new LedgerAccount(parsedAccount.getAname()); + acc = new LedgerAccount(profile, parsedAccount.getAname()); else acc.removeAmounts(); diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java index 7510576a..3bc01d1f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java @@ -20,6 +20,7 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -36,17 +37,23 @@ public class LedgerAccount { private List amounts; private boolean hasSubAccounts; private boolean amountsExpanded; + private WeakReference profileWeakReference; - public LedgerAccount(String name) { + public LedgerAccount(MobileLedgerProfile profile, String name) { + this.profileWeakReference = new WeakReference<>(profile); this.setName(name); } - public LedgerAccount(String name, float amount) { + public LedgerAccount(MobileLedgerProfile profile, String name, float amount) { + this.profileWeakReference = new WeakReference<>(profile); this.setName(name); this.expanded = true; this.amounts = new ArrayList(); this.addAmount(amount); } + public @Nullable MobileLedgerProfile getProfile() { + return profileWeakReference.get(); + } @Override public int hashCode() { return name.hashCode(); 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 65dd1108..21acf677 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -409,7 +409,7 @@ public final class MobileLedgerProfile { new String[]{uuid, accName})) { if (cursor.moveToFirst()) { - LedgerAccount acc = new LedgerAccount(accName); + LedgerAccount acc = new LedgerAccount(this, accName); acc.setExpanded(cursor.getInt(0) == 1); acc.setHasSubAccounts(cursor.getInt(1) == 1);