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 androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import com.google.android.material.floatingactionbutton.FloatingActionButton;
26 import androidx.recyclerview.widget.LinearLayoutManager;
27 import androidx.recyclerview.widget.RecyclerView;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.view.inputmethod.InputMethodManager;
36 import android.widget.AutoCompleteTextView;
37 import android.widget.Toast;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
42 import net.ktnx.mobileledger.ui.activity.MainActivity;
43 import net.ktnx.mobileledger.utils.Colors;
44 import net.ktnx.mobileledger.utils.Globals;
45 import net.ktnx.mobileledger.utils.MLDB;
46 import net.ktnx.mobileledger.utils.ObservableValue;
48 import java.util.Observable;
49 import java.util.Observer;
51 import static android.content.Context.INPUT_METHOD_SERVICE;
53 public class TransactionListFragment extends MobileLedgerListFragment {
54 public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name";
55 public static ObservableValue<String> accountFilter = new ObservableValue<>();
56 private String mShowOnlyAccountName;
57 private MenuItem menuTransactionListFilter;
58 private View vAccountFilter;
59 private AutoCompleteTextView accNameFilter;
60 private Observer backgroundTaskCountObserver;
61 private static void update(Observable o, Object arg) {
64 public void onDestroy() {
65 if (backgroundTaskCountObserver != null) {
66 Log.d("rtl", "destroying background task count observer");
67 Data.backgroundTaskCount.deleteObserver(backgroundTaskCountObserver);
71 public void setShowOnlyAccountName(String mShowOnlyAccountName) {
72 this.mShowOnlyAccountName = mShowOnlyAccountName;
73 if (modelAdapter != null) {
74 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
76 if (accNameFilter != null) {
77 accNameFilter.setText(mShowOnlyAccountName, false);
79 if (vAccountFilter != null) {
80 vAccountFilter.setVisibility(
81 ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
82 ? View.VISIBLE : View.GONE);
86 public void setArguments(@Nullable Bundle args) {
87 super.setArguments(args);
88 mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
91 public void onCreate(@Nullable Bundle savedInstanceState) {
92 super.onCreate(savedInstanceState);
93 setHasOptionsMenu(true);
94 if (backgroundTaskCountObserver == null) {
95 Log.d("rtl", "creating background task count observer");
96 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
98 public void update(Observable o, Object arg) {
99 mActivity.runOnUiThread(() -> {
100 int cnt = Data.backgroundTaskCount.get();
101 Log.d("trl", String.format("background task count changed to %d", cnt));
102 swiper.setRefreshing(cnt > 0);
109 public void onAttach(Context context) {
110 super.onAttach(context);
111 mActivity = (MainActivity) context;
115 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
116 @Nullable Bundle savedInstanceState) {
117 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
120 public void onResume() {
122 Log.d("flow", "TransactionListFragment.onResume()");
125 public void onStop() {
127 Log.d("flow", "TransactionListFragment.onStop()");
130 public void onPause() {
132 Log.d("flow", "TransactionListFragment.onPause()");
135 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
136 Log.d("flow", "TransactionListFragment.onActivityCreated called");
137 super.onActivityCreated(savedInstanceState);
139 swiper = mActivity.findViewById(R.id.transaction_swipe);
140 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
141 root = mActivity.findViewById(R.id.transaction_root);
143 throw new RuntimeException("Can't get hold on the transaction value view");
144 modelAdapter = new TransactionListAdapter();
146 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
147 root.setAdapter(modelAdapter);
149 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
151 mActivity.fabShouldShow();
152 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
154 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
155 if (dy < 0) mActivity.fabShouldShow();
156 if (dy > 0) fab.hide();
160 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
162 llm.setOrientation(LinearLayoutManager.VERTICAL);
163 root.setLayoutManager(llm);
165 swiper.setOnRefreshListener(() -> {
166 Log.d("ui", "refreshing transactions via swipe");
167 mActivity.scheduleTransactionListRetrieval();
170 Colors.themeWatch.addObserver(
171 (o, arg) -> swiper.setColorSchemeColors(Colors.primary));
172 swiper.setColorSchemeColors(Colors.primary);
174 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
175 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
177 TransactionListFragment me = this;
178 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
179 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
180 // Log.d("tmp", "direct onItemClick");
181 TransactionListViewModel.scheduleTransactionListReload();
182 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
183 accountFilter.set(mc.getString(1));
184 Globals.hideSoftKeyboard(mActivity);
187 accountFilter.addObserver((o, arg) -> {
188 String accountName = accountFilter.get();
189 modelAdapter.setBoldAccountName(accountName);
190 setShowOnlyAccountName(accountName);
191 TransactionListViewModel.scheduleTransactionListReload();
192 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
195 Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(() -> {
196 Log.d("transactions", "requesting list reload");
197 TransactionListViewModel.scheduleTransactionListReload();
200 TransactionListViewModel.scheduleTransactionListReload();
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);