]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
view binding for account and transaction list fragments
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.inputmethod.InputMethodManager;
29
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.lifecycle.ViewModelProvider;
33 import androidx.recyclerview.widget.LinearLayoutManager;
34 import androidx.recyclerview.widget.RecyclerView;
35 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.async.TransactionDateFinder;
39 import net.ktnx.mobileledger.databinding.TransactionListFragmentBinding;
40 import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
41 import net.ktnx.mobileledger.db.Profile;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.ui.DatePickerFragment;
44 import net.ktnx.mobileledger.ui.FabManager;
45 import net.ktnx.mobileledger.ui.MainModel;
46 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
47 import net.ktnx.mobileledger.ui.activity.MainActivity;
48 import net.ktnx.mobileledger.utils.Colors;
49 import net.ktnx.mobileledger.utils.Globals;
50 import net.ktnx.mobileledger.utils.Logger;
51 import net.ktnx.mobileledger.utils.SimpleDate;
52
53 import org.jetbrains.annotations.NotNull;
54
55 import java.util.Locale;
56
57 import static android.content.Context.INPUT_METHOD_SERVICE;
58 import static net.ktnx.mobileledger.utils.Logger.debug;
59
60 public class TransactionListFragment extends MobileLedgerListFragment
61         implements DatePickerFragment.DatePickedListener {
62     private MenuItem menuTransactionListFilter;
63     private MenuItem menuGoToDate;
64     private MainModel model;
65     private boolean fragmentActive = false;
66     private TransactionListFragmentBinding b;
67     @Override
68     public void onCreate(@Nullable Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setHasOptionsMenu(true);
71     }
72     @Nullable
73     @Override
74     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
75                              @Nullable Bundle savedInstanceState) {
76         b = TransactionListFragmentBinding.inflate(inflater, container, false);
77         return b.getRoot();
78     }
79     @Override
80     public void onResume() {
81         super.onResume();
82         fragmentActive = true;
83         toggleMenuItems();
84         debug("flow", "TransactionListFragment.onResume()");
85     }
86     private void toggleMenuItems() {
87         if (menuGoToDate != null)
88             menuGoToDate.setVisible(fragmentActive);
89         if (menuTransactionListFilter != null) {
90             final int filterVisibility = b.transactionListAccountNameFilter.getVisibility();
91             menuTransactionListFilter.setVisible(
92                     fragmentActive && filterVisibility != View.VISIBLE);
93         }
94     }
95     @Override
96     public void onStop() {
97         super.onStop();
98         fragmentActive = false;
99         toggleMenuItems();
100         debug("flow", "TransactionListFragment.onStop()");
101     }
102     @Override
103     public void onPause() {
104         super.onPause();
105         fragmentActive = false;
106         toggleMenuItems();
107         debug("flow", "TransactionListFragment.onPause()");
108     }
109     @Override
110     public SwipeRefreshLayout getRefreshLayout() {
111         return b.transactionSwipe;
112     }
113     @Override
114     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
115         debug("flow", "TransactionListFragment.onActivityCreated called");
116         super.onViewCreated(view, savedInstanceState);
117         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
118                 this::onBackgroundTaskRunningChanged);
119
120         MainActivity mainActivity = getMainActivity();
121
122         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
123
124         modelAdapter = new TransactionListAdapter();
125         b.transactionRoot.setAdapter(modelAdapter);
126
127         mainActivity.fabShouldShow();
128
129         if (mainActivity instanceof FabManager.FabHandler)
130             FabManager.handle(mainActivity, b.transactionRoot);
131
132         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
133
134         llm.setOrientation(RecyclerView.VERTICAL);
135         b.transactionRoot.setLayoutManager(llm);
136
137         b.transactionSwipe.setOnRefreshListener(() -> {
138             debug("ui", "refreshing transactions via swipe");
139             model.scheduleTransactionListRetrieval();
140         });
141
142         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
143
144         Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged);
145
146         b.transactionFilterAccountName.setOnItemClickListener((parent, v, position, id) -> {
147 //                debug("tmp", "direct onItemClick");
148             model.getAccountFilter()
149                  .setValue(parent.getItemAtPosition(position)
150                                  .toString());
151             Globals.hideSoftKeyboard(mainActivity);
152         });
153
154         model.getAccountFilter()
155              .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
156
157         model.getUpdatingFlag()
158              .observe(getViewLifecycleOwner(), (flag) -> b.transactionSwipe.setRefreshing(flag));
159         model.getDisplayedTransactions()
160              .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
161
162         view.findViewById(R.id.clearAccountNameFilter)
163             .setOnClickListener(v -> {
164                 model.getAccountFilter()
165                      .setValue(null);
166                 Globals.hideSoftKeyboard(mainActivity);
167             });
168
169         model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
170             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
171             if (pos != null) {
172                 b.transactionRoot.scrollToPosition(pos);
173                 // reset the value to avoid re-notification upon reconfiguration or app restart
174                 model.foundTransactionItemIndex.setValue(null);
175             }
176         });
177     }
178     private void onProfileChanged(Profile profile) {
179         if (profile == null)
180             return;
181
182         b.transactionFilterAccountName.setAdapter(
183                 new AccountAutocompleteAdapter(getContext(), profile));
184     }
185     private void onAccountNameFilterChanged(String accName) {
186         b.transactionFilterAccountName.setText(accName, false);
187
188         boolean filterActive = (accName != null) && !accName.isEmpty();
189         b.transactionListAccountNameFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
190         if (menuTransactionListFilter != null)
191             menuTransactionListFilter.setVisible(!filterActive);
192     }
193     @Override
194     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
195         inflater.inflate(R.menu.transaction_list, menu);
196
197         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
198         if ((menuTransactionListFilter == null))
199             throw new AssertionError();
200         menuGoToDate = menu.findItem(R.id.menu_go_to_date);
201         if ((menuGoToDate == null))
202             throw new AssertionError();
203
204         model.getAccountFilter()
205              .observe(this, v -> menuTransactionListFilter.setVisible(v == null));
206
207         super.onCreateOptionsMenu(menu, inflater);
208
209         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
210             b.transactionListAccountNameFilter.setVisibility(View.VISIBLE);
211             menuTransactionListFilter.setVisible(false);
212             b.transactionFilterAccountName.requestFocus();
213             InputMethodManager imm =
214                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
215             imm.showSoftInput(b.transactionFilterAccountName, 0);
216
217             return true;
218         });
219
220         menuGoToDate.setOnMenuItemClickListener(item -> {
221             DatePickerFragment picker = new DatePickerFragment();
222             picker.setOnDatePickedListener(this);
223             picker.setDateRange(model.getFirstTransactionDate(), model.getLastTransactionDate());
224             picker.show(requireActivity().getSupportFragmentManager(), null);
225             return true;
226         });
227
228         toggleMenuItems();
229     }
230     @Override
231     public void onDatePicked(int year, int month, int day) {
232         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
233         AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
234
235         finder.execute(
236                 new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));
237     }
238 }