]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java
major rework of parsed transaction/descriptions/accounts storage
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / TransactionDateFinder.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 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 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 AsyncTask<TransactionDateFinder.Params, Void, Integer> {
34     private MainModel model;
35     @Override
36     protected void onPostExecute(Integer pos) {
37         model.foundTransactionItemIndex.setValue(pos);
38     }
39     @Override
40     protected Integer doInBackground(Params... param) {
41         this.model = param[0].model;
42         SimpleDate date = param[0].date;
43         Logger.debug("go-to-date",
44                 String.format(Locale.US, "Looking for date %04d-%02d-%02d", date.year, date.month,
45                         date.day));
46         List<TransactionListItem> transactions = Objects.requireNonNull(
47                 param[0].model.getDisplayedTransactions()
48                               .getValue());
49         Logger.debug("go-to-date",
50                 String.format(Locale.US, "List contains %d transactions", transactions.size()));
51
52         TransactionListItem target = new TransactionListItem(date, true);
53         int found =
54                 Collections.binarySearch(transactions, target, new TransactionListItemComparator());
55         if (found >= 0)
56             return found;
57         else
58             return 1 - found;
59     }
60
61     public static class Params {
62         public MainModel model;
63         public SimpleDate date;
64         public Params(MainModel model, SimpleDate date) {
65             this.model = model;
66             this.date = date;
67         }
68     }
69
70     static class TransactionListItemComparator implements Comparator<TransactionListItem> {
71         @Override
72         public int compare(TransactionListItem a, TransactionListItem b) {
73             final SimpleDate aDate = a.getDate();
74             final SimpleDate bDate = b.getDate();
75             int res = aDate.compareTo(bDate);
76             if (res != 0)
77                 return -res;    // transactions are reverse sorted by date
78
79             if (a.getType() == TransactionListItem.Type.DELIMITER) {
80                 if (b.getType() == TransactionListItem.Type.DELIMITER)
81                     return 0;
82                 else
83                     return -1;
84             }
85             else {
86                 if (b.getType() == TransactionListItem.Type.DELIMITER)
87                     return +1;
88                 else
89                     return 0;
90             }
91         }
92     }
93 }