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