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 androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
41 import net.ktnx.mobileledger.ui.activity.MainActivity;
42 import net.ktnx.mobileledger.utils.Colors;
43 import net.ktnx.mobileledger.utils.Globals;
44 import net.ktnx.mobileledger.utils.MLDB;
46 import org.jetbrains.annotations.NotNull;
48 import static android.content.Context.INPUT_METHOD_SERVICE;
49 import static net.ktnx.mobileledger.utils.Logger.debug;
51 public class TransactionListFragment extends MobileLedgerListFragment {
52 private MenuItem menuTransactionListFilter;
53 private View vAccountFilter;
54 private AutoCompleteTextView accNameFilter;
56 public void onCreate(@Nullable Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 setHasOptionsMenu(true);
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 Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
92 this::onBackgroundTaskRunningChanged);
94 swiper = mActivity.findViewById(R.id.transaction_swipe);
96 throw new RuntimeException("Can't get hold on the swipe layout");
97 root = mActivity.findViewById(R.id.transaction_root);
99 throw new RuntimeException("Can't get hold on the transaction value view");
100 modelAdapter = new TransactionListAdapter();
101 root.setAdapter(modelAdapter);
103 mActivity.fabShouldShow();
107 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
109 llm.setOrientation(RecyclerView.VERTICAL);
110 root.setLayoutManager(llm);
112 swiper.setOnRefreshListener(() -> {
113 debug("ui", "refreshing transactions via swipe");
114 Data.scheduleTransactionListRetrieval(mActivity);
117 Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
119 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
120 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
122 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
123 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
124 // debug("tmp", "direct onItemClick");
125 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
126 Data.accountFilter.setValue(mc.getString(1));
127 Globals.hideSoftKeyboard(mActivity);
130 Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
132 TransactionListViewModel.updating.addObserver(
133 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
134 TransactionListViewModel.updateError.addObserver((o, arg) -> {
135 String err = TransactionListViewModel.updateError.get();
139 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT)
141 TransactionListViewModel.updateError.set(null);
143 Data.transactions.addObserver(
144 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
146 mActivity.findViewById(R.id.clearAccountNameFilter)
147 .setOnClickListener(v -> {
148 Data.accountFilter.setValue(null);
149 vAccountFilter.setVisibility(View.GONE);
150 menuTransactionListFilter.setVisible(true);
151 Globals.hideSoftKeyboard(mActivity);
154 private void onAccountNameFilterChanged(String accName) {
155 final String fieldText = accNameFilter.getText()
157 if ((accName == null) && (fieldText.equals("")))
160 if (accNameFilter != null) {
161 accNameFilter.setText(accName, false);
163 final boolean filterActive = (accName != null) && !accName.isEmpty();
164 if (vAccountFilter != null) {
165 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
167 if (menuTransactionListFilter != null)
168 menuTransactionListFilter.setVisible(!filterActive);
170 TransactionListViewModel.scheduleTransactionListReload();
174 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
175 inflater.inflate(R.menu.transaction_list, menu);
177 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
178 if ((menuTransactionListFilter == null))
179 throw new AssertionError();
181 if ((Data.accountFilter.getValue() != null) ||
182 (vAccountFilter.getVisibility() == View.VISIBLE))
184 menuTransactionListFilter.setVisible(false);
187 super.onCreateOptionsMenu(menu, inflater);
189 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
190 vAccountFilter.setVisibility(View.VISIBLE);
191 if (menuTransactionListFilter != null)
192 menuTransactionListFilter.setVisible(false);
193 accNameFilter.requestFocus();
194 InputMethodManager imm =
195 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
196 imm.showSoftInput(accNameFilter, 0);