From 826bfbfb7fe7a2491954793b0a954f26e28a784d Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 16 Apr 2019 20:52:43 +0300 Subject: [PATCH] migrate account name filter to LiveData --- .../net/ktnx/mobileledger/model/Data.java | 2 +- .../ui/activity/MainActivity.java | 13 +++++---- .../TransactionListAdapter.java | 2 +- .../TransactionListFragment.java | 27 +++++-------------- .../TransactionListViewModel.java | 2 +- 5 files changed, 15 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 57e2774d..43c6e3bd 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -40,7 +40,7 @@ public final class Data { public static ObservableList profiles = new ObservableList<>(new ArrayList<>()); public static ObservableValue optShowOnlyStarred = new ObservableValue<>(); - public static ObservableValue accountFilter = new ObservableValue<>(); + public static MutableLiveData accountFilter = new MutableLiveData<>(); public static void setCurrentProfile(MobileLedgerProfile newProfile) { MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid()); profile.set(newProfile); 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 3b14b422..344f33f8 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 @@ -114,15 +114,15 @@ public class MainActivity extends ProfileThemedActivity { Log.d("flow", "MainActivity.onStart()"); mViewPager.setCurrentItem(mCurrentPage, false); if (mAccountFilter != null) showTransactionsFragment(mAccountFilter); - else Data.accountFilter.set(null); + else Data.accountFilter.setValue(null); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_CURRENT_PAGE, mViewPager.getCurrentItem()); - if (Data.accountFilter.get() != null) - outState.putString(STATE_ACC_FILTER, Data.accountFilter.get()); + if (mAccountFilter != null) + outState.putString(STATE_ACC_FILTER, mAccountFilter); } @Override protected void onDestroy() { @@ -520,7 +520,7 @@ public class MainActivity extends ProfileThemedActivity { } private void showAccountSummaryFragment() { mViewPager.setCurrentItem(0, true); - Data.accountFilter.set(null); + Data.accountFilter.setValue(null); // FragmentTransaction ft = fragmentManager.beginTransaction(); // accountSummaryFragment = new AccountSummaryFragment(); // ft.replace(R.id.root_frame, accountSummaryFragment); @@ -536,8 +536,7 @@ public class MainActivity extends ProfileThemedActivity { // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE); } private void showTransactionsFragment(String accName) { - Data.accountFilter.set(accName); - Data.accountFilter.notifyObservers(); + Data.accountFilter.setValue(accName); mViewPager.setCurrentItem(1, true); } private void showTransactionsFragment(LedgerAccount account) { @@ -572,7 +571,7 @@ public class MainActivity extends ProfileThemedActivity { } else { if (mBackMeansToAccountList && (mViewPager.getCurrentItem() == 1)) { - Data.accountFilter.set(null); + Data.accountFilter.setValue(null); showAccountSummaryFragment(); mBackMeansToAccountList = false; } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java index 544e3342..63c91952 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java @@ -73,7 +73,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter { // Log.d("tmp", "direct onItemClick"); MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position); - Data.accountFilter.set(mc.getString(1)); + Data.accountFilter.setValue(mc.getString(1)); Globals.hideSoftKeyboard(mActivity); }); - if (accountFilterObserver == null) { - accountFilterObserver = (o, arg) -> onAccountNameFilterChanged(); - Data.accountFilter.addObserver(accountFilterObserver); - } + Data.accountFilter.observe(this, this::onAccountNameFilterChanged); TransactionListViewModel.updating.addObserver( (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get())); @@ -183,20 +175,13 @@ public class TransactionListFragment extends MobileLedgerListFragment { (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged())); mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> { - String current = Data.accountFilter.get(); - Data.accountFilter.set(null); + Data.accountFilter.setValue(null); vAccountFilter.setVisibility(View.GONE); menuTransactionListFilter.setVisible(true); Globals.hideSoftKeyboard(mActivity); }); - - onAccountNameFilterChanged(false); - } - private void onAccountNameFilterChanged() { - onAccountNameFilterChanged(true); } - private void onAccountNameFilterChanged(boolean reloadTransactions) { - String accName = Data.accountFilter.get(); + private void onAccountNameFilterChanged(String accName) { if (accNameFilter != null) { accNameFilter.setText(accName, false); } @@ -206,7 +191,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { } if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive); - if (reloadTransactions) TransactionListViewModel.scheduleTransactionListReload(); + TransactionListViewModel.scheduleTransactionListReload(); } @Override @@ -216,7 +201,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter); if ((menuTransactionListFilter == null)) throw new AssertionError(); - if (Data.accountFilter.get() != null) { + if (Data.accountFilter.getValue() != null) { menuTransactionListFilter.setVisible(false); } 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 2c049b3a..89b390f4 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 @@ -34,7 +34,7 @@ public class TransactionListViewModel extends ViewModel { public static void scheduleTransactionListReload() { if (Data.profile.get() == null) return; - String filter = Data.accountFilter.get(); + String filter = Data.accountFilter.getValue(); AsyncTask task = new UTT(); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filter); } -- 2.39.5