2 * Copyright © 2024 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 androidx.lifecycle.LiveData;
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.ViewModel;
24 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
25 import net.ktnx.mobileledger.async.TransactionAccumulator;
26 import net.ktnx.mobileledger.db.Profile;
27 import net.ktnx.mobileledger.model.Data;
28 import net.ktnx.mobileledger.model.LedgerAccount;
29 import net.ktnx.mobileledger.model.LedgerTransaction;
30 import net.ktnx.mobileledger.model.TransactionListItem;
31 import net.ktnx.mobileledger.utils.Logger;
32 import net.ktnx.mobileledger.utils.SimpleDate;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Locale;
38 public class MainModel extends ViewModel {
39 public final MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
40 private final MutableLiveData<Boolean> updatingFlag = new MutableLiveData<>(false);
41 private final MutableLiveData<Boolean> showZeroBalanceAccounts = new MutableLiveData<>(true);
42 private final MutableLiveData<String> accountFilter = new MutableLiveData<>(null);
43 private final MutableLiveData<List<TransactionListItem>> displayedTransactions =
44 new MutableLiveData<>(new ArrayList<>());
45 private final MutableLiveData<String> updateError = new MutableLiveData<>();
46 private SimpleDate firstTransactionDate;
47 private SimpleDate lastTransactionDate;
48 transient private RetrieveTransactionsTask retrieveTransactionsTask;
49 private TransactionsDisplayedFilter displayedTransactionsUpdater;
50 public LiveData<Boolean> getUpdatingFlag() {
53 public LiveData<String> getUpdateError() {
56 public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
57 return displayedTransactions;
59 public void setDisplayedTransactions(List<TransactionListItem> list, int transactionCount) {
60 displayedTransactions.postValue(list);
61 Data.lastUpdateTransactionCount.postValue(transactionCount);
63 public SimpleDate getFirstTransactionDate() {
64 return firstTransactionDate;
66 public void setFirstTransactionDate(SimpleDate earliestDate) {
67 this.firstTransactionDate = earliestDate;
69 public MutableLiveData<Boolean> getShowZeroBalanceAccounts() {return showZeroBalanceAccounts;}
70 public MutableLiveData<String> getAccountFilter() {
73 public SimpleDate getLastTransactionDate() {
74 return lastTransactionDate;
76 public void setLastTransactionDate(SimpleDate latestDate) {
77 this.lastTransactionDate = latestDate;
79 public synchronized void scheduleTransactionListRetrieval() {
80 if (retrieveTransactionsTask != null) {
81 Logger.debug("db", "Ignoring request for transaction retrieval - already active");
84 Profile profile = Data.getProfile();
85 assert profile != null;
87 retrieveTransactionsTask = new RetrieveTransactionsTask(profile);
88 Logger.debug("db", "Created a background transaction retrieval task");
90 retrieveTransactionsTask.start();
92 public synchronized void stopTransactionsRetrieval() {
93 if (retrieveTransactionsTask != null)
94 retrieveTransactionsTask.interrupt();
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, 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");