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;
21 import android.text.TextUtils;
23 import androidx.lifecycle.LiveData;
24 import androidx.lifecycle.MutableLiveData;
25 import androidx.lifecycle.ViewModel;
27 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
28 import net.ktnx.mobileledger.async.TransactionAccumulator;
29 import net.ktnx.mobileledger.async.UpdateTransactionsTask;
30 import net.ktnx.mobileledger.db.Profile;
31 import net.ktnx.mobileledger.model.Data;
32 import net.ktnx.mobileledger.model.LedgerAccount;
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.TransactionListItem;
35 import net.ktnx.mobileledger.utils.Logger;
36 import net.ktnx.mobileledger.utils.SimpleDate;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Locale;
42 public class MainModel extends ViewModel {
43 public final MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
44 private final MutableLiveData<Boolean> updatingFlag = new MutableLiveData<>(false);
45 private final MutableLiveData<String> accountFilter = new MutableLiveData<>();
46 private final MutableLiveData<List<TransactionListItem>> displayedTransactions =
47 new MutableLiveData<>(new ArrayList<>());
48 private final MutableLiveData<String> updateError = new MutableLiveData<>();
49 private SimpleDate firstTransactionDate;
50 private SimpleDate lastTransactionDate;
51 transient private RetrieveTransactionsTask retrieveTransactionsTask;
52 transient private Thread displayedAccountsUpdater;
53 private TransactionsDisplayedFilter displayedTransactionsUpdater;
54 public void scheduleTransactionListReload() {
55 UpdateTransactionsTask task = new UpdateTransactionsTask();
56 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, this);
58 public LiveData<Boolean> getUpdatingFlag() {
61 public LiveData<String> getUpdateError() {
64 public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
65 return displayedTransactions;
67 public void setDisplayedTransactions(List<TransactionListItem> list) {
68 displayedTransactions.postValue(list);
69 Data.lastUpdateTransactionCount.postValue(list.size());
71 public SimpleDate getFirstTransactionDate() {
72 return firstTransactionDate;
74 public void setFirstTransactionDate(SimpleDate earliestDate) {
75 this.firstTransactionDate = earliestDate;
77 public MutableLiveData<String> getAccountFilter() {
80 public SimpleDate getLastTransactionDate() {
81 return lastTransactionDate;
83 public void setLastTransactionDate(SimpleDate latestDate) {
84 this.lastTransactionDate = latestDate;
86 private void applyTransactionFilter(List<LedgerTransaction> list) {
87 final String accFilter = accountFilter.getValue();
88 ArrayList<TransactionListItem> newList = new ArrayList<>();
90 TransactionAccumulator accumulator = new TransactionAccumulator(this);
91 if (TextUtils.isEmpty(accFilter))
92 for (LedgerTransaction tr : list)
93 newList.add(new TransactionListItem(tr));
95 for (LedgerTransaction tr : list)
96 if (tr.hasAccountNamedLike(accFilter))
97 newList.add(new TransactionListItem(tr));
99 displayedTransactions.postValue(newList);
101 public synchronized void scheduleTransactionListRetrieval() {
102 if (retrieveTransactionsTask != null) {
103 Logger.debug("db", "Ignoring request for transaction retrieval - already active");
106 Profile profile = Data.getProfile();
108 retrieveTransactionsTask = new RetrieveTransactionsTask(this, profile);
109 Logger.debug("db", "Created a background transaction retrieval task");
111 retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
113 public synchronized void stopTransactionsRetrieval() {
114 if (retrieveTransactionsTask != null)
115 retrieveTransactionsTask.cancel(true);
117 Data.backgroundTaskProgress.setValue(null);
119 public void transactionRetrievalDone() {
120 retrieveTransactionsTask = null;
122 synchronized public void updateDisplayedTransactionsFromWeb(List<LedgerTransaction> list) {
123 if (displayedTransactionsUpdater != null) {
124 displayedTransactionsUpdater.interrupt();
126 displayedTransactionsUpdater = new TransactionsDisplayedFilter(this, list);
127 displayedTransactionsUpdater.start();
129 public void clearUpdateError() {
130 updateError.postValue(null);
132 public void clearTransactions() {
133 displayedTransactions.setValue(new ArrayList<>());
136 static class TransactionsDisplayedFilter extends Thread {
137 private final MainModel model;
138 private final List<LedgerTransaction> list;
139 TransactionsDisplayedFilter(MainModel model, List<LedgerTransaction> list) {
145 List<LedgerAccount> newDisplayed = new ArrayList<>();
146 Logger.debug("dFilter", "waiting for synchronized block");
147 Logger.debug("dFilter", String.format(Locale.US,
148 "entered synchronized block (about to examine %d transactions)", list.size()));
149 String accNameFilter = model.getAccountFilter()
152 TransactionAccumulator acc = new TransactionAccumulator(model);
153 for (LedgerTransaction tr : list) {
154 if (isInterrupted()) {
158 if (accNameFilter == null || tr.hasAccountNamedLike(accNameFilter)) {
159 acc.put(tr, tr.getDate());
162 if (!isInterrupted()) {
165 Logger.debug("dFilter", "left synchronized block");