]> git.ktnx.net Git - mobile-ledger.git/commitdiff
drop hard reference to the main activity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 16 Jul 2020 05:26:24 +0000 (08:26 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 16 Jul 2020 05:26:24 +0000 (08:26 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index b7cafca484a8cfb98271212ae01256046122e0f3..94d54f16b41208063810ad5dbe3da8bc6c649b46 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -32,20 +32,24 @@ import net.ktnx.mobileledger.utils.DimensionUtils;
 public class MobileLedgerListFragment extends Fragment {
     public SwipeRefreshLayout swiper;
     public TransactionListAdapter modelAdapter;
-    protected MainActivity mActivity;
     protected RecyclerView root;
+    @NonNull
+    public MainActivity getMainActivity() {
+        return (MainActivity) requireActivity();
+    }
     protected void themeChanged(Integer counter) {
         swiper.setColorSchemeColors(Colors.getSwipeCircleColors());
     }
     public void onBackgroundTaskRunningChanged(Boolean isRunning) {
-        if (mActivity == null)
+        if (getActivity() == null)
             return;
         if (swiper == null)
             return;
         swiper.setRefreshing(isRunning);
     }
     protected void manageFabOnScroll() {
-        int triggerPixels = DimensionUtils.dp2px(mActivity, 30f);
+        final MainActivity mainActivity = getMainActivity();
+        int triggerPixels = DimensionUtils.dp2px(mainActivity, 30f);
         root.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
             private float upAnchor = -1;
             private float downAnchor = -1;
@@ -62,14 +66,14 @@ public class MobileLedgerListFragment extends Fragment {
                             // swipe down
                             upAnchor = lastY;
 
-                            mActivity.fabShouldShow();
+                            mainActivity.fabShouldShow();
                         }
                         else {
                             // swipe up
                             downAnchor = lastY;
 
                             if (currentY < upAnchor - triggerPixels)
-                                mActivity.fabHide();
+                                mainActivity.fabHide();
                         }
 
                         lastY = currentY;
index 8ee910b54fe33b4c33655cc771fc18cd8d0fb167..79253bfb238faedd682e8dcdcefebca2ae96ca2c 100644 (file)
@@ -50,7 +50,6 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     public void onAttach(@NotNull Context context) {
         super.onAttach(context);
         debug("flow", "AccountSummaryFragment.onAttach()");
-        mActivity = (MainActivity) context;
     }
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@@ -69,28 +68,29 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
                 this::onBackgroundTaskRunningChanged);
 
         modelAdapter = new AccountSummaryAdapter();
+        MainActivity mainActivity = getMainActivity();
 
-        root = mActivity.findViewById(R.id.account_root);
-        LinearLayoutManager llm = new LinearLayoutManager(mActivity);
+        root = mainActivity.findViewById(R.id.account_root);
+        LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
         llm.setOrientation(RecyclerView.VERTICAL);
         root.setLayoutManager(llm);
         root.setAdapter(modelAdapter);
         DividerItemDecoration did =
-                new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL);
+                new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
         root.addItemDecoration(did);
 
-        mActivity.fabShouldShow();
+        mainActivity.fabShouldShow();
 
         manageFabOnScroll();
 
-        swiper = mActivity.findViewById(R.id.account_swiper);
+        swiper = mainActivity.findViewById(R.id.account_swiper);
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
         swiper.setOnRefreshListener(() -> {
             debug("ui", "refreshing accounts via swipe");
-            Data.scheduleTransactionListRetrieval(mActivity);
+            Data.scheduleTransactionListRetrieval(mainActivity);
         });
 
         Data.accounts.addObserver(
-                (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
+                (o, arg) -> mainActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
     }
 }
index 47794138cd1cb9ed526d3d7360aa7cf904ddc306..19945505c86dbb524bb529f41f759071be840c5b 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;
@@ -68,11 +67,6 @@ public class TransactionListFragment extends MobileLedgerListFragment
         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,40 +96,42 @@ public class TransactionListFragment extends MobileLedgerListFragment
         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
                 this::onBackgroundTaskRunningChanged);
 
-        swiper = mActivity.findViewById(R.id.transaction_swipe);
+        MainActivity mainActivity = getMainActivity();
+
+        swiper = mainActivity.findViewById(R.id.transaction_swipe);
         if (swiper == 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();
         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(() -> {
             debug("ui", "refreshing transactions via swipe");
-            Data.scheduleTransactionListRetrieval(mActivity);
+            Data.scheduleTransactionListRetrieval(mainActivity);
         });
 
         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);
+            Globals.hideSoftKeyboard(mainActivity);
         });
 
         Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
@@ -152,15 +148,15 @@ public class TransactionListFragment extends MobileLedgerListFragment
             TransactionListViewModel.updateError.set(null);
         });
         Data.transactions.addObserver(
-                (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
+                (o, arg) -> mainActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
 
-        mActivity.findViewById(R.id.clearAccountNameFilter)
-                 .setOnClickListener(v -> {
-                     Data.accountFilter.setValue(null);
-                     vAccountFilter.setVisibility(View.GONE);
-                     menuTransactionListFilter.setVisible(true);
-                     Globals.hideSoftKeyboard(mActivity);
-                 });
+        mainActivity.findViewById(R.id.clearAccountNameFilter)
+                    .setOnClickListener(v -> {
+                        Data.accountFilter.setValue(null);
+                        vAccountFilter.setVisibility(View.GONE);
+                        menuTransactionListFilter.setVisible(true);
+                        Globals.hideSoftKeyboard(mainActivity);
+                    });
 
         Data.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
@@ -212,7 +208,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;