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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
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;
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;
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;
53 import org.jetbrains.annotations.NotNull;
55 import java.util.Locale;
57 import static android.content.Context.INPUT_METHOD_SERVICE;
58 import static net.ktnx.mobileledger.utils.Logger.debug;
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;
68 public void onCreate(@Nullable Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setHasOptionsMenu(true);
74 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
75 @Nullable Bundle savedInstanceState) {
76 b = TransactionListFragmentBinding.inflate(inflater, container, false);
80 public void onResume() {
82 fragmentActive = true;
84 debug("flow", "TransactionListFragment.onResume()");
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);
96 public void onStop() {
98 fragmentActive = false;
100 debug("flow", "TransactionListFragment.onStop()");
103 public void onPause() {
105 fragmentActive = false;
107 debug("flow", "TransactionListFragment.onPause()");
110 public SwipeRefreshLayout getRefreshLayout() {
111 return b.transactionSwipe;
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);
120 MainActivity mainActivity = getMainActivity();
122 model = new ViewModelProvider(requireActivity()).get(MainModel.class);
124 modelAdapter = new TransactionListAdapter();
125 b.transactionRoot.setAdapter(modelAdapter);
127 mainActivity.fabShouldShow();
129 if (mainActivity instanceof FabManager.FabHandler)
130 FabManager.handle(mainActivity, b.transactionRoot);
132 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
134 llm.setOrientation(RecyclerView.VERTICAL);
135 b.transactionRoot.setLayoutManager(llm);
137 b.transactionSwipe.setOnRefreshListener(() -> {
138 debug("ui", "refreshing transactions via swipe");
139 model.scheduleTransactionListRetrieval();
142 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
144 Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged);
146 b.transactionFilterAccountName.setOnItemClickListener((parent, v, position, id) -> {
147 // debug("tmp", "direct onItemClick");
148 model.getAccountFilter()
149 .setValue(parent.getItemAtPosition(position)
151 Globals.hideSoftKeyboard(mainActivity);
154 model.getAccountFilter()
155 .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
157 model.getUpdatingFlag()
158 .observe(getViewLifecycleOwner(), (flag) -> b.transactionSwipe.setRefreshing(flag));
159 model.getDisplayedTransactions()
160 .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
162 view.findViewById(R.id.clearAccountNameFilter)
163 .setOnClickListener(v -> {
164 model.getAccountFilter()
166 Globals.hideSoftKeyboard(mainActivity);
169 model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
170 Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
172 b.transactionRoot.scrollToPosition(pos);
173 // reset the value to avoid re-notification upon reconfiguration or app restart
174 model.foundTransactionItemIndex.setValue(null);
178 private void onProfileChanged(Profile profile) {
182 b.transactionFilterAccountName.setAdapter(
183 new AccountAutocompleteAdapter(getContext(), profile));
185 private void onAccountNameFilterChanged(String accName) {
186 b.transactionFilterAccountName.setText(accName, false);
188 boolean filterActive = (accName != null) && !accName.isEmpty();
189 b.transactionListAccountNameFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
190 if (menuTransactionListFilter != null)
191 menuTransactionListFilter.setVisible(!filterActive);
194 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
195 inflater.inflate(R.menu.transaction_list, menu);
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();
204 model.getAccountFilter()
205 .observe(this, v -> menuTransactionListFilter.setVisible(v == null));
207 super.onCreateOptionsMenu(menu, inflater);
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);
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);
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();
236 new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));