]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
rewrite update of transaction list from DB with Room
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateTransactionsTask.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.os.AsyncTask;
21
22 import net.ktnx.mobileledger.db.DB;
23 import net.ktnx.mobileledger.db.Profile;
24 import net.ktnx.mobileledger.db.TransactionWithAccounts;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerTransaction;
27 import net.ktnx.mobileledger.ui.MainModel;
28 import net.ktnx.mobileledger.utils.Logger;
29
30 import java.util.List;
31
32 import static net.ktnx.mobileledger.utils.Logger.debug;
33
34 public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
35     protected String doInBackground(MainModel[] parentModel) {
36         final Profile profile = Data.getProfile();
37
38         long profile_id = profile.getId();
39         Data.backgroundTaskStarted();
40         try {
41             Logger.debug("UTT", "Starting DB transaction list retrieval");
42
43             final MainModel model = parentModel[0];
44             final String accFilter = model.getAccountFilter()
45                                           .getValue();
46             final List<TransactionWithAccounts> transactions;
47
48             if (accFilter == null) {
49                 transactions = DB.get()
50                                  .getTransactionDAO()
51                                  .getAllWithAccountsSync(profile_id);
52             }
53             else {
54                 transactions = DB.get()
55                                  .getTransactionDAO()
56                                  .getAllWithAccountsFilteredSync(profile_id, accFilter);
57             }
58
59             TransactionAccumulator accumulator = new TransactionAccumulator(model);
60
61             for (TransactionWithAccounts tr : transactions) {
62                 if (isCancelled())
63                     return null;
64
65                 accumulator.put(new LedgerTransaction(tr));
66             }
67
68             accumulator.done();
69             debug("UTT", "transaction list value updated");
70
71             return null;
72         }
73         finally {
74             Data.backgroundTaskFinished();
75         }
76     }
77 }