]> git.ktnx.net Git - mobile-ledger.git/commitdiff
migrate account name filter to LiveData
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Apr 2019 17:52:43 +0000 (20:52 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Apr 2019 17:52:43 +0000 (20:52 +0300)
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.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 57e2774dee137c4a642c50a265d42a4c2a6f7ee6..43c6e3bd6315738d132bdd5e9f19cb7b10012d66 100644 (file)
@@ -40,7 +40,7 @@ public final class Data {
     public static ObservableList<MobileLedgerProfile> profiles =
             new ObservableList<>(new ArrayList<>());
     public static ObservableValue<Boolean> optShowOnlyStarred = new ObservableValue<>();
-    public static ObservableValue<String> accountFilter = new ObservableValue<>();
+    public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
     public static void setCurrentProfile(MobileLedgerProfile newProfile) {
         MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
         profile.set(newProfile);
index 3b14b422c8906b5e7817dc2c3d01278d8cc86497..344f33f8a8a5225cfb7953f528342221ac42b7d1 100644 (file)
@@ -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;
             }
index 544e3342d2fad195a928046b36d91b95f49fb922..63c91952e9bcd07b0cd8f9f0e4862ed7391cf0a9 100644 (file)
@@ -73,7 +73,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
 
                 TransactionLoader loader = new TransactionLoader();
                 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
-                        new TransactionLoaderParams(tr, holder, position, Data.accountFilter.get(),
+                        new TransactionLoaderParams(tr, holder, position, Data.accountFilter.getValue(),
                                 item.isOdd()));
 
                 // WORKAROUND what seems to be a bug in CardHolder somewhere
index 12447d26f42bc68da59cacbc9aa706aaf168597e..a7787a7e515517800bc20f23759032a134a16b5b 100644 (file)
@@ -56,9 +56,6 @@ public class TransactionListFragment extends MobileLedgerListFragment {
     private View vAccountFilter;
     private AutoCompleteTextView accNameFilter;
     private Observer backgroundTaskCountObserver;
-    private Observer accountFilterObserver;
-    private static void update(Observable o, Object arg) {
-    }
     @Override
     public void onDestroy() {
         if (backgroundTaskCountObserver != null) {
@@ -67,8 +64,6 @@ public class TransactionListFragment extends MobileLedgerListFragment {
         }
         super.onDestroy();
     }
-    public void setShowOnlyAccountName(String mShowOnlyAccountName) {
-    }
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -158,14 +153,11 @@ public class TransactionListFragment extends MobileLedgerListFragment {
         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
 //                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);
         }
 
index 2c049b3ad6ad9c909d1d3ec9756141252390891e..89b390f4bc7c1b6b315d03adb631616656369d35 100644 (file)
@@ -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<String, Void, String> task = new UTT();
         task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filter);
     }