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