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;
59 private Observer accountFilterObserver;
60 private static void update(Observable o, Object arg) {
63 public void onDestroy() {
64 if (backgroundTaskCountObserver != null) {
65 Log.d("rtl", "destroying background task count observer");
66 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
70 public void setShowOnlyAccountName(String mShowOnlyAccountName) {
73 public void onCreate(@Nullable Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
75 setHasOptionsMenu(true);
76 if (backgroundTaskCountObserver == null) {
77 Log.d("rtl", "creating background task count observer");
78 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
80 public void update(Observable o, Object arg) {
81 mActivity.runOnUiThread(() -> {
82 int cnt = Data.backgroundTaskCount.get();
83 Log.d("trl", String.format("background task count changed to %d", cnt));
84 swiper.setRefreshing(cnt > 0);
91 public void onAttach(Context context) {
92 super.onAttach(context);
93 mActivity = (MainActivity) context;
97 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
98 @Nullable Bundle savedInstanceState) {
99 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
102 public void onResume() {
104 Log.d("flow", "TransactionListFragment.onResume()");
107 public void onStop() {
109 Log.d("flow", "TransactionListFragment.onStop()");
112 public void onPause() {
114 Log.d("flow", "TransactionListFragment.onPause()");
117 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
118 Log.d("flow", "TransactionListFragment.onActivityCreated called");
119 super.onActivityCreated(savedInstanceState);
121 swiper = mActivity.findViewById(R.id.transaction_swipe);
122 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
123 root = mActivity.findViewById(R.id.transaction_root);
125 throw new RuntimeException("Can't get hold on the transaction value view");
126 modelAdapter = new TransactionListAdapter();
127 root.setAdapter(modelAdapter);
129 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
131 mActivity.fabShouldShow();
132 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
134 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
135 if (dy < 0) mActivity.fabShouldShow();
136 if (dy > 0) fab.hide();
140 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
142 llm.setOrientation(LinearLayoutManager.VERTICAL);
143 root.setLayoutManager(llm);
145 swiper.setOnRefreshListener(() -> {
146 Log.d("ui", "refreshing transactions via swipe");
147 mActivity.scheduleTransactionListRetrieval();
150 Colors.themeWatch.addObserver((o, arg) -> swiper.setColorSchemeColors(Colors.primary));
151 swiper.setColorSchemeColors(Colors.primary);
153 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
154 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
156 TransactionListFragment me = this;
157 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
158 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
159 // Log.d("tmp", "direct onItemClick");
160 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
161 Data.accountFilter.set(mc.getString(1));
162 Globals.hideSoftKeyboard(mActivity);
165 if (accountFilterObserver == null) {
166 accountFilterObserver = (o, arg) -> onAccountNameFilterChanged();
167 Data.accountFilter.addObserver(accountFilterObserver);
170 TransactionListViewModel.updating.addObserver(
171 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
172 TransactionListViewModel.updateError.addObserver(new Observer() {
174 public void update(Observable o, Object arg) {
175 String err = TransactionListViewModel.updateError.get();
176 if (err == null) return;
178 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
179 TransactionListViewModel.updateError.set(null);
182 Data.transactions.addObserver(
183 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
185 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
186 String current = Data.accountFilter.get();
187 Data.accountFilter.set(null);
188 vAccountFilter.setVisibility(View.GONE);
189 menuTransactionListFilter.setVisible(true);
190 Globals.hideSoftKeyboard(mActivity);
193 onAccountNameFilterChanged(false);
195 private void onAccountNameFilterChanged() {
196 onAccountNameFilterChanged(true);
198 private void onAccountNameFilterChanged(boolean reloadTransactions) {
199 String accName = Data.accountFilter.get();
200 if (accNameFilter != null) {
201 accNameFilter.setText(accName, false);
203 final boolean filterActive = (accName != null) && !accName.isEmpty();
204 if (vAccountFilter != null) {
205 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
207 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
209 if (reloadTransactions) TransactionListViewModel.scheduleTransactionListReload();
213 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
214 inflater.inflate(R.menu.transaction_list, menu);
216 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
217 if ((menuTransactionListFilter == null)) throw new AssertionError();
219 if (Data.accountFilter.get() != null) {
220 menuTransactionListFilter.setVisible(false);
223 super.onCreateOptionsMenu(menu, inflater);
225 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
226 vAccountFilter.setVisibility(View.VISIBLE);
227 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
228 accNameFilter.requestFocus();
229 InputMethodManager imm =
230 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
231 imm.showSoftInput(accNameFilter, 0);