]> git.ktnx.net Git - mobile-ledger.git/commitdiff
restore account name filter in the transaction list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 11 Jan 2019 20:49:01 +0000 (20:49 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 11 Jan 2019 20:49:01 +0000 (20:49 +0000)
the filter menu item has a glitch but mostly works

app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java

index 4e2c6108f5d1eb79f74d8898b8b25899d8f2fdf5..91ea8ffc55bfe394df8b7001c9d9b7ea90b76b9b 100644 (file)
@@ -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();
index 33b378450c874652bb5dd7443cb4c8ffbf317779..178db99fb473776bf4ad5cfaf71cf45724b58001 100644 (file)
@@ -36,21 +36,20 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, List<LedgerT
         try {
             ArrayList<LedgerTransaction> 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);
index 1218d0cf3d5cc37a5b4f6c1081f82b83e591d566..f16de22d84b3cce63e07118f063fea6afe5d5c39 100644 (file)
@@ -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) {
index 9ec46e79f9fbbc1cca26590ebeb61e9a5812f1b7..d719259237c5ded76fd946123483aaac37fd9aff 100644 (file)
@@ -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<String> 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
index d391b6227b5674019a853609cad9802f72330204..89b56022689f910425ce48ed363cb9d521679531 100644 (file)
 
 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<Boolean> 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<String, Void, List<LedgerTransaction>> task = new UTT();
-        task.execute(accFilter);
+        task.execute(filter);
     }
     public static LedgerTransaction getTransaction(int position) {
         List<LedgerTransaction> transactions = Data.transactions.get();