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;
31 import android.widget.Toast;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
36 import net.ktnx.mobileledger.ui.activity.MainActivity;
37 import net.ktnx.mobileledger.utils.Colors;
38 import net.ktnx.mobileledger.utils.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
41 import org.jetbrains.annotations.NotNull;
43 import androidx.annotation.NonNull;
44 import androidx.annotation.Nullable;
45 import androidx.recyclerview.widget.LinearLayoutManager;
47 import static android.content.Context.INPUT_METHOD_SERVICE;
48 import static net.ktnx.mobileledger.utils.Logger.debug;
50 public class TransactionListFragment extends MobileLedgerListFragment {
51 private MenuItem menuTransactionListFilter;
52 private View vAccountFilter;
53 private AutoCompleteTextView accNameFilter;
55 public void onCreate(@Nullable Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57 setHasOptionsMenu(true);
58 Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
61 public void onAttach(@NotNull Context context) {
62 super.onAttach(context);
63 mActivity = (MainActivity) context;
67 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
68 @Nullable Bundle savedInstanceState) {
69 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
72 public void onResume() {
74 debug("flow", "TransactionListFragment.onResume()");
77 public void onStop() {
79 debug("flow", "TransactionListFragment.onStop()");
82 public void onPause() {
84 debug("flow", "TransactionListFragment.onPause()");
87 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
88 debug("flow", "TransactionListFragment.onActivityCreated called");
89 super.onActivityCreated(savedInstanceState);
91 swiper = mActivity.findViewById(R.id.transaction_swipe);
92 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
93 root = mActivity.findViewById(R.id.transaction_root);
95 throw new RuntimeException("Can't get hold on the transaction value view");
96 modelAdapter = new TransactionListAdapter();
97 root.setAdapter(modelAdapter);
99 mActivity.fabShouldShow();
103 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
105 llm.setOrientation(LinearLayoutManager.VERTICAL);
106 root.setLayoutManager(llm);
108 swiper.setOnRefreshListener(() -> {
109 debug("ui", "refreshing transactions via swipe");
110 mActivity.scheduleTransactionListRetrieval();
113 Colors.themeWatch.observe(this, this::themeChanged);
115 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
116 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
118 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
119 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
120 // debug("tmp", "direct onItemClick");
121 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
122 Data.accountFilter.setValue(mc.getString(1));
123 Globals.hideSoftKeyboard(mActivity);
126 Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
128 TransactionListViewModel.updating.addObserver(
129 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
130 TransactionListViewModel.updateError.addObserver((o, arg) -> {
131 String err = TransactionListViewModel.updateError.get();
132 if (err == null) return;
134 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
135 TransactionListViewModel.updateError.set(null);
137 Data.transactions.addObserver(
138 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
140 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
141 Data.accountFilter.setValue(null);
142 vAccountFilter.setVisibility(View.GONE);
143 menuTransactionListFilter.setVisible(true);
144 Globals.hideSoftKeyboard(mActivity);
147 private void onAccountNameFilterChanged(String accName) {
148 final String fieldText = accNameFilter.getText().toString();
149 if ((accName == null) && (fieldText.equals(""))) return;
151 if (accNameFilter != null) {
152 accNameFilter.setText(accName, false);
154 final boolean filterActive = (accName != null) && !accName.isEmpty();
155 if (vAccountFilter != null) {
156 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
158 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
160 TransactionListViewModel.scheduleTransactionListReload();
164 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
165 inflater.inflate(R.menu.transaction_list, menu);
167 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
168 if ((menuTransactionListFilter == null)) throw new AssertionError();
170 if (Data.accountFilter.getValue() != null) {
171 menuTransactionListFilter.setVisible(false);
174 super.onCreateOptionsMenu(menu, inflater);
176 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
177 vAccountFilter.setVisibility(View.VISIBLE);
178 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
179 accNameFilter.requestFocus();
180 InputMethodManager imm =
181 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
182 imm.showSoftInput(accNameFilter, 0);