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.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.Menu;
26 import android.view.MenuInflater;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.inputmethod.InputMethodManager;
31 import android.widget.AutoCompleteTextView;
32 import android.widget.Toast;
34 import com.google.android.material.floatingactionbutton.FloatingActionButton;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
39 import net.ktnx.mobileledger.ui.activity.MainActivity;
40 import net.ktnx.mobileledger.utils.Colors;
41 import net.ktnx.mobileledger.utils.Globals;
42 import net.ktnx.mobileledger.utils.MLDB;
44 import org.jetbrains.annotations.NotNull;
46 import java.util.Observer;
48 import androidx.annotation.NonNull;
49 import androidx.annotation.Nullable;
50 import androidx.recyclerview.widget.LinearLayoutManager;
51 import androidx.recyclerview.widget.RecyclerView;
53 import static android.content.Context.INPUT_METHOD_SERVICE;
55 public class TransactionListFragment extends MobileLedgerListFragment {
56 private MenuItem menuTransactionListFilter;
57 private View vAccountFilter;
58 private AutoCompleteTextView accNameFilter;
59 private Observer backgroundTaskCountObserver;
61 public void onDestroy() {
62 if (backgroundTaskCountObserver != null) {
63 Log.d("rtl", "destroying background task count observer");
64 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
69 public void onCreate(@Nullable Bundle savedInstanceState) {
70 super.onCreate(savedInstanceState);
71 setHasOptionsMenu(true);
72 if (backgroundTaskCountObserver == null) {
73 Log.d("rtl", "creating background task count observer");
74 Data.backgroundTaskCount.addObserver(
75 backgroundTaskCountObserver = (o, arg) -> mActivity.runOnUiThread(() -> {
76 int cnt = Data.backgroundTaskCount.get();
77 Log.d("trl", String.format("background task count changed to %d", cnt));
78 swiper.setRefreshing(cnt > 0);
83 public void onAttach(@NotNull Context context) {
84 super.onAttach(context);
85 mActivity = (MainActivity) context;
89 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
90 @Nullable Bundle savedInstanceState) {
91 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
94 public void onResume() {
96 Log.d("flow", "TransactionListFragment.onResume()");
99 public void onStop() {
101 Log.d("flow", "TransactionListFragment.onStop()");
104 public void onPause() {
106 Log.d("flow", "TransactionListFragment.onPause()");
109 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
110 Log.d("flow", "TransactionListFragment.onActivityCreated called");
111 super.onActivityCreated(savedInstanceState);
113 swiper = mActivity.findViewById(R.id.transaction_swipe);
114 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
115 root = mActivity.findViewById(R.id.transaction_root);
117 throw new RuntimeException("Can't get hold on the transaction value view");
118 modelAdapter = new TransactionListAdapter();
119 root.setAdapter(modelAdapter);
121 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
123 mActivity.fabShouldShow();
124 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
126 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
127 if (dy < 0) mActivity.fabShouldShow();
128 if (dy > 0) fab.hide();
132 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
134 llm.setOrientation(LinearLayoutManager.VERTICAL);
135 root.setLayoutManager(llm);
137 swiper.setOnRefreshListener(() -> {
138 Log.d("ui", "refreshing transactions via swipe");
139 mActivity.scheduleTransactionListRetrieval();
142 Colors.themeWatch.observe(this, this::themeChanged);
143 swiper.setColorSchemeColors(Colors.primary);
145 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
146 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
148 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
149 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
150 // Log.d("tmp", "direct onItemClick");
151 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
152 Data.accountFilter.setValue(mc.getString(1));
153 Globals.hideSoftKeyboard(mActivity);
156 Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
158 TransactionListViewModel.updating.addObserver(
159 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
160 TransactionListViewModel.updateError.addObserver((o, arg) -> {
161 String err = TransactionListViewModel.updateError.get();
162 if (err == null) return;
164 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
165 TransactionListViewModel.updateError.set(null);
167 Data.transactions.addObserver(
168 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
170 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
171 Data.accountFilter.setValue(null);
172 vAccountFilter.setVisibility(View.GONE);
173 menuTransactionListFilter.setVisible(true);
174 Globals.hideSoftKeyboard(mActivity);
177 private void onAccountNameFilterChanged(String accName) {
178 if (accNameFilter != null) {
179 accNameFilter.setText(accName, false);
181 final boolean filterActive = (accName != null) && !accName.isEmpty();
182 if (vAccountFilter != null) {
183 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
185 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
187 TransactionListViewModel.scheduleTransactionListReload();
191 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
192 inflater.inflate(R.menu.transaction_list, menu);
194 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
195 if ((menuTransactionListFilter == null)) throw new AssertionError();
197 if (Data.accountFilter.getValue() != null) {
198 menuTransactionListFilter.setVisible(false);
201 super.onCreateOptionsMenu(menu, inflater);
203 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
204 vAccountFilter.setVisibility(View.VISIBLE);
205 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
206 accNameFilter.requestFocus();
207 InputMethodManager imm =
208 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
209 imm.showSoftInput(accNameFilter, 0);