]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / TransactionDateFinder.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.TransactionListItem;
21 import net.ktnx.mobileledger.ui.MainModel;
22 import net.ktnx.mobileledger.utils.Logger;
23 import net.ktnx.mobileledger.utils.SimpleDate;
24
25 import org.jetbrains.annotations.NotNull;
26
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Objects;
32
33 public class TransactionDateFinder extends Thread {
34     private final MainModel model;
35     private final SimpleDate date;
36     public TransactionDateFinder(MainModel model, SimpleDate date) {
37         this.model = model;
38         this.date = date;
39     }
40     @Override
41     public void run() {
42         Logger.debug("go-to-date",
43                 String.format(Locale.US, "Looking for date %04d-%02d-%02d", date.year, date.month,
44                         date.day));
45         List<TransactionListItem> transactions = Objects.requireNonNull(
46                 model.getDisplayedTransactions()
47                      .getValue());
48         final int transactionCount = transactions.size();
49         Logger.debug("go-to-date",
50                 String.format(Locale.US, "List contains %d transactions", transactionCount));
51
52         TransactionListItem target = new TransactionListItem(date, true);
53         int found =
54                 Collections.binarySearch(transactions, target, new TransactionListItemComparator());
55         if (found < 0)
56             found = -1 - found;
57
58         model.foundTransactionItemIndex.postValue(found);
59     }
60
61     static class TransactionListItemComparator implements Comparator<TransactionListItem> {
62         @Override
63         public int compare(@NotNull TransactionListItem a, @NotNull TransactionListItem b) {
64             final TransactionListItem.Type aType = a.getType();
65             if (aType == TransactionListItem.Type.HEADER)
66                 return +1;
67             final TransactionListItem.Type bType = b.getType();
68             if (bType == TransactionListItem.Type.HEADER)
69                 return -1;
70             final SimpleDate aDate = a.getDate();
71             final SimpleDate bDate = b.getDate();
72             int res = aDate.compareTo(bDate);
73             if (res != 0)
74                 return -res;    // transactions are reverse sorted by date
75
76             if (aType == TransactionListItem.Type.DELIMITER) {
77                 if (bType == TransactionListItem.Type.DELIMITER)
78                     return 0;
79                 else
80                     return -1;
81             }
82             else {
83                 if (bType == TransactionListItem.Type.DELIMITER)
84                     return +1;
85                 else
86                     return 0;
87             }
88         }
89     }
90 }