X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionItemsAdapter.java;h=80deaf27281315af7974e05f2c7def6fd9bb1cae;hp=7d01880057901954a2f9ad4c439050531aeabc78;hb=de1680dc2e67d2f7be40466a2e51cf8641393cf1;hpb=49a250214ca9dcf55c5960e82332b55e51ecaf38 diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java index 7d018800..80deaf27 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2020 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 @@ -18,6 +18,7 @@ package net.ktnx.mobileledger.ui.activity; import android.annotation.SuppressLint; +import android.app.Activity; import android.database.Cursor; import android.view.LayoutInflater; import android.view.ViewGroup; @@ -28,7 +29,8 @@ import androidx.annotation.Nullable; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; -import net.ktnx.mobileledger.App; +import com.google.android.material.snackbar.Snackbar; + import net.ktnx.mobileledger.BuildConfig; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.DescriptionSelectedCallback; @@ -38,6 +40,7 @@ import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Logger; +import net.ktnx.mobileledger.utils.MLDB; import net.ktnx.mobileledger.utils.Misc; import java.util.ArrayList; @@ -50,7 +53,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug; class NewTransactionItemsAdapter extends RecyclerView.Adapter implements DescriptionSelectedCallback { - NewTransactionModel model; + private NewTransactionModel model; private MobileLedgerProfile mProfile; private ItemTouchHelper touchHelper; private RecyclerView recyclerView; @@ -124,10 +127,10 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter params = new ArrayList<>(); - StringBuilder sb = new StringBuilder( - "select t.profile, t.id from transactions t where t.description=?"); + StringBuilder sb = new StringBuilder("select t.profile, t.id from transactions t"); + + if (!Misc.isEmptyOrNull(accFilter)) { + sb.append(" JOIN transaction_accounts ta") + .append(" ON ta.profile = t.profile") + .append(" AND ta.transaction_id = t.id"); + } + + sb.append(" WHERE t.description=?"); params.add(description); - if (accFilter != null) { - sb.append(" AND EXISTS (") - .append("SELECT 1 FROM transaction_accounts ta ") - .append("WHERE ta.profile = t.profile") - .append(" AND ta.transaction_id = t.id") - .append(" AND UPPER(ta.account_name) LIKE '%'||?||'%')"); - params.add(accFilter.toUpperCase()); + if (!Misc.isEmptyOrNull(accFilter)) { + sb.append(" AND ta.account_name LIKE '%'||?||'%'"); + params.add(accFilter); } - sb.append(" ORDER BY date desc limit 1"); + sb.append(" ORDER BY t.year desc, t.month desc, t.day desc LIMIT 1"); final String sql = sb.toString(); debug("descr", sql); debug("descr", params.toString()); - try (Cursor c = App.getDatabase() - .rawQuery(sql, params.toArray(new String[]{}))) - { - String profileUUID; - int transactionId; + Activity activity = (Activity) recyclerView.getContext(); + // FIXME: handle exceptions? + MLDB.queryInBackground(sql, params.toArray(new String[]{}), new MLDB.CallbackHelper() { + @Override + public void onStart() { + model.incrementBusyCounter(); + } + @Override + public void onDone() { + model.decrementBusyCounter(); + } + @Override + public boolean onRow(@NonNull Cursor cursor) { + final String profileUUID = cursor.getString(0); + final int transactionId = cursor.getInt(1); + activity.runOnUiThread(() -> loadTransactionIntoModel(profileUUID, transactionId)); + return false; // limit 1, by the way + } + @Override + public void onNoRows() { + if (Misc.isEmptyOrNull(accFilter)) + return; - if (!c.moveToNext()) { - sb = new StringBuilder("select t.profile, t.id from transactions t where t.description=?"); - sb.append(" ORDER BY date desc LIMIT 1"); + debug("descr", "Trying transaction search without preferred account filter"); - final String broaderSql = sb.toString(); + final String broaderSql = + "select t.profile, t.id from transactions t where t.description=?" + + " ORDER BY year desc, month desc, day desc LIMIT 1"; + params.remove(1); debug("descr", broaderSql); - debug("descr", params.toString()); - try (Cursor c2 = App.getDatabase().rawQuery(broaderSql, new String[]{description})) { - if (!c2.moveToNext()) return; - - profileUUID = c2.getString(0); - transactionId = c2.getInt(1); - } - } - else { - profileUUID = c.getString(0); - transactionId = c.getInt(1); + debug("descr", description); + + activity.runOnUiThread(() -> { + Snackbar.make(recyclerView, R.string.ignoring_preferred_account, + Snackbar.LENGTH_LONG) + .show(); + }); + + MLDB.queryInBackground(broaderSql, new String[]{description}, + new MLDB.CallbackHelper() { + @Override + public void onStart() { + model.incrementBusyCounter(); + } + @Override + public boolean onRow(@NonNull Cursor cursor) { + final String profileUUID = cursor.getString(0); + final int transactionId = cursor.getInt(1); + activity.runOnUiThread( + () -> loadTransactionIntoModel(profileUUID, transactionId)); + return false; + } + @Override + public void onDone() { + model.decrementBusyCounter(); + } + }); } - - loadTransactionIntoModel(profileUUID, transactionId); - } + }); } private void loadTransactionIntoModel(String profileUUID, int transactionId) { LedgerTransaction tr; @@ -263,6 +301,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 2) notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone } - public void updateFocusedItem(int position) { + void updateFocusedItem(int position) { model.updateFocusedItem(position); } - public void noteFocusIsOnAccount(int position) { + void noteFocusIsOnAccount(int position) { model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account); } - public void noteFocusIsOnAmount(int position) { + void noteFocusIsOnAmount(int position) { model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount); } - public void noteFocusIsOnComment(int position) { + void noteFocusIsOnComment(int position) { model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment); } - public void toggleComment(int position) { - model.toggleComment(position); + void noteFocusIsOnTransactionComment(int position) { + model.noteFocusChanged(position, NewTransactionModel.FocusedElement.TransactionComment); + } + public void noteFocusIsOnDescription(int pos) { + model.noteFocusChanged(pos, NewTransactionModel.FocusedElement.Description); } private void holdSubmittableChecks() { checkHoldCounter++; @@ -431,7 +473,12 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter hashMap = new HashMap<>(); float get(String currencyName) { Float f = hashMap.get(currencyName); @@ -624,7 +672,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter> hashMap = new HashMap<>(); @NonNull List getList(@Nullable String currencyName) {