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 org.jetbrains.annotations.NotNull;
46 import androidx.annotation.NonNull;
47 import androidx.annotation.Nullable;
48 import androidx.recyclerview.widget.LinearLayoutManager;
49 import androidx.recyclerview.widget.RecyclerView;
51 import static android.content.Context.INPUT_METHOD_SERVICE;
53 public class TransactionListFragment extends MobileLedgerListFragment {
54 private MenuItem menuTransactionListFilter;
55 private View vAccountFilter;
56 private AutoCompleteTextView accNameFilter;
58 public void onCreate(@Nullable Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setHasOptionsMenu(true);
61 Data.backgroundTasksRunning.observe(this, this::onBackgroundTaskRunningChanged);
64 public void onAttach(@NotNull Context context) {
65 super.onAttach(context);
66 mActivity = (MainActivity) context;
70 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
71 @Nullable Bundle savedInstanceState) {
72 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
75 public void onResume() {
77 Log.d("flow", "TransactionListFragment.onResume()");
80 public void onStop() {
82 Log.d("flow", "TransactionListFragment.onStop()");
85 public void onPause() {
87 Log.d("flow", "TransactionListFragment.onPause()");
90 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
91 Log.d("flow", "TransactionListFragment.onActivityCreated called");
92 super.onActivityCreated(savedInstanceState);
94 swiper = mActivity.findViewById(R.id.transaction_swipe);
95 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
96 root = mActivity.findViewById(R.id.transaction_root);
98 throw new RuntimeException("Can't get hold on the transaction value view");
99 modelAdapter = new TransactionListAdapter();
100 root.setAdapter(modelAdapter);
102 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
104 mActivity.fabShouldShow();
105 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
107 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
108 if (dy < 0) mActivity.fabShouldShow();
109 if (dy > 0) fab.hide();
113 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
115 llm.setOrientation(LinearLayoutManager.VERTICAL);
116 root.setLayoutManager(llm);
118 swiper.setOnRefreshListener(() -> {
119 Log.d("ui", "refreshing transactions via swipe");
120 mActivity.scheduleTransactionListRetrieval();
123 Colors.themeWatch.observe(this, this::themeChanged);
124 swiper.setColorSchemeColors(Colors.primary);
126 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
127 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
129 MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name", true);
130 accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
131 // Log.d("tmp", "direct onItemClick");
132 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
133 Data.accountFilter.setValue(mc.getString(1));
134 Globals.hideSoftKeyboard(mActivity);
137 Data.accountFilter.observe(this, this::onAccountNameFilterChanged);
139 TransactionListViewModel.updating.addObserver(
140 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
141 TransactionListViewModel.updateError.addObserver((o, arg) -> {
142 String err = TransactionListViewModel.updateError.get();
143 if (err == null) return;
145 Toast.makeText(mActivity, err, Toast.LENGTH_SHORT).show();
146 TransactionListViewModel.updateError.set(null);
148 Data.transactions.addObserver(
149 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
151 mActivity.findViewById(R.id.clearAccountNameFilter).setOnClickListener(v -> {
152 Data.accountFilter.setValue(null);
153 vAccountFilter.setVisibility(View.GONE);
154 menuTransactionListFilter.setVisible(true);
155 Globals.hideSoftKeyboard(mActivity);
158 private void onAccountNameFilterChanged(String accName) {
159 if (accNameFilter != null) {
160 accNameFilter.setText(accName, false);
162 final boolean filterActive = (accName != null) && !accName.isEmpty();
163 if (vAccountFilter != null) {
164 vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
166 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(!filterActive);
168 TransactionListViewModel.scheduleTransactionListReload();
172 public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
173 inflater.inflate(R.menu.transaction_list, menu);
175 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
176 if ((menuTransactionListFilter == null)) throw new AssertionError();
178 if (Data.accountFilter.getValue() != null) {
179 menuTransactionListFilter.setVisible(false);
182 super.onCreateOptionsMenu(menu, inflater);
184 menuTransactionListFilter.setOnMenuItemClickListener(item -> {
185 vAccountFilter.setVisibility(View.VISIBLE);
186 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
187 accNameFilter.requestFocus();
188 InputMethodManager imm =
189 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
190 imm.showSoftInput(accNameFilter, 0);