]> git.ktnx.net Git - mobile-ledger.git/commitdiff
view binding for account and transaction list fragments
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 30 Apr 2021 06:51:06 +0000 (09:51 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 30 Apr 2021 19:20:20 +0000 (19:20 +0000)
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 c5bbe0c4eece2f4325c353ac935bb15ffcad6fef..658ce66b9b8b13252787c5539f3ff65b7384a13c 100644 (file)
@@ -19,29 +19,28 @@ package net.ktnx.mobileledger.ui;
 
 import androidx.annotation.NonNull;
 import androidx.fragment.app.Fragment;
-import androidx.recyclerview.widget.RecyclerView;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
 import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListAdapter;
 import net.ktnx.mobileledger.utils.Colors;
 
-public class MobileLedgerListFragment extends Fragment {
-    public SwipeRefreshLayout refreshLayout;
+public abstract class MobileLedgerListFragment extends Fragment {
     public TransactionListAdapter modelAdapter;
-    protected RecyclerView root;
+    public abstract SwipeRefreshLayout getRefreshLayout();
     @NonNull
     public MainActivity getMainActivity() {
         return (MainActivity) requireActivity();
     }
     protected void themeChanged(Integer counter) {
-        refreshLayout.setColorSchemeColors(Colors.getSwipeCircleColors());
+        getRefreshLayout().setColorSchemeColors(Colors.getSwipeCircleColors());
     }
     public void onBackgroundTaskRunningChanged(Boolean isRunning) {
         if (getActivity() == null)
             return;
-        if (refreshLayout == null)
+        SwipeRefreshLayout l = getRefreshLayout();
+        if (l == null)
             return;
-        refreshLayout.setRefreshing(isRunning);
+        l.setRefreshing(isRunning);
     }
 }
index 8ad6da6ff0fa7233613d1ffaa53fd694e99d805e..9ccdf4948fdd6d87ecdf16e1e20c1652e4d23b53 100644 (file)
@@ -30,8 +30,9 @@ import androidx.lifecycle.ViewModelProvider;
 import androidx.recyclerview.widget.DividerItemDecoration;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
-import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.databinding.AccountSummaryFragmentBinding;
 import net.ktnx.mobileledger.db.AccountWithAmounts;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Profile;
@@ -54,6 +55,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class AccountSummaryFragment extends MobileLedgerListFragment {
     public AccountSummaryAdapter modelAdapter;
+    private AccountSummaryFragmentBinding b;
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -68,9 +70,13 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
         debug("flow", "AccountSummaryFragment.onCreateView()");
-        return inflater.inflate(R.layout.account_summary_fragment, container, false);
+        b = AccountSummaryFragmentBinding.inflate(inflater, container, false);
+        return b.getRoot();
+    }
+    @Override
+    public SwipeRefreshLayout getRefreshLayout() {
+        return b.accountSwipeRefreshLayout;
     }
-
     @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         debug("flow", "AccountSummaryFragment.onActivityCreated()");
@@ -84,23 +90,21 @@ public class AccountSummaryFragment extends MobileLedgerListFragment {
         modelAdapter = new AccountSummaryAdapter();
         MainActivity mainActivity = getMainActivity();
 
-        root = view.findViewById(R.id.account_root);
         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
         llm.setOrientation(RecyclerView.VERTICAL);
-        root.setLayoutManager(llm);
-        root.setAdapter(modelAdapter);
+        b.accountRoot.setLayoutManager(llm);
+        b.accountRoot.setAdapter(modelAdapter);
         DividerItemDecoration did =
                 new DividerItemDecoration(mainActivity, DividerItemDecoration.VERTICAL);
-        root.addItemDecoration(did);
+        b.accountRoot.addItemDecoration(did);
 
         mainActivity.fabShouldShow();
 
         if (mainActivity instanceof FabManager.FabHandler)
-            FabManager.handle(mainActivity, root);
+            FabManager.handle(mainActivity, b.accountRoot);
 
-        refreshLayout = view.findViewById(R.id.account_swipe_refresh_layout);
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
-        refreshLayout.setOnRefreshListener(() -> {
+        b.accountSwipeRefreshLayout.setOnRefreshListener(() -> {
             debug("ui", "refreshing accounts via swipe");
             model.scheduleTransactionListRetrieval();
         });
index c9fd0bf33f51a75b0af907dd504eb07985cba65c..5a01bde0b9c435c32dd5c48e6ccd431f6da7a4c9 100644 (file)
@@ -26,16 +26,17 @@ import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.inputmethod.InputMethodManager;
-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 androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.TransactionDateFinder;
+import net.ktnx.mobileledger.databinding.TransactionListFragmentBinding;
 import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
 import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.model.Data;
@@ -60,10 +61,9 @@ public class TransactionListFragment extends MobileLedgerListFragment
         implements DatePickerFragment.DatePickedListener {
     private MenuItem menuTransactionListFilter;
     private MenuItem menuGoToDate;
-    private View vAccountFilter;
-    private AutoCompleteTextView accNameFilter;
     private MainModel model;
     private boolean fragmentActive = false;
+    private TransactionListFragmentBinding b;
     @Override
     public void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -73,7 +73,8 @@ public class TransactionListFragment extends MobileLedgerListFragment
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
-        return inflater.inflate(R.layout.transaction_list_fragment, container, false);
+        b = TransactionListFragmentBinding.inflate(inflater, container, false);
+        return b.getRoot();
     }
     @Override
     public void onResume() {
@@ -86,7 +87,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
         if (menuGoToDate != null)
             menuGoToDate.setVisible(fragmentActive);
         if (menuTransactionListFilter != null) {
-            final int filterVisibility = vAccountFilter.getVisibility();
+            final int filterVisibility = b.transactionListAccountNameFilter.getVisibility();
             menuTransactionListFilter.setVisible(
                     fragmentActive && filterVisibility != View.VISIBLE);
         }
@@ -106,10 +107,13 @@ public class TransactionListFragment extends MobileLedgerListFragment
         debug("flow", "TransactionListFragment.onPause()");
     }
     @Override
+    public SwipeRefreshLayout getRefreshLayout() {
+        return b.transactionSwipe;
+    }
+    @Override
     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
         debug("flow", "TransactionListFragment.onActivityCreated called");
         super.onViewCreated(view, savedInstanceState);
-
         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
                 this::onBackgroundTaskRunningChanged);
 
@@ -117,38 +121,29 @@ public class TransactionListFragment extends MobileLedgerListFragment
 
         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
 
-        refreshLayout = view.findViewById(R.id.transaction_swipe);
-        if (refreshLayout == null)
-            throw new RuntimeException("Can't get hold on the swipe layout");
-        root = view.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);
+        b.transactionRoot.setAdapter(modelAdapter);
 
         mainActivity.fabShouldShow();
 
         if (mainActivity instanceof FabManager.FabHandler)
-            FabManager.handle(mainActivity, root);
+            FabManager.handle(mainActivity, b.transactionRoot);
 
         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
 
         llm.setOrientation(RecyclerView.VERTICAL);
-        root.setLayoutManager(llm);
+        b.transactionRoot.setLayoutManager(llm);
 
-        refreshLayout.setOnRefreshListener(() -> {
+        b.transactionSwipe.setOnRefreshListener(() -> {
             debug("ui", "refreshing transactions via swipe");
             model.scheduleTransactionListRetrieval();
         });
 
         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
 
-        vAccountFilter = view.findViewById(R.id.transaction_list_account_name_filter);
-        accNameFilter = view.findViewById(R.id.transaction_filter_account_name);
-
         Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged);
 
-        accNameFilter.setOnItemClickListener((parent, v, position, id) -> {
+        b.transactionFilterAccountName.setOnItemClickListener((parent, v, position, id) -> {
 //                debug("tmp", "direct onItemClick");
             model.getAccountFilter()
                  .setValue(parent.getItemAtPosition(position)
@@ -160,7 +155,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
              .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
 
         model.getUpdatingFlag()
-             .observe(getViewLifecycleOwner(), (flag) -> refreshLayout.setRefreshing(flag));
+             .observe(getViewLifecycleOwner(), (flag) -> b.transactionSwipe.setRefreshing(flag));
         model.getDisplayedTransactions()
              .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
 
@@ -174,7 +169,7 @@ public class TransactionListFragment extends MobileLedgerListFragment
         model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
             if (pos != null) {
-                root.scrollToPosition(pos);
+                b.transactionRoot.scrollToPosition(pos);
                 // reset the value to avoid re-notification upon reconfiguration or app restart
                 model.foundTransactionItemIndex.setValue(null);
             }
@@ -184,15 +179,14 @@ public class TransactionListFragment extends MobileLedgerListFragment
         if (profile == null)
             return;
 
-        accNameFilter.setAdapter(new AccountAutocompleteAdapter(getContext(), profile));
+        b.transactionFilterAccountName.setAdapter(
+                new AccountAutocompleteAdapter(getContext(), profile));
     }
     private void onAccountNameFilterChanged(String accName) {
-        accNameFilter.setText(accName, false);
+        b.transactionFilterAccountName.setText(accName, false);
 
         boolean filterActive = (accName != null) && !accName.isEmpty();
-        if (vAccountFilter != null) {
-            vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
-        }
+        b.transactionListAccountNameFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
         if (menuTransactionListFilter != null)
             menuTransactionListFilter.setVisible(!filterActive);
     }
@@ -213,12 +207,12 @@ public class TransactionListFragment extends MobileLedgerListFragment
         super.onCreateOptionsMenu(menu, inflater);
 
         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
-            vAccountFilter.setVisibility(View.VISIBLE);
+            b.transactionListAccountNameFilter.setVisibility(View.VISIBLE);
             menuTransactionListFilter.setVisible(false);
-            accNameFilter.requestFocus();
+            b.transactionFilterAccountName.requestFocus();
             InputMethodManager imm =
                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
-            imm.showSoftInput(accNameFilter, 0);
+            imm.showSoftInput(b.transactionFilterAccountName, 0);
 
             return true;
         });