]> git.ktnx.net Git - mobile-ledger.git/blobdiff - 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
index 608b477198af04f99c6a7bf49cc05b6f310892d7..18cea62fa2b3f415f49598d1054bda02106d5d87 100644 (file)
@@ -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,8 +17,6 @@
 
 package net.ktnx.mobileledger.async;
 
-import android.os.AsyncTask;
-
 import net.ktnx.mobileledger.model.TransactionListItem;
 import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.utils.Logger;
@@ -32,49 +30,42 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Objects;
 
-public class TransactionDateFinder extends AsyncTask<TransactionDateFinder.Params, Void, Integer> {
-    private MainModel model;
-    @Override
-    protected void onPostExecute(Integer pos) {
-        model.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(Params... param) {
-        this.model = param[0].model;
-        SimpleDate date = param[0].date;
+    public void run() {
         Logger.debug("go-to-date",
                 String.format(Locale.US, "Looking for date %04d-%02d-%02d", date.year, date.month,
                         date.day));
         List<TransactionListItem> transactions = Objects.requireNonNull(
-                param[0].model.getDisplayedTransactions()
-                              .getValue());
+                model.getDisplayedTransactions()
+                     .getValue());
+        final int transactionCount = transactions.size();
         Logger.debug("go-to-date",
-                String.format(Locale.US, "List contains %d transactions", transactions.size()));
+                String.format(Locale.US, "List contains %d transactions", transactionCount));
 
         TransactionListItem target = new TransactionListItem(date, true);
         int found =
                 Collections.binarySearch(transactions, target, new TransactionListItemComparator());
-        if (found >= 0)
-            return found;
-        else
-            return 1 - found;
-    }
+        if (found < 0)
+            found = -1 - found;
 
-    public static class Params {
-        public final MainModel model;
-        public final SimpleDate date;
-        public Params(@NotNull MainModel model, @NotNull SimpleDate date) {
-            this.model = model;
-            this.date = date;
-        }
+        model.foundTransactionItemIndex.postValue(found);
     }
 
     static class TransactionListItemComparator implements Comparator<TransactionListItem> {
         @Override
         public int compare(@NotNull TransactionListItem a, @NotNull TransactionListItem b) {
-            if (a.getType() == TransactionListItem.Type.HEADER)
+            final TransactionListItem.Type aType = a.getType();
+            if (aType == TransactionListItem.Type.HEADER)
                 return +1;
-            if (b.getType() == TransactionListItem.Type.HEADER)
+            final TransactionListItem.Type bType = b.getType();
+            if (bType == TransactionListItem.Type.HEADER)
                 return -1;
             final SimpleDate aDate = a.getDate();
             final SimpleDate bDate = b.getDate();
@@ -82,14 +73,14 @@ public class TransactionDateFinder extends AsyncTask<TransactionDateFinder.Param
             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;