X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FTransactionDateFinder.java;h=18cea62fa2b3f415f49598d1054bda02106d5d87;hb=HEAD;hp=c15e59906e9932d4ca9453d0aecf5aefd3f1b97b;hpb=2c14b80572cc9199f7ed0171786a04931075b50d;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java b/app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java index c15e5990..18cea62f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/TransactionDateFinder.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -17,61 +17,70 @@ package net.ktnx.mobileledger.async; -import android.os.AsyncTask; - -import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.TransactionListItem; -import net.ktnx.mobileledger.utils.LockHolder; +import net.ktnx.mobileledger.ui.MainModel; import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.SimpleDate; +import org.jetbrains.annotations.NotNull; + import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Locale; +import java.util.Objects; -public class TransactionDateFinder extends AsyncTask { - @Override - protected void onPostExecute(Integer pos) { - Data.foundTransactionItemIndex.setValue(pos); +public class TransactionDateFinder extends Thread { + private final MainModel model; + private final SimpleDate date; + public TransactionDateFinder(MainModel model, SimpleDate date) { + this.model = model; + this.date = date; } @Override - protected Integer doInBackground(SimpleDate... simpleDates) { - SimpleDate date = simpleDates[0]; + public void run() { Logger.debug("go-to-date", String.format(Locale.US, "Looking for date %04d-%02d-%02d", date.year, date.month, date.day)); - Logger.debug("go-to-date", String.format(Locale.US, "List contains %d transactions", - Data.transactions.size())); + List transactions = Objects.requireNonNull( + model.getDisplayedTransactions() + .getValue()); + final int transactionCount = transactions.size(); + Logger.debug("go-to-date", + String.format(Locale.US, "List contains %d transactions", transactionCount)); - try (LockHolder locker = Data.transactions.lockForWriting()) { - List transactions = Data.transactions.getList(); - TransactionListItem target = new TransactionListItem(date, true); - int found = Collections.binarySearch(transactions, target, - new TransactionListItemComparator()); - if (found >= 0) - return found; - else - return 1 - found; - } + TransactionListItem target = new TransactionListItem(date, true); + int found = + Collections.binarySearch(transactions, target, new TransactionListItemComparator()); + if (found < 0) + found = -1 - found; + + model.foundTransactionItemIndex.postValue(found); } + static class TransactionListItemComparator implements Comparator { @Override - public int compare(TransactionListItem a, TransactionListItem b) { + public int compare(@NotNull TransactionListItem a, @NotNull TransactionListItem b) { + final TransactionListItem.Type aType = a.getType(); + if (aType == TransactionListItem.Type.HEADER) + return +1; + final TransactionListItem.Type bType = b.getType(); + if (bType == TransactionListItem.Type.HEADER) + return -1; final SimpleDate aDate = a.getDate(); final SimpleDate bDate = b.getDate(); int res = aDate.compareTo(bDate); if (res != 0) return -res; // transactions are reverse sorted by date - if (a.getType() == TransactionListItem.Type.DELIMITER) { - if (b.getType() == TransactionListItem.Type.DELIMITER) + if (aType == TransactionListItem.Type.DELIMITER) { + if (bType == TransactionListItem.Type.DELIMITER) return 0; else return -1; } else { - if (b.getType() == TransactionListItem.Type.DELIMITER) + if (bType == TransactionListItem.Type.DELIMITER) return +1; else return 0;