]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
asking UpdateTransactionsTask to load a null profile is fine
[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.ArrayList;
31 import java.util.List;
32
33 import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID;
34 import static net.ktnx.mobileledger.utils.Logger.debug;
35
36 public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
37     protected String doInBackground(MainModel[] parentModel) {
38         final Profile profile = Data.getProfile();
39
40         long profileId = (profile == null) ? NO_PROFILE_ID : profile.getId();
41         Data.backgroundTaskStarted();
42         try {
43             Logger.debug("UTT", "Starting DB transaction list retrieval");
44
45             final MainModel model = parentModel[0];
46             final String accFilter = model.getAccountFilter()
47                                           .getValue();
48             final List<TransactionWithAccounts> transactions;
49
50             if (profileId == NO_PROFILE_ID)
51                 transactions = new ArrayList<>();
52             else if (accFilter == null) {
53                 transactions = DB.get()
54                                  .getTransactionDAO()
55                                  .getAllWithAccountsSync(profileId);
56             }
57             else {
58                 transactions = DB.get()
59                                  .getTransactionDAO()
60                                  .getAllWithAccountsFilteredSync(profileId, accFilter);
61             }
62
63             TransactionAccumulator accumulator = new TransactionAccumulator(model);
64
65             for (TransactionWithAccounts tr : transactions) {
66                 if (isCancelled())
67                     return null;
68
69                 accumulator.put(new LedgerTransaction(tr));
70             }
71
72             accumulator.done();
73             debug("UTT", "transaction list value updated");
74
75             return null;
76         }
77         finally {
78             Data.backgroundTaskFinished();
79         }
80     }
81 }