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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.os.AsyncTask;
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;
30 import java.util.ArrayList;
31 import java.util.List;
33 import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID;
34 import static net.ktnx.mobileledger.utils.Logger.debug;
36 public class UpdateTransactionsTask extends AsyncTask<MainModel, Void, String> {
37 protected String doInBackground(MainModel[] parentModel) {
38 final Profile profile = Data.getProfile();
40 long profileId = (profile == null) ? NO_PROFILE_ID : profile.getId();
41 Data.backgroundTaskStarted();
43 Logger.debug("UTT", "Starting DB transaction list retrieval");
45 final MainModel model = parentModel[0];
46 final String accFilter = model.getAccountFilter()
48 final List<TransactionWithAccounts> transactions;
50 if (profileId == NO_PROFILE_ID)
51 transactions = new ArrayList<>();
52 else if (accFilter == null) {
53 transactions = DB.get()
55 .getAllWithAccountsSync(profileId);
58 transactions = DB.get()
60 .getAllWithAccountsFilteredSync(profileId, accFilter);
63 TransactionAccumulator accumulator = new TransactionAccumulator(model);
65 for (TransactionWithAccounts tr : transactions) {
69 accumulator.put(new LedgerTransaction(tr));
73 debug("UTT", "transaction list value updated");
78 Data.backgroundTaskFinished();