]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fully employ room for loading transactions off DB
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 23 Apr 2021 21:36:04 +0000 (00:36 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 25 Apr 2021 17:07:58 +0000 (17:07 +0000)
app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java [deleted file]
app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java

index a9517e1ce13e2c6eafb1cc23406cce416f57dda1..7470f0eaf3970bdf1f4b380d86b1380d0c1d490f 100644 (file)
@@ -570,8 +570,6 @@ public class RetrieveTransactionsTask extends
                 retrieveTransactionListLegacy(accounts, transactions);
             }
 
-            mainModel.updateDisplayedTransactionsFromWeb(transactions);
-
             new AccountAndTransactionListSaver(accounts, transactions).start();
 
             Data.lastUpdateDate.postValue(new Date());
diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
deleted file mode 100644 (file)
index 785ec14..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright © 2021 Damyan Ivanov.
- * This file is part of MoLe.
- * MoLe is free software: you can distribute it and/or modify it
- * under the term of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your opinion), any later version.
- *
- * MoLe is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License terms for details.
- *
- * You should have received a copy of the GNU General Public License
- * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
- */
-
-package net.ktnx.mobileledger.async;
-
-import android.os.AsyncTask;
-
-import net.ktnx.mobileledger.db.DB;
-import net.ktnx.mobileledger.db.Profile;
-import net.ktnx.mobileledger.db.TransactionWithAccounts;
-import net.ktnx.mobileledger.model.Data;
-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 profileId = (profile == null) ? NO_PROFILE_ID : profile.getId();
-        Data.backgroundTaskStarted();
-        try {
-            Logger.debug("UTT", "Starting DB transaction list retrieval");
-
-            final MainModel model = parentModel[0];
-            final String accFilter = model.getAccountFilter()
-                                          .getValue();
-            final List<TransactionWithAccounts> transactions;
-
-            if (profileId == NO_PROFILE_ID)
-                transactions = new ArrayList<>();
-            else if (accFilter == null) {
-                transactions = DB.get()
-                                 .getTransactionDAO()
-                                 .getAllWithAccountsSync(profileId);
-            }
-            else {
-                transactions = DB.get()
-                                 .getTransactionDAO()
-                                 .getAllWithAccountsFilteredSync(profileId, accFilter);
-            }
-
-            TransactionAccumulator accumulator = new TransactionAccumulator(accFilter);
-
-            for (TransactionWithAccounts tr : transactions) {
-                if (isCancelled())
-                    return null;
-
-                accumulator.put(new LedgerTransaction(tr));
-            }
-
-            accumulator.publishResults(model);
-
-            debug("UTT", "transaction list value updated");
-
-            return null;
-        }
-        finally {
-            Data.backgroundTaskFinished();
-        }
-    }
-}
index 5af78ea9ac1198c799695473fa3e7551eb5f22bb..2c0995e1226cef8480babebff7ac8f3623008721 100644 (file)
@@ -106,6 +106,10 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
     public abstract List<TransactionWithAccounts> getAllWithAccountsSync(long profileId);
 
+    @androidx.room.Transaction
+    @Query("SELECT * FROM transactions WHERE profile_id = :profileId")
+    public abstract LiveData<List<TransactionWithAccounts>> getAllWithAccounts(long profileId);
+
     @androidx.room.Transaction
     @Query("SELECT distinct(tr.id), tr.ledger_id, tr.profile_id, tr.data_hash, tr.year, tr.month," +
            " tr.day, tr.description, tr.comment, tr.generation FROM transactions tr JOIN " +
index 2355d2a833c2f5192d64887fac87162f39bc9bf2..4247e39fbd4f242b9726c80ec41f41b0c31946ee 100644 (file)
@@ -25,7 +25,6 @@ import androidx.lifecycle.ViewModel;
 
 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
 import net.ktnx.mobileledger.async.TransactionAccumulator;
-import net.ktnx.mobileledger.async.UpdateTransactionsTask;
 import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
@@ -50,10 +49,6 @@ public class MainModel extends ViewModel {
     transient private RetrieveTransactionsTask retrieveTransactionsTask;
     transient private Thread displayedAccountsUpdater;
     private TransactionsDisplayedFilter displayedTransactionsUpdater;
-    public void scheduleTransactionListReload() {
-        UpdateTransactionsTask task = new UpdateTransactionsTask();
-        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this);
-    }
     public LiveData<Boolean> getUpdatingFlag() {
         return updatingFlag;
     }
index 46cc410e18d401eb1da86bdcb21c950d79081dff..406d0d7364da6f4c4c083529be6357dc72b0b9b4 100644 (file)
@@ -41,6 +41,8 @@ import androidx.core.view.GravityCompat;
 import androidx.drawerlayout.widget.DrawerLayout;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
+import androidx.lifecycle.LiveData;
+import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
@@ -51,11 +53,14 @@ import com.google.android.material.snackbar.Snackbar;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
+import net.ktnx.mobileledger.async.TransactionAccumulator;
 import net.ktnx.mobileledger.databinding.ActivityMainBinding;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Option;
 import net.ktnx.mobileledger.db.Profile;
+import net.ktnx.mobileledger.db.TransactionWithAccounts;
 import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.ui.FabManager;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
@@ -457,9 +462,33 @@ public class MainActivity extends ProfileThemedActivity implements FabManager.Fa
         }
 
         mainModel.getAccountFilter()
-                 .observe(this, v -> {
+                 .observe(this, accFilter -> {
                      Logger.debug(TAG, "account filter changed, reloading transactions");
-                     mainModel.scheduleTransactionListReload();
+//                     mainModel.scheduleTransactionListReload();
+                     LiveData<List<TransactionWithAccounts>> transactions =
+                             new MutableLiveData<>(new ArrayList<TransactionWithAccounts>());
+                     if (profile != null) {
+                         if (accFilter == null || accFilter.isEmpty()) {
+                             transactions = DB.get()
+                                              .getTransactionDAO()
+                                              .getAllWithAccounts(profile.getId());
+                         }
+                         else {
+                             transactions = DB.get()
+                                              .getTransactionDAO()
+                                              .getAllWithAccountsFiltered(profile.getId(),
+                                                      accFilter);
+                         }
+                     }
+
+                     transactions.observe(this, list -> {
+                         TransactionAccumulator accumulator = new TransactionAccumulator(accFilter);
+
+                         for (TransactionWithAccounts tr : list)
+                             accumulator.put(new LedgerTransaction(tr));
+
+                         accumulator.publishResults(mainModel);
+                     });
                  });
 
         mainModel.stopTransactionsRetrieval();