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 java.util.Observable;
45 import java.util.Observer;
47 import androidx.annotation.NonNull;
48 import androidx.annotation.Nullable;
49 import androidx.recyclerview.widget.LinearLayoutManager;
50 import androidx.recyclerview.widget.RecyclerView;
52 import static android.content.Context.INPUT_METHOD_SERVICE;
54 public class TransactionListFragment extends MobileLedgerListFragment {
55 private MenuItem menuTransactionListFilter;
56 private View vAccountFilter;
57 private AutoCompleteTextView accNameFilter;
58 private Observer backgroundTaskCountObserver;
60 public void onDestroy() {
61 if (backgroundTaskCountObserver != null) {
62 Log.d("rtl", "destroying background task count observer");
63 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
68 public void onCreate(@Nullable Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70 setHasOptionsMenu(true);
71 if (backgroundTaskCountObserver == null) {
72 Log.d("rtl", "creating background task count observer");
73 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
75 public void update(Observable o, Object arg) {
76 mActivity.runOnUiThread(() -> {
77 int cnt = Data.backgroundTaskCount.get();
78 Log.d("trl", String.format("background task count changed to %d", cnt));
79 swiper.setRefreshing(cnt > 0);
86 public void onAttach(Context context) {
87 super.onAttach(context);
88 mActivity = (MainActivity) context;
92 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
93 @Nullable Bundle savedInstanceState) {
94 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
97 public void onResume() {
99 Log.d("flow", "TransactionListFragment.onResume()");
102 public void onStop() {
104 Log.d("flow", "TransactionListFragment.onStop()");
107 public void onPause() {
109 Log.d("flow", "TransactionListFragment.onPause()");
112 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
113 Log.d("flow", "TransactionListFragment.onActivityCreated called");
114 super.onActivityCreated(savedInstanceState);
116 swiper = mActivity.findViewById(R.id.transaction_swipe);
117 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
118 root = mActivity.findViewById(R.id.transaction_root);
120 throw new RuntimeException("Can't get hold on the transaction value view");
121 modelAdapter = new TransactionListAdapter();
122 root.setAdapter(modelAdapter);
124 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
126 mActivity.fabShouldShow();
127 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
129 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
130 if (dy < 0) mActivity.fabShouldShow();
131 if (dy > 0) fab.hide();
135 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
137 llm.setOrientation(LinearLayoutManager.VERTICAL);
138 root.setLayoutManager(llm);
140 swiper.setOnRefreshListener(() -> {
141 Log.d("ui", "refreshing transactions via swipe");
142 mActivity.scheduleTransactionListRetrieval();
145 Colors.themeWatch.observe(this, this::themeChanged);
146 swiper.setColorSchemeColors(Colors.primary);
148 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
149 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
151 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
152 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
153 // Log.d("tmp", "direct onItemClick");
154 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
155 Data.accountFilter.setValue(mc.getString(1));
156 Globals.hideSoftKeyboard(mActivity);
159 Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
161 TransactionListViewModel.updating.addObserver(
162 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
163 TransactionListViewModel.updateError.addObserver(new Observer() {
165 public void update(Observable o, Object arg) {
166 String err = TransactionListViewModel.updateError.get();
167 if (err == null) return;
169 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
170 TransactionListViewModel.updateError.set(null);
173 Data.transactions.addObserver(
174 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
176 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
177 Data.accountFilter.setValue(null);
178 vAccountFilter.setVisibility(View.GONE);
179 menuTransactionListFilter.setVisible(true);
180 Globals.hideSoftKeyboard(mActivity);
183 private void onAccountNameFilterChanged(String accName) {
184 if (accNameFilter != null) {
185 accNameFilter.setText(accName, false);
187 final boolean filterActive = (accName != null) && !accName.isEmpty();
188 if (vAccountFilter != null) {
189 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
191 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
193 TransactionListViewModel.scheduleTransactionListReload();
197 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
198 inflater.inflate(R.menu.transaction_list, menu);
200 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
201 if ((menuTransactionListFilter == null)) throw new AssertionError();
203 if (Data.accountFilter.getValue() != null) {
204 menuTransactionListFilter.setVisible(false);
207 super.onCreateOptionsMenu(menu, inflater);
209 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
210 vAccountFilter.setVisibility(View.VISIBLE);
211 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
212 accNameFilter.requestFocus();
213 InputMethodManager imm =
214 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
215 imm.showSoftInput(accNameFilter, 0);