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.ui;
20 import android.os.AsyncTask;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.ViewModel;
26 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
27 import net.ktnx.mobileledger.async.TransactionAccumulator;
28 import net.ktnx.mobileledger.db.Profile;
29 import net.ktnx.mobileledger.model.Data;
30 import net.ktnx.mobileledger.model.LedgerAccount;
31 import net.ktnx.mobileledger.model.LedgerTransaction;
32 import net.ktnx.mobileledger.model.TransactionListItem;
33 import net.ktnx.mobileledger.utils.Logger;
34 import net.ktnx.mobileledger.utils.SimpleDate;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
40 public class MainModel extends ViewModel {
41 public final MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
42 private final MutableLiveData<Boolean> updatingFlag = new MutableLiveData<>(false);
43 private final MutableLiveData<String> accountFilter = new MutableLiveData<>(null);
44 private final MutableLiveData<List<TransactionListItem>> displayedTransactions =
45 new MutableLiveData<>(new ArrayList<>());
46 private final MutableLiveData<String> updateError = new MutableLiveData<>();
47 private SimpleDate firstTransactionDate;
48 private SimpleDate lastTransactionDate;
49 transient private RetrieveTransactionsTask retrieveTransactionsTask;
50 transient private Thread displayedAccountsUpdater;
51 private TransactionsDisplayedFilter displayedTransactionsUpdater;
52 public LiveData<Boolean> getUpdatingFlag() {
55 public LiveData<String> getUpdateError() {
58 public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
59 return displayedTransactions;
61 public void setDisplayedTransactions(List<TransactionListItem> list) {
62 displayedTransactions.postValue(list);
63 Data.lastUpdateTransactionCount.postValue(list.size());
65 public SimpleDate getFirstTransactionDate() {
66 return firstTransactionDate;
68 public void setFirstTransactionDate(SimpleDate earliestDate) {
69 this.firstTransactionDate = earliestDate;
71 public MutableLiveData<String> getAccountFilter() {
74 public SimpleDate getLastTransactionDate() {
75 return lastTransactionDate;
77 public void setLastTransactionDate(SimpleDate latestDate) {
78 this.lastTransactionDate = latestDate;
80 public synchronized void scheduleTransactionListRetrieval() {
81 if (retrieveTransactionsTask != null) {
82 Logger.debug("db", "Ignoring request for transaction retrieval - already active");
85 Profile profile = Data.getProfile();
87 retrieveTransactionsTask = new RetrieveTransactionsTask(this, profile);
88 Logger.debug("db", "Created a background transaction retrieval task");
90 retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
92 public synchronized void stopTransactionsRetrieval() {
93 if (retrieveTransactionsTask != null)
94 retrieveTransactionsTask.cancel(true);
96 Data.backgroundTaskProgress.setValue(null);
98 public void transactionRetrievalDone() {
99 retrieveTransactionsTask = null;
101 synchronized public void updateDisplayedTransactionsFromWeb(List<LedgerTransaction> list) {
102 if (displayedTransactionsUpdater != null) {
103 displayedTransactionsUpdater.interrupt();
105 displayedTransactionsUpdater = new TransactionsDisplayedFilter(this, list);
106 displayedTransactionsUpdater.start();
108 public void clearUpdateError() {
109 updateError.postValue(null);
111 public void clearTransactions() {
112 displayedTransactions.setValue(new ArrayList<>());
115 static class TransactionsDisplayedFilter extends Thread {
116 private final MainModel model;
117 private final List<LedgerTransaction> list;
118 TransactionsDisplayedFilter(MainModel model, List<LedgerTransaction> list) {
124 List<LedgerAccount> newDisplayed = new ArrayList<>();
125 Logger.debug("dFilter", String.format(Locale.US,
126 "entered synchronized block (about to examine %d transactions)", list.size()));
127 String accNameFilter = model.getAccountFilter()
130 TransactionAccumulator acc = new TransactionAccumulator(accNameFilter);
131 for (LedgerTransaction tr : list) {
132 if (isInterrupted()) {
136 if (accNameFilter == null || tr.hasAccountNamedLike(accNameFilter)) {
137 acc.put(tr, tr.getDate());
144 acc.publishResults(model);
145 Logger.debug("dFilter", "transaction list updated");