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.lifecycle.ViewModelProvider;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.async.TransactionDateFinder;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.MobileLedgerProfile;
42 import net.ktnx.mobileledger.ui.DatePickerFragment;
43 import net.ktnx.mobileledger.ui.MainModel;
44 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
45 import net.ktnx.mobileledger.ui.activity.MainActivity;
46 import net.ktnx.mobileledger.utils.Colors;
47 import net.ktnx.mobileledger.utils.Globals;
48 import net.ktnx.mobileledger.utils.Logger;
49 import net.ktnx.mobileledger.utils.MLDB;
50 import net.ktnx.mobileledger.utils.SimpleDate;
52 import org.jetbrains.annotations.NotNull;
54 import java.util.Locale;
56 import static android.content.Context.INPUT_METHOD_SERVICE;
57 import static net.ktnx.mobileledger.utils.Logger.debug;
59 public class TransactionListFragment extends MobileLedgerListFragment
60 implements DatePickerFragment.DatePickedListener {
61 private MenuItem menuTransactionListFilter;
62 private View vAccountFilter;
63 private AutoCompleteTextView accNameFilter;
64 private MainModel model;
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 onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
93 debug("flow", "TransactionListFragment.onActivityCreated called");
94 super.onViewCreated(view, savedInstanceState);
96 Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
97 this::onBackgroundTaskRunningChanged);
99 MainActivity mainActivity = getMainActivity();
101 model = new ViewModelProvider(requireActivity()).get(MainModel.class);
103 refreshLayout = view.findViewById(R.id.transaction_swipe);
104 if (refreshLayout == null)
105 throw new RuntimeException("Can't get hold on the swipe layout");
106 root = view.findViewById(R.id.transaction_root);
108 throw new RuntimeException("Can't get hold on the transaction value view");
109 modelAdapter = new TransactionListAdapter(model);
110 root.setAdapter(modelAdapter);
112 mainActivity.fabShouldShow();
116 LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
118 llm.setOrientation(RecyclerView.VERTICAL);
119 root.setLayoutManager(llm);
121 refreshLayout.setOnRefreshListener(() -> {
122 debug("ui", "refreshing transactions via swipe");
123 model.scheduleTransactionListRetrieval();
126 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
128 vAccountFilter = view.findViewById(R.id.transaction_list_account_name_filter);
129 accNameFilter = view.findViewById(R.id.transaction_filter_account_name);
131 MLDB.hookAutocompletionAdapter(mainActivity, accNameFilter, "accounts", "name");
132 accNameFilter.setOnItemClickListener((parent, v, position, id) -> {
133 // debug("tmp", "direct onItemClick");
134 Cursor c = (Cursor) parent.getItemAtPosition(position);
135 model.getAccountFilter()
136 .setValue(c.getString(1));
137 Globals.hideSoftKeyboard(mainActivity);
140 model.getAccountFilter()
141 .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
143 model.getUpdatingFlag()
144 .observe(getViewLifecycleOwner(), (flag) -> refreshLayout.setRefreshing(flag));
145 MobileLedgerProfile profile = Data.getProfile();
146 model.getDisplayedTransactions()
147 .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
149 view.findViewById(R.id.clearAccountNameFilter)
150 .setOnClickListener(v -> {
151 model.getAccountFilter()
153 vAccountFilter.setVisibility(View.GONE);
154 menuTransactionListFilter.setVisible(true);
155 Globals.hideSoftKeyboard(mainActivity);
158 model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
159 Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
161 root.scrollToPosition(pos);
162 // reset the value to avoid re-notification upon reconfiguration or app restart
163 model.foundTransactionItemIndex.setValue(null);
167 private void onAccountNameFilterChanged(String accName) {
168 final String fieldText = accNameFilter.getText()
170 if ((accName == null) && (fieldText.equals("")))
173 if (accNameFilter != null) {
174 accNameFilter.setText(accName, false);
176 final boolean filterActive = (accName != null) && !accName.isEmpty();
177 if (vAccountFilter != null) {
178 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
180 if (menuTransactionListFilter != null)
181 menuTransactionListFilter.setVisible(!filterActive);
183 model.scheduleTransactionListReload();
187 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
188 inflater.inflate(R.menu.transaction_list, menu);
190 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
191 if ((menuTransactionListFilter == null))
192 throw new AssertionError();
194 if ((model.getAccountFilter()
195 .getValue() != null) || (vAccountFilter.getVisibility() == View.VISIBLE))
197 menuTransactionListFilter.setVisible(false);
200 super.onCreateOptionsMenu(menu, inflater);
202 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
203 vAccountFilter.setVisibility(View.VISIBLE);
204 if (menuTransactionListFilter != null)
205 menuTransactionListFilter.setVisible(false);
206 accNameFilter.requestFocus();
207 InputMethodManager imm =
208 (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
209 imm.showSoftInput(accNameFilter, 0);
214 menu.findItem(R.id.menu_go_to_date)
215 .setOnMenuItemClickListener(item -> {
216 DatePickerFragment picker = new DatePickerFragment();
217 picker.setOnDatePickedListener(this);
218 picker.setDateRange(model.getFirstTransactionDate(),
219 model.getLastTransactionDate());
220 picker.show(requireActivity().getSupportFragmentManager(), null);
225 public void onDatePicked(int year, int month, int day) {
226 RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
227 AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
230 new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));