]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
add ability to hide all-zero accounts from the account list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / MainModel.java
1 /*
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.
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 androidx.lifecycle.LiveData;
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.ViewModel;
23
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;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Locale;
37
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() {
51         return updatingFlag;
52     }
53     public LiveData<String> getUpdateError() {
54         return updateError;
55     }
56     public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
57         return displayedTransactions;
58     }
59     public void setDisplayedTransactions(List<TransactionListItem> list, int transactionCount) {
60         displayedTransactions.postValue(list);
61         Data.lastUpdateTransactionCount.postValue(transactionCount);
62     }
63     public SimpleDate getFirstTransactionDate() {
64         return firstTransactionDate;
65     }
66     public void setFirstTransactionDate(SimpleDate earliestDate) {
67         this.firstTransactionDate = earliestDate;
68     }
69     public MutableLiveData<Boolean> getShowZeroBalanceAccounts() {return showZeroBalanceAccounts;}
70     public MutableLiveData<String> getAccountFilter() {
71         return accountFilter;
72     }
73     public SimpleDate getLastTransactionDate() {
74         return lastTransactionDate;
75     }
76     public void setLastTransactionDate(SimpleDate latestDate) {
77         this.lastTransactionDate = latestDate;
78     }
79     public synchronized void scheduleTransactionListRetrieval() {
80         if (retrieveTransactionsTask != null) {
81             Logger.debug("db", "Ignoring request for transaction retrieval - already active");
82             return;
83         }
84         Profile profile = Data.getProfile();
85         assert profile != null;
86
87         retrieveTransactionsTask = new RetrieveTransactionsTask(profile);
88         Logger.debug("db", "Created a background transaction retrieval task");
89
90         retrieveTransactionsTask.start();
91     }
92     public synchronized void stopTransactionsRetrieval() {
93         if (retrieveTransactionsTask != null)
94             retrieveTransactionsTask.interrupt();
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, 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 }