]> git.ktnx.net Git - mobile-ledger.git/commitdiff
store a weak reference to the profile in the account object
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 16 Jul 2020 06:50:32 +0000 (06:50 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 16 Jul 2020 06:50:32 +0000 (06:50 +0000)
to be used for storing account data to the DB

app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java

index 1f4ee97238debc8b1b1be6a1921d93ff0f07579f..db8f8de76a9b7b4d328b4f82b48e636bcebefa60 100644 (file)
@@ -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();
 
index 7510576a81a65fdd31792158514f40c474f8c8ef..3bc01d1fecd852cc957aeb205640465dec80320f 100644 (file)
@@ -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<LedgerAmount> amounts;
     private boolean hasSubAccounts;
     private boolean amountsExpanded;
+    private WeakReference<MobileLedgerProfile> 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<LedgerAmount>();
         this.addAmount(amount);
     }
+    public @Nullable MobileLedgerProfile getProfile() {
+        return profileWeakReference.get();
+    }
     @Override
     public int hashCode() {
         return name.hashCode();
index 65dd11084fbf007bd2dea1ad15c182781f234ff4..21acf677ddc426fac048fb79937d2dbbb49a406e 100644 (file)
@@ -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);