]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
TransactionAccumulator needs not keep a reference to the main model
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.java
index 11de6a915e7de6c25392109f93fe270734674c78..785ec1447eac017e3303a192f2e9eafe7269504a 100644 (file)
@@ -27,15 +27,17 @@ import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.utils.Logger;
 
+import java.util.ArrayList;
 import java.util.List;
 
+import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID;
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
     protected String doInBackground(MainModel[] parentModel) {
         final Profile profile = Data.getProfile();
 
-        long profile_id = profile.getId();
+        long profileId = (profile == null) ? NO_PROFILE_ID : profile.getId();
         Data.backgroundTaskStarted();
         try {
             Logger.debug("UTT", "Starting DB transaction list retrieval");
@@ -45,18 +47,20 @@ public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
                                           .getValue();
             final List<TransactionWithAccounts> transactions;
 
-            if (accFilter == null) {
+            if (profileId == NO_PROFILE_ID)
+                transactions = new ArrayList<>();
+            else if (accFilter == null) {
                 transactions = DB.get()
                                  .getTransactionDAO()
-                                 .getAllWithAccountsSync(profile_id);
+                                 .getAllWithAccountsSync(profileId);
             }
             else {
                 transactions = DB.get()
                                  .getTransactionDAO()
-                                 .getAllWithAccountsFilteredSync(profile_id, accFilter);
+                                 .getAllWithAccountsFilteredSync(profileId, accFilter);
             }
 
-            TransactionAccumulator accumulator = new TransactionAccumulator(model);
+            TransactionAccumulator accumulator = new TransactionAccumulator(accFilter);
 
             for (TransactionWithAccounts tr : transactions) {
                 if (isCancelled())
@@ -65,7 +69,8 @@ public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
                 accumulator.put(new LedgerTransaction(tr));
             }
 
-            accumulator.done();
+            accumulator.publishResults(model);
+
             debug("UTT", "transaction list value updated");
 
             return null;