From: Damyan Ivanov Date: Thu, 16 Jul 2020 05:26:24 +0000 (+0300) Subject: drop hard reference to the main activity X-Git-Tag: v0.15.0~91 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=b54a11eca1e4d0a03a3076047dc77f68666d4338;p=mobile-ledger.git drop hard reference to the main activity --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java index b7cafca4..94d54f16 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MobileLedgerListFragment.java @@ -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; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java index 8ee910b5..79253bfb 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryFragment.java @@ -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())); } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 47794138..19945505 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -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;