]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
major rework of parsed transaction/descriptions/accounts storage
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
index 68e243a891d4662cdfc76d4e9ef5a0e12fdb4dc3..9564cc8597317c79fa5bd378470c67eaf7ba9e0d 100644 (file)
@@ -17,7 +17,6 @@
 
 package net.ktnx.mobileledger.ui.transaction_list;
 
-import android.content.Context;
 import android.database.Cursor;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -32,15 +31,16 @@ import android.widget.AutoCompleteTextView;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
-import com.google.android.material.snackbar.Snackbar;
-
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.TransactionDateFinder;
 import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.DatePickerFragment;
+import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.Colors;
@@ -63,16 +63,12 @@ public class TransactionListFragment extends MobileLedgerListFragment
     private MenuItem menuTransactionListFilter;
     private View vAccountFilter;
     private AutoCompleteTextView accNameFilter;
+    private MainModel model;
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
     }
-    @Override
-    public void onAttach(@NotNull Context context) {
-        super.onAttach(context);
-        mActivity = (MainActivity) context;
-    }
     @Nullable
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@@ -102,70 +98,72 @@ public class TransactionListFragment extends MobileLedgerListFragment
         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
                 this::onBackgroundTaskRunningChanged);
 
-        swiper = mActivity.findViewById(R.id.transaction_swipe);
-        if (swiper == null)
+        MainActivity mainActivity = getMainActivity();
+
+        model = new ViewModelProvider(requireActivity()).get(MainModel.class);
+
+        refreshLayout = mainActivity.findViewById(R.id.transaction_swipe);
+        if (refreshLayout == null)
             throw new RuntimeException("Can't get hold on the swipe layout");
-        root = mActivity.findViewById(R.id.transaction_root);
+        root = mainActivity.findViewById(R.id.transaction_root);
         if (root == null)
             throw new RuntimeException("Can't get hold on the transaction value view");
-        modelAdapter = new TransactionListAdapter();
+        modelAdapter = new TransactionListAdapter(model);
         root.setAdapter(modelAdapter);
 
-        mActivity.fabShouldShow();
+        mainActivity.fabShouldShow();
 
         manageFabOnScroll();
 
-        LinearLayoutManager llm = new LinearLayoutManager(mActivity);
+        LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
 
         llm.setOrientation(RecyclerView.VERTICAL);
         root.setLayoutManager(llm);
 
-        swiper.setOnRefreshListener(() -> {
+        refreshLayout.setOnRefreshListener(() -> {
             debug("ui", "refreshing transactions via swipe");
-            Data.scheduleTransactionListRetrieval(mActivity);
+            model.scheduleTransactionListRetrieval();
         });
 
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
 
-        vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
-        accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
+        vAccountFilter = mainActivity.findViewById(R.id.transaction_list_account_name_filter);
+        accNameFilter = mainActivity.findViewById(R.id.transaction_filter_account_name);
 
-        MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
+        MLDB.hookAutocompletionAdapter(mainActivity, accNameFilter, "accounts", "name");
         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
 //                debug("tmp", "direct onItemClick");
             Cursor c = (Cursor) parent.getItemAtPosition(position);
-            Data.accountFilter.setValue(c.getString(1));
-            Globals.hideSoftKeyboard(mActivity);
+            model.getAccountFilter()
+                 .setValue(c.getString(1));
+            Globals.hideSoftKeyboard(mainActivity);
         });
 
-        Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
-
-        TransactionListViewModel.updating.addObserver(
-                (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
-        TransactionListViewModel.updateError.addObserver((o, arg) -> {
-            String err = TransactionListViewModel.updateError.get();
-            if (err == null)
-                return;
-
-            Snackbar.make(this.root, err, Snackbar.LENGTH_LONG)
-                    .show();
-            TransactionListViewModel.updateError.set(null);
-        });
-        Data.transactions.addObserver(
-                (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
-
-        mActivity.findViewById(R.id.clearAccountNameFilter)
-                 .setOnClickListener(v -> {
-                     Data.accountFilter.setValue(null);
-                     vAccountFilter.setVisibility(View.GONE);
-                     menuTransactionListFilter.setVisible(true);
-                     Globals.hideSoftKeyboard(mActivity);
-                 });
-
-        Data.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
+        model.getAccountFilter()
+             .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
+
+        model.getUpdatingFlag()
+             .observe(getViewLifecycleOwner(), (flag) -> refreshLayout.setRefreshing(flag));
+        MobileLedgerProfile profile = Data.getProfile();
+        model.getDisplayedTransactions()
+             .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
+
+        mainActivity.findViewById(R.id.clearAccountNameFilter)
+                    .setOnClickListener(v -> {
+                        model.getAccountFilter()
+                             .setValue(null);
+                        vAccountFilter.setVisibility(View.GONE);
+                        menuTransactionListFilter.setVisible(true);
+                        Globals.hideSoftKeyboard(mainActivity);
+                    });
+
+        model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
-            if (pos != null)
+            if (pos != null) {
                 root.scrollToPosition(pos);
+                // reset the value to avoid re-notification upon reconfiguration or app restart
+                model.foundTransactionItemIndex.setValue(null);
+            }
         });
     }
     private void onAccountNameFilterChanged(String accName) {
@@ -184,7 +182,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
         if (menuTransactionListFilter != null)
             menuTransactionListFilter.setVisible(!filterActive);
 
-        TransactionListViewModel.scheduleTransactionListReload();
+        model.scheduleTransactionListReload();
 
     }
     @Override
@@ -195,8 +193,8 @@ public class TransactionListFragment extends MobileLedgerListFragment
         if ((menuTransactionListFilter == null))
             throw new AssertionError();
 
-        if ((Data.accountFilter.getValue() != null) ||
-            (vAccountFilter.getVisibility() == View.VISIBLE))
+        if ((model.getAccountFilter()
+                  .getValue() != null) || (vAccountFilter.getVisibility() == View.VISIBLE))
         {
             menuTransactionListFilter.setVisible(false);
         }
@@ -209,7 +207,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
                 menuTransactionListFilter.setVisible(false);
             accNameFilter.requestFocus();
             InputMethodManager imm =
-                    (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
+                    (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
             imm.showSoftInput(accNameFilter, 0);
 
             return true;
@@ -219,8 +217,8 @@ public class TransactionListFragment extends MobileLedgerListFragment
             .setOnMenuItemClickListener(item -> {
                 DatePickerFragment picker = new DatePickerFragment();
                 picker.setOnDatePickedListener(this);
-                picker.setDateRange(Data.earliestTransactionDate.getValue(),
-                        Data.latestTransactionDate.getValue());
+                picker.setDateRange(model.getFirstTransactionDate(),
+                        model.getLastTransactionDate());
                 picker.show(requireActivity().getSupportFragmentManager(), null);
                 return true;
             });
@@ -228,8 +226,9 @@ public class TransactionListFragment extends MobileLedgerListFragment
     @Override
     public void onDatePicked(int year, int month, int day) {
         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
-        AsyncTask<SimpleDate, Void, Integer> finder = new TransactionDateFinder();
+        AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
 
-        finder.execute(new SimpleDate(year, month + 1, day));
+        finder.execute(
+                new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));
     }
 }