]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java
final fix for the first date delimiter item
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / async / TransactionAccumulator.java
1 /*
2  * Copyright © 2020 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 MainModel model;
30     private SimpleDate earliestDate, latestDate;
31     private SimpleDate lastDate;
32     private boolean done;
33     private int transactionCount = 0;
34     public TransactionAccumulator(MainModel model) {
35         this.model = model;
36
37         list.add(new TransactionListItem());    // head item
38     }
39     public void put(LedgerTransaction transaction, SimpleDate date) {
40         if (done)
41             throw new IllegalStateException("Can't put new items after done()");
42
43         // first item
44         if (null == latestDate)
45             latestDate = date;
46         earliestDate = date;
47
48         if (!date.equals(lastDate)) {
49             if (lastDate == null)
50                 lastDate = SimpleDate.today();
51             boolean showMonth = date.month != lastDate.month || date.year != lastDate.year;
52             list.add(new TransactionListItem(date, showMonth));
53         }
54
55         list.add(new TransactionListItem(transaction));
56
57         lastDate = date;
58         transactionCount++;
59     }
60     public void done() {
61         done = true;
62         model.setDisplayedTransactions(list, transactionCount);
63         model.setFirstTransactionDate(earliestDate);
64         model.setLastTransactionDate(latestDate);
65     }
66 }