]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java
8f57e769174e763abaa2a204776319b08a0acf6e
[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 = SimpleDate.today();
32     private boolean done;
33     public TransactionAccumulator(MainModel model) {
34         this.model = model;
35     }
36     public void put(LedgerTransaction transaction, SimpleDate date) {
37         if (done)
38             throw new IllegalStateException("Can't put new items after done()");
39         if (null == latestDate)
40             latestDate = date;
41         earliestDate = date;
42
43         if (!date.equals(lastDate)) {
44             boolean showMonth = date.month != lastDate.month || date.year != lastDate.year;
45             list.add(new TransactionListItem(date, showMonth));
46         }
47
48         list.add(new TransactionListItem(transaction));
49
50         lastDate = date;
51     }
52     public void done() {
53         done = true;
54         model.setDisplayedTransactions(list);
55         model.setFirstTransactionDate(earliestDate);
56         model.setLastTransactionDate(latestDate);
57     }
58 }