]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
4247e39fbd4f242b9726c80ec41f41b0c31946ee
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / MainModel.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.ui;
19
20 import android.os.AsyncTask;
21
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.ViewModel;
25
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;
35
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Locale;
39
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() {
53         return updatingFlag;
54     }
55     public LiveData<String> getUpdateError() {
56         return updateError;
57     }
58     public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
59         return displayedTransactions;
60     }
61     public void setDisplayedTransactions(List<TransactionListItem> list) {
62         displayedTransactions.postValue(list);
63         Data.lastUpdateTransactionCount.postValue(list.size());
64     }
65     public SimpleDate getFirstTransactionDate() {
66         return firstTransactionDate;
67     }
68     public void setFirstTransactionDate(SimpleDate earliestDate) {
69         this.firstTransactionDate = earliestDate;
70     }
71     public MutableLiveData<String> getAccountFilter() {
72         return accountFilter;
73     }
74     public SimpleDate getLastTransactionDate() {
75         return lastTransactionDate;
76     }
77     public void setLastTransactionDate(SimpleDate latestDate) {
78         this.lastTransactionDate = latestDate;
79     }
80     public synchronized void scheduleTransactionListRetrieval() {
81         if (retrieveTransactionsTask != null) {
82             Logger.debug("db", "Ignoring request for transaction retrieval - already active");
83             return;
84         }
85         Profile profile = Data.getProfile();
86
87         retrieveTransactionsTask = new RetrieveTransactionsTask(this, profile);
88         Logger.debug("db", "Created a background transaction retrieval task");
89
90         retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
91     }
92     public synchronized void stopTransactionsRetrieval() {
93         if (retrieveTransactionsTask != null)
94             retrieveTransactionsTask.cancel(true);
95         else
96             Data.backgroundTaskProgress.setValue(null);
97     }
98     public void transactionRetrievalDone() {
99         retrieveTransactionsTask = null;
100     }
101     synchronized public void updateDisplayedTransactionsFromWeb(List<LedgerTransaction> list) {
102         if (displayedTransactionsUpdater != null) {
103             displayedTransactionsUpdater.interrupt();
104         }
105         displayedTransactionsUpdater = new TransactionsDisplayedFilter(this, list);
106         displayedTransactionsUpdater.start();
107     }
108     public void clearUpdateError() {
109         updateError.postValue(null);
110     }
111     public void clearTransactions() {
112         displayedTransactions.setValue(new ArrayList<>());
113     }
114
115     static class TransactionsDisplayedFilter extends Thread {
116         private final MainModel model;
117         private final List<LedgerTransaction> list;
118         TransactionsDisplayedFilter(MainModel model, List<LedgerTransaction> list) {
119             this.model = model;
120             this.list = list;
121         }
122         @Override
123         public void run() {
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()
128                                         .getValue();
129
130             TransactionAccumulator acc = new TransactionAccumulator(accNameFilter);
131             for (LedgerTransaction tr : list) {
132                 if (isInterrupted()) {
133                     return;
134                 }
135
136                 if (accNameFilter == null || tr.hasAccountNamedLike(accNameFilter)) {
137                     acc.put(tr, tr.getDate());
138                 }
139             }
140
141             if (isInterrupted())
142                 return;
143
144             acc.publishResults(model);
145             Logger.debug("dFilter", "transaction list updated");
146         }
147     }
148 }