]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java
speculatively update last update date before the transactions are stored
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / TransactionAccumulator.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.async;
19
20 import net.ktnx.mobileledger.model.Data;
21 import net.ktnx.mobileledger.model.LedgerTransaction;
22 import net.ktnx.mobileledger.model.TransactionListItem;
23 import net.ktnx.mobileledger.ui.MainModel;
24 import net.ktnx.mobileledger.utils.SimpleDate;
25
26 import java.util.ArrayList;
27 import java.util.Date;
28
29 public class TransactionAccumulator {
30     private final ArrayList<TransactionListItem> list = new ArrayList<>();
31     private final MainModel model;
32     private final String boldAccountName;
33     private SimpleDate earliestDate, latestDate;
34     private SimpleDate lastDate;
35     private boolean done;
36     public TransactionAccumulator(MainModel model) {
37         this.model = model;
38
39         boldAccountName = model.getAccountFilter()
40                                .getValue();
41
42         list.add(new TransactionListItem());    // head item
43     }
44     public void put(LedgerTransaction transaction) {
45         put(transaction, transaction.getDate());
46     }
47     public void put(LedgerTransaction transaction, SimpleDate date) {
48         if (done)
49             throw new IllegalStateException("Can't put new items after done()");
50
51         // first item
52         if (null == latestDate)
53             latestDate = date;
54         earliestDate = date;
55
56         if (!date.equals(lastDate)) {
57             if (lastDate == null)
58                 lastDate = SimpleDate.today();
59             boolean showMonth = date.month != lastDate.month || date.year != lastDate.year;
60             list.add(new TransactionListItem(date, showMonth));
61         }
62
63         list.add(new TransactionListItem(transaction, boldAccountName));
64
65         lastDate = date;
66     }
67     public void done() {
68         done = true;
69         model.setDisplayedTransactions(list);
70         Data.lastUpdateDate.postValue(new Date());
71         model.setFirstTransactionDate(earliestDate);
72         model.setLastTransactionDate(latestDate);
73     }
74 }