2 * Copyright © 2020 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.database.Cursor;
21 import android.os.AsyncTask;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.inputmethod.InputMethodManager;
30 import android.widget.AutoCompleteTextView;
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.recyclerview.widget.LinearLayoutManager;
35 import androidx.recyclerview.widget.RecyclerView;
37 import com.google.android.material.snackbar.Snackbar;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.async.TransactionDateFinder;
41 import net.ktnx.mobileledger.model.Data;
42 import net.ktnx.mobileledger.ui.DatePickerFragment;
43 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
44 import net.ktnx.mobileledger.ui.activity.MainActivity;
45 import net.ktnx.mobileledger.utils.Colors;
46 import net.ktnx.mobileledger.utils.Globals;
47 import net.ktnx.mobileledger.utils.Logger;
48 import net.ktnx.mobileledger.utils.MLDB;
49 import net.ktnx.mobileledger.utils.SimpleDate;
51 import org.jetbrains.annotations.NotNull;
53 import java.util.Locale;
55 import static android.content.Context.INPUT_METHOD_SERVICE;
56 import static net.ktnx.mobileledger.utils.Logger.debug;
58 // TODO: support transaction-level comment
60 public class TransactionListFragment extends MobileLedgerListFragment
61 implements DatePickerFragment.DatePickedListener {
62 private MenuItem menuTransactionListFilter;
63 private View vAccountFilter;
64 private AutoCompleteTextView accNameFilter;
66 public void onCreate(@Nullable Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 setHasOptionsMenu(true);
72 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
73 @Nullable Bundle savedInstanceState) {
74 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
77 public void onResume() {
79 debug("flow", "TransactionListFragment.onResume()");
82 public void onStop() {
84 debug("flow", "TransactionListFragment.onStop()");
87 public void onPause() {
89 debug("flow", "TransactionListFragment.onPause()");
92 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
93 debug("flow", "TransactionListFragment.onActivityCreated called");
94 super.onActivityCreated(savedInstanceState);
96 Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
97 this::onBackgroundTaskRunningChanged);
99 MainActivity mainActivity = getMainActivity();
101 swiper = mainActivity.findViewById(R.id.transaction_swipe);
103 throw new RuntimeException("Can't get hold on the swipe layout");
104 root = mainActivity.findViewById(R.id.transaction_root);
106 throw new RuntimeException("Can't get hold on the transaction value view");
107 modelAdapter = new TransactionListAdapter();
108 root.setAdapter(modelAdapter);
110 mainActivity.fabShouldShow();
114 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
116 llm.setOrientation(RecyclerView.VERTICAL);
117 root.setLayoutManager(llm);
119 swiper.setOnRefreshListener(() -> {
120 debug("ui", "refreshing transactions via swipe");
121 Data.scheduleTransactionListRetrieval(mainActivity);
124 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
126 vAccountFilter = mainActivity.findViewById(R.id.transaction_list_account_name_filter);
127 accNameFilter = mainActivity.findViewById(R.id.transaction_filter_account_name);
129 MLDB.hookAutocompletionAdapter(mainActivity, accNameFilter, "accounts", "name");
130 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
131 // debug("tmp", "direct onItemClick");
132 Cursor c = (Cursor) parent.getItemAtPosition(position);
133 Data.accountFilter.setValue(c.getString(1));
134 Globals.hideSoftKeyboard(mainActivity);
137 Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
139 TransactionListViewModel.updating.addObserver(
140 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
141 TransactionListViewModel.updateError.addObserver((o, arg) -> {
142 String err = TransactionListViewModel.updateError.get();
146 Snackbar.make(this.root, err, Snackbar.LENGTH_LONG)
148 TransactionListViewModel.updateError.set(null);
150 Data.transactions.addObserver(
151 (o, arg) -> mainActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
153 mainActivity.findViewById(R.id.clearAccountNameFilter)
154 .setOnClickListener(v -> {
155 Data.accountFilter.setValue(null);
156 vAccountFilter.setVisibility(View.GONE);
157 menuTransactionListFilter.setVisible(true);
158 Globals.hideSoftKeyboard(mainActivity);
161 Data.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
162 Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
164 root.scrollToPosition(pos);
165 // reset the value to avoid re-notification upon reconfiguration or app restart
166 Data.foundTransactionItemIndex.setValue(null);
170 private void onAccountNameFilterChanged(String accName) {
171 final String fieldText = accNameFilter.getText()
173 if ((accName == null) && (fieldText.equals("")))
176 if (accNameFilter != null) {
177 accNameFilter.setText(accName, false);
179 final boolean filterActive = (accName != null) && !accName.isEmpty();
180 if (vAccountFilter != null) {
181 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
183 if (menuTransactionListFilter != null)
184 menuTransactionListFilter.setVisible(!filterActive);
186 TransactionListViewModel.scheduleTransactionListReload();
190 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
191 inflater.inflate(R.menu.transaction_list, menu);
193 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
194 if ((menuTransactionListFilter == null))
195 throw new AssertionError();
197 if ((Data.accountFilter.getValue() != null) ||
198 (vAccountFilter.getVisibility() == View.VISIBLE))
200 menuTransactionListFilter.setVisible(false);
203 super.onCreateOptionsMenu(menu, inflater);
205 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
206 vAccountFilter.setVisibility(View.VISIBLE);
207 if (menuTransactionListFilter != null)
208 menuTransactionListFilter.setVisible(false);
209 accNameFilter.requestFocus();
210 InputMethodManager imm =
211 (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
212 imm.showSoftInput(accNameFilter, 0);
217 menu.findItem(R.id.menu_go_to_date)
218 .setOnMenuItemClickListener(item -> {
219 DatePickerFragment picker = new DatePickerFragment();
220 picker.setOnDatePickedListener(this);
221 picker.setDateRange(Data.earliestTransactionDate.getValue(),
222 Data.latestTransactionDate.getValue());
223 picker.show(requireActivity().getSupportFragmentManager(), null);
228 public void onDatePicked(int year, int month, int day) {
229 RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
230 AsyncTask<SimpleDate, Void, Integer> finder = new TransactionDateFinder();
232 finder.execute(new SimpleDate(year, month + 1, day));