From 2e6d7c5f759986f89a867ee5b9162496019d5130 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 11 Jan 2019 20:49:01 +0000 Subject: [PATCH] restore account name filter in the transaction list the filter menu item has a glitch but mostly works --- .../async/RetrieveTransactionsTask.java | 3 +- .../async/UpdateTransactionsTask.java | 15 ++- .../ui/activity/MainActivity.java | 11 +-- .../TransactionListFragment.java | 92 ++++++++----------- .../TransactionListViewModel.java | 15 +-- 5 files changed, 51 insertions(+), 85 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 4e2c6108..91ea8ffc 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -30,6 +30,7 @@ import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.activity.MainActivity; +import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel; import net.ktnx.mobileledger.utils.MLDB; import net.ktnx.mobileledger.utils.NetworkUtil; @@ -354,7 +355,7 @@ public class RetrieveTransactionsTask Date now = new Date(); profile.set_option_value(MLDB.OPT_LAST_SCRAPE, now.getTime()); Data.lastUpdateDate.set(now); - Data.transactions.set(transactionList); + TransactionListViewModel.scheduleTransactionListReload(); } finally { db.endTransaction(); diff --git a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java index 33b37845..178db99f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java @@ -36,21 +36,20 @@ public class UpdateTransactionsTask extends AsyncTask newList = new ArrayList<>(); - boolean hasFilter = (filterAccName != null) && (filterAccName.length > 0) && - (filterAccName[0] != null) && !filterAccName[0].isEmpty(); - String sql; String[] params; - sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc"; - params = new String[]{profile_uuid}; + if (filterAccName[0] == null) { + sql = "SELECT id FROM transactions WHERE profile=? ORDER BY date desc, id desc"; + params = new String[]{profile_uuid}; - if (hasFilter) { + } + else { sql = "SELECT distinct tr.id from transactions tr JOIN transaction_accounts ta " + "ON ta.transaction_id=tr.id AND ta.profile=tr.profile WHERE tr.profile=? " + - "and ta" + ".account_name LIKE ?||'%' AND ta" + + "and ta.account_name LIKE ?||'%' AND ta" + ".amount <> 0 ORDER BY tr.date desc, tr.id desc"; - params = filterAccName; + params = new String[]{profile_uuid, filterAccName[0]}; } Log.d("UTT", sql); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 1218d0cf..f16de22d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -250,16 +250,6 @@ public class MainActivity extends AppCompatActivity { } } } - public void onViewClicked(View view) { - switch (view.getId()) { - case R.id.clearAccountNameFilter: - if (transactionListFragment != null) - transactionListFragment.onClearAccountNameClick(view); - break; - default: - Log.e("click", String.format("View %d click not handled", view.getId())); - } - } public void onAccountSummaryClicked(View view) { drawer.closeDrawers(); @@ -282,6 +272,7 @@ public class MainActivity extends AppCompatActivity { // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE); } private void showTransactionsFragment(LedgerAccount account) { + TransactionListFragment.accountFilter.set(account.getName()); mViewPager.setCurrentItem(1, true); // FragmentTransaction ft = fragmentManager.beginTransaction(); // if (transactionListFragment == null) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 9ec46e79..d7192592 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -34,7 +34,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; import android.widget.AutoCompleteTextView; import net.ktnx.mobileledger.R; @@ -43,6 +42,7 @@ import net.ktnx.mobileledger.ui.MobileLedgerListFragment; import net.ktnx.mobileledger.ui.activity.MainActivity; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.MLDB; +import net.ktnx.mobileledger.utils.ObservableValue; import java.util.Observable; import java.util.Observer; @@ -51,6 +51,7 @@ import static android.content.Context.INPUT_METHOD_SERVICE; public class TransactionListFragment extends MobileLedgerListFragment { public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name"; + public static ObservableValue accountFilter = new ObservableValue<>(); private String mShowOnlyAccountName; private MenuItem menuTransactionListFilter; private View vAccountFilter; @@ -114,7 +115,6 @@ public class TransactionListFragment extends MobileLedgerListFragment { @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.transaction_list_fragment, container, false); } - @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { Log.d("flow", "TransactionListFragment.onActivityCreated called"); @@ -159,49 +159,42 @@ public class TransactionListFragment extends MobileLedgerListFragment { TransactionListFragment me = this; MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name", true); - accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { + accNameFilter.setOnItemClickListener((parent, view, position, id) -> { // Log.d("tmp", "direct onItemClick"); - TransactionListViewModel.scheduleTransactionListReload(mActivity); - MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position); - modelAdapter.setBoldAccountName(mc.getString(1)); - modelAdapter.notifyDataSetChanged(); - Globals.hideSoftKeyboard(mActivity); - } + TransactionListViewModel.scheduleTransactionListReload(); + MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position); + accountFilter.set(mc.getString(1)); + Globals.hideSoftKeyboard(mActivity); }); - if (mShowOnlyAccountName != null) { - accNameFilter.setText(mShowOnlyAccountName, false); - onShowFilterClick(null); - Log.d("flow", String.format("Account filter set to '%s'", mShowOnlyAccountName)); - } - - Data.profile.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - mActivity.runOnUiThread(() -> { - Log.d("transactions", "requesting list reload"); - TransactionListViewModel.scheduleTransactionListReload(mActivity); - }); - } + accountFilter.addObserver((o, arg) -> { + String accountName = accountFilter.get(); + modelAdapter.setBoldAccountName(accountName); + setShowOnlyAccountName(accountName); + TransactionListViewModel.scheduleTransactionListReload(); + if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false); }); - TransactionListViewModel.scheduleTransactionListReload(mActivity); - TransactionListViewModel.updating.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - swiper.setRefreshing(TransactionListViewModel.updating.get()); - } + Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(() -> { + Log.d("transactions", "requesting list reload"); + TransactionListViewModel.scheduleTransactionListReload(); + })); + + TransactionListViewModel.scheduleTransactionListReload(); + TransactionListViewModel.updating.addObserver( + (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get())); + + Data.transactions.addObserver( + (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged())); + + mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> { + vAccountFilter.setVisibility(View.GONE); + if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true); + accountFilter.set(null); + accNameFilter.setText(null); + TransactionListViewModel.scheduleTransactionListReload(); + Globals.hideSoftKeyboard(mActivity); }); - - Data.transactions.addObserver(new Observer() { - @Override - public void update(Observable o, Object arg) { - mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()); - } - }); - } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @@ -215,25 +208,16 @@ public class TransactionListFragment extends MobileLedgerListFragment { } super.onCreateOptionsMenu(menu, inflater); - } - public void onClearAccountNameClick(View view) { - vAccountFilter.setVisibility(View.GONE); - if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true); - accNameFilter.setText(null); - mShowOnlyAccountName = null; - modelAdapter.resetBoldAccountName(); - TransactionListViewModel.scheduleTransactionListReload(mActivity); - Globals.hideSoftKeyboard(mActivity); - } - public void onShowFilterClick(MenuItem menuItem) { - vAccountFilter.setVisibility(View.VISIBLE); - if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false); - if (menuItem != null) { + menuTransactionListFilter.setOnMenuItemClickListener(item -> { + vAccountFilter.setVisibility(View.VISIBLE); + if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false); accNameFilter.requestFocus(); InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE); imm.showSoftInput(accNameFilter, 0); - } + + return true; + }); } } \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index d391b622..89b56022 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -17,13 +17,9 @@ package net.ktnx.mobileledger.ui.transaction_list; -import android.app.Activity; import android.arch.lifecycle.ViewModel; import android.os.AsyncTask; -import android.view.View; -import android.widget.AutoCompleteTextView; -import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.UpdateTransactionsTask; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransaction; @@ -34,15 +30,10 @@ import java.util.List; public class TransactionListViewModel extends ViewModel { public static ObservableValue updating = new ObservableValue<>(); - public static void scheduleTransactionListReload(Activity act) { - View filter = act.findViewById(R.id.transaction_list_account_name_filter); - if (filter == null) return; - boolean hasFilter = filter.getVisibility() == View.VISIBLE; - String accFilter = hasFilter ? String.valueOf( - ((AutoCompleteTextView) act.findViewById(R.id.transaction_filter_account_name)) - .getText()) : null; + public static void scheduleTransactionListReload() { + String filter = TransactionListFragment.accountFilter.get(); AsyncTask> task = new UTT(); - task.execute(accFilter); + task.execute(filter); } public static LedgerTransaction getTransaction(int position) { List transactions = Data.transactions.get(); -- 2.39.5