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;
43 import net.ktnx.mobileledger.utils.ObservableValue;
45 import java.util.Observable;
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 public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name";
57 public static ObservableValue<String> accountFilter = new ObservableValue<>();
58 private String mShowOnlyAccountName;
59 private MenuItem menuTransactionListFilter;
60 private View vAccountFilter;
61 private AutoCompleteTextView accNameFilter;
62 private Observer backgroundTaskCountObserver;
63 private Observer accountFilterObserver;
64 private static void update(Observable o, Object arg) {
67 public void onDestroy() {
68 if (backgroundTaskCountObserver != null) {
69 Log.d("rtl", "destroying background task count observer");
70 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
74 public void setShowOnlyAccountName(String mShowOnlyAccountName) {
75 this.mShowOnlyAccountName = mShowOnlyAccountName;
76 if (modelAdapter != null) {
77 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
79 if (accNameFilter != null) {
80 accNameFilter.setText(mShowOnlyAccountName, false);
82 if (vAccountFilter != null) {
83 vAccountFilter.setVisibility(
84 ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
85 ? View.VISIBLE : View.GONE);
89 public void setArguments(@Nullable Bundle args) {
90 super.setArguments(args);
91 mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
94 public void onCreate(@Nullable Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
96 setHasOptionsMenu(true);
97 if (backgroundTaskCountObserver == null) {
98 Log.d("rtl", "creating background task count observer");
99 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
101 public void update(Observable o, Object arg) {
102 mActivity.runOnUiThread(() -> {
103 int cnt = Data.backgroundTaskCount.get();
104 Log.d("trl", String.format("background task count changed to %d", cnt));
105 swiper.setRefreshing(cnt > 0);
112 public void onAttach(Context context) {
113 super.onAttach(context);
114 mActivity = (MainActivity) context;
118 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
119 @Nullable Bundle savedInstanceState) {
120 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
123 public void onResume() {
125 Log.d("flow", "TransactionListFragment.onResume()");
128 public void onStop() {
130 Log.d("flow", "TransactionListFragment.onStop()");
133 public void onPause() {
135 Log.d("flow", "TransactionListFragment.onPause()");
138 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
139 Log.d("flow", "TransactionListFragment.onActivityCreated called");
140 super.onActivityCreated(savedInstanceState);
142 swiper = mActivity.findViewById(R.id.transaction_swipe);
143 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
144 root = mActivity.findViewById(R.id.transaction_root);
146 throw new RuntimeException("Can't get hold on the transaction value view");
147 modelAdapter = new TransactionListAdapter();
149 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
150 root.setAdapter(modelAdapter);
152 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
154 mActivity.fabShouldShow();
155 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
157 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
158 if (dy < 0) mActivity.fabShouldShow();
159 if (dy > 0) fab.hide();
163 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
165 llm.setOrientation(LinearLayoutManager.VERTICAL);
166 root.setLayoutManager(llm);
168 swiper.setOnRefreshListener(() -> {
169 Log.d("ui", "refreshing transactions via swipe");
170 mActivity.scheduleTransactionListRetrieval();
173 Colors.themeWatch.addObserver(
174 (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
175 swiper.setColorSchemeColors(Colors.primary);
177 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
178 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
180 TransactionListFragment me = this;
181 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
182 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
183 // Log.d("tmp", "direct onItemClick");
184 TransactionListViewModel.scheduleTransactionListReload();
185 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
186 accountFilter.set(mc.getString(1));
187 Globals.hideSoftKeyboard(mActivity);
190 if (accountFilterObserver == null) {
191 accountFilterObserver = (o, arg) -> {
192 String accountName = accountFilter.get();
193 modelAdapter.setBoldAccountName(accountName);
194 setShowOnlyAccountName(accountName);
195 TransactionListViewModel.scheduleTransactionListReload();
196 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
198 accountFilter.addObserver(accountFilterObserver);
201 TransactionListViewModel.updating.addObserver(
202 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
203 TransactionListViewModel.updateError.addObserver(new Observer() {
205 public void update(Observable o, Object arg) {
206 String err = TransactionListViewModel.updateError.get();
207 if (err == null) return;
209 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
210 TransactionListViewModel.updateError.set(null);
213 Data.transactions.addObserver(
214 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
216 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
217 vAccountFilter.setVisibility(View.GONE);
218 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
219 accountFilter.set(null);
220 accNameFilter.setText(null);
221 TransactionListViewModel.scheduleTransactionListReload();
222 Globals.hideSoftKeyboard(mActivity);
226 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
227 inflater.inflate(R.menu.transaction_list, menu);
229 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
230 if ((menuTransactionListFilter == null)) throw new AssertionError();
232 if (mShowOnlyAccountName != null) {
233 menuTransactionListFilter.setVisible(false);
236 super.onCreateOptionsMenu(menu, inflater);
238 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
239 vAccountFilter.setVisibility(View.VISIBLE);
240 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
241 accNameFilter.requestFocus();
242 InputMethodManager imm =
243 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
244 imm.showSoftInput(accNameFilter, 0);