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.support.annotation.NonNull;
24 import android.support.annotation.Nullable;
25 import android.support.design.widget.FloatingActionButton;
26 import android.support.v7.widget.LinearLayoutManager;
27 import android.support.v7.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.Globals;
44 import net.ktnx.mobileledger.utils.MLDB;
45 import net.ktnx.mobileledger.utils.ObservableValue;
47 import java.util.Observable;
48 import java.util.Observer;
50 import static android.content.Context.INPUT_METHOD_SERVICE;
52 public class TransactionListFragment extends MobileLedgerListFragment {
53 public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name";
54 public static ObservableValue<String> accountFilter = new ObservableValue<>();
55 private String mShowOnlyAccountName;
56 private MenuItem menuTransactionListFilter;
57 private View vAccountFilter;
58 private AutoCompleteTextView accNameFilter;
59 private Observer backgroundTaskCountObserver;
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) {
71 this.mShowOnlyAccountName = mShowOnlyAccountName;
72 if (modelAdapter != null) {
73 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
75 if (accNameFilter != null) {
76 accNameFilter.setText(mShowOnlyAccountName, false);
78 if (vAccountFilter != null) {
79 vAccountFilter.setVisibility(
80 ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
81 ? View.VISIBLE : View.GONE);
85 public void setArguments(@Nullable Bundle args) {
86 super.setArguments(args);
87 mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
90 public void onCreate(@Nullable Bundle savedInstanceState) {
91 super.onCreate(savedInstanceState);
92 setHasOptionsMenu(true);
93 if (backgroundTaskCountObserver == null) {
94 Log.d("rtl", "creating background task count observer");
95 Data.backgroundTaskCount.addObserver(backgroundTaskCountObserver = new Observer() {
97 public void update(Observable o, Object arg) {
98 mActivity.runOnUiThread(() -> {
99 int cnt = Data.backgroundTaskCount.get();
100 Log.d("trl", String.format("background task count changed to %d", cnt));
101 swiper.setRefreshing(cnt > 0);
108 public void onAttach(Context context) {
109 super.onAttach(context);
110 mActivity = (MainActivity) context;
114 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
115 @Nullable Bundle savedInstanceState) {
116 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
119 public void onResume() {
121 Log.d("flow", "TransactionListFragment.onResume()");
124 public void onStop() {
126 Log.d("flow", "TransactionListFragment.onStop()");
129 public void onPause() {
131 Log.d("flow", "TransactionListFragment.onPause()");
134 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
135 Log.d("flow", "TransactionListFragment.onActivityCreated called");
136 super.onActivityCreated(savedInstanceState);
138 swiper = mActivity.findViewById(R.id.transaction_swipe);
139 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
140 root = mActivity.findViewById(R.id.transaction_root);
142 throw new RuntimeException("Can't get hold on the transaction value view");
143 modelAdapter = new TransactionListAdapter();
145 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
146 root.setAdapter(modelAdapter);
148 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
150 mActivity.fabShouldShow();
151 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
153 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
154 if (dy < 0) mActivity.fabShouldShow();
155 if (dy > 0) fab.hide();
159 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
161 llm.setOrientation(LinearLayoutManager.VERTICAL);
162 root.setLayoutManager(llm);
164 swiper.setOnRefreshListener(() -> {
165 Log.d("ui", "refreshing transactions via swipe");
166 mActivity.scheduleTransactionListRetrieval();
169 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
171 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
172 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
174 TransactionListFragment me = this;
175 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
176 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
177 // Log.d("tmp", "direct onItemClick");
178 TransactionListViewModel.scheduleTransactionListReload();
179 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
180 accountFilter.set(mc.getString(1));
181 Globals.hideSoftKeyboard(mActivity);
184 accountFilter.addObserver((o, arg) -> {
185 String accountName = accountFilter.get();
186 modelAdapter.setBoldAccountName(accountName);
187 setShowOnlyAccountName(accountName);
188 TransactionListViewModel.scheduleTransactionListReload();
189 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
192 Data.profile.addObserver((o, arg) -> mActivity.runOnUiThread(() -> {
193 Log.d("transactions", "requesting list reload");
194 TransactionListViewModel.scheduleTransactionListReload();
197 TransactionListViewModel.scheduleTransactionListReload();
198 TransactionListViewModel.updating.addObserver(
199 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
200 TransactionListViewModel.updateError.addObserver(new Observer() {
202 public void update(Observable o, Object arg) {
203 String err = TransactionListViewModel.updateError.get();
204 if (err == null) return;
206 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
207 TransactionListViewModel.updateError.set(null);
210 Data.transactions.addObserver(
211 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
213 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
214 vAccountFilter.setVisibility(View.GONE);
215 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
216 accountFilter.set(null);
217 accNameFilter.setText(null);
218 TransactionListViewModel.scheduleTransactionListReload();
219 Globals.hideSoftKeyboard(mActivity);
223 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
224 inflater.inflate(R.menu.transaction_list, menu);
226 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
227 if ((menuTransactionListFilter == null)) throw new AssertionError();
229 if (mShowOnlyAccountName != null) {
230 menuTransactionListFilter.setVisible(false);
233 super.onCreateOptionsMenu(menu, inflater);
235 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
236 vAccountFilter.setVisibility(View.VISIBLE);
237 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
238 accNameFilter.requestFocus();
239 InputMethodManager imm =
240 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
241 imm.showSoftInput(accNameFilter, 0);