2 * Copyright © 2019 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.content.Context;
21 import android.database.MatrixCursor;
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.model.Data;
41 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
42 import net.ktnx.mobileledger.ui.activity.MainActivity;
43 import net.ktnx.mobileledger.utils.Colors;
44 import net.ktnx.mobileledger.utils.Globals;
45 import net.ktnx.mobileledger.utils.MLDB;
47 import org.jetbrains.annotations.NotNull;
49 import static android.content.Context.INPUT_METHOD_SERVICE;
50 import static net.ktnx.mobileledger.utils.Logger.debug;
52 // TODO: support transaction-level comment
54 public class TransactionListFragment extends MobileLedgerListFragment {
55 private MenuItem menuTransactionListFilter;
56 private View vAccountFilter;
57 private AutoCompleteTextView accNameFilter;
59 public void onCreate(@Nullable Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 setHasOptionsMenu(true);
64 public void onAttach(@NotNull Context context) {
65 super.onAttach(context);
66 mActivity = (MainActivity) context;
70 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
71 @Nullable Bundle savedInstanceState) {
72 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
75 public void onResume() {
77 debug("flow", "TransactionListFragment.onResume()");
80 public void onStop() {
82 debug("flow", "TransactionListFragment.onStop()");
85 public void onPause() {
87 debug("flow", "TransactionListFragment.onPause()");
90 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
91 debug("flow", "TransactionListFragment.onActivityCreated called");
92 super.onActivityCreated(savedInstanceState);
94 Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
95 this::onBackgroundTaskRunningChanged);
97 swiper = mActivity.findViewById(R.id.transaction_swipe);
99 throw new RuntimeException("Can't get hold on the swipe layout");
100 root = mActivity.findViewById(R.id.transaction_root);
102 throw new RuntimeException("Can't get hold on the transaction value view");
103 modelAdapter = new TransactionListAdapter();
104 root.setAdapter(modelAdapter);
106 mActivity.fabShouldShow();
110 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
112 llm.setOrientation(RecyclerView.VERTICAL);
113 root.setLayoutManager(llm);
115 swiper.setOnRefreshListener(() -> {
116 debug("ui", "refreshing transactions via swipe");
117 Data.scheduleTransactionListRetrieval(mActivity);
120 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
122 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
123 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
125 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
126 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
127 // debug("tmp", "direct onItemClick");
128 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
129 Data.accountFilter.setValue(mc.getString(1));
130 Globals.hideSoftKeyboard(mActivity);
133 Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
135 TransactionListViewModel.updating.addObserver(
136 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
137 TransactionListViewModel.updateError.addObserver((o, arg) -> {
138 String err = TransactionListViewModel.updateError.get();
142 Snackbar.make(this.root, err, Snackbar.LENGTH_LONG)
144 TransactionListViewModel.updateError.set(null);
146 Data.transactions.addObserver(
147 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
149 mActivity.findViewById(R.id.clearAccountNameFilter)
150 .setOnClickListener(v -> {
151 Data.accountFilter.setValue(null);
152 vAccountFilter.setVisibility(View.GONE);
153 menuTransactionListFilter.setVisible(true);
154 Globals.hideSoftKeyboard(mActivity);
157 private void onAccountNameFilterChanged(String accName) {
158 final String fieldText = accNameFilter.getText()
160 if ((accName == null) && (fieldText.equals("")))
163 if (accNameFilter != null) {
164 accNameFilter.setText(accName, false);
166 final boolean filterActive = (accName != null) && !accName.isEmpty();
167 if (vAccountFilter != null) {
168 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
170 if (menuTransactionListFilter != null)
171 menuTransactionListFilter.setVisible(!filterActive);
173 TransactionListViewModel.scheduleTransactionListReload();
177 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
178 inflater.inflate(R.menu.transaction_list, menu);
180 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
181 if ((menuTransactionListFilter == null))
182 throw new AssertionError();
184 if ((Data.accountFilter.getValue() != null) ||
185 (vAccountFilter.getVisibility() == View.VISIBLE))
187 menuTransactionListFilter.setVisible(false);
190 super.onCreateOptionsMenu(menu, inflater);
192 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
193 vAccountFilter.setVisibility(View.VISIBLE);
194 if (menuTransactionListFilter != null)
195 menuTransactionListFilter.setVisible(false);
196 accNameFilter.requestFocus();
197 InputMethodManager imm =
198 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
199 imm.showSoftInput(accNameFilter, 0);