]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
edd3782578faf550a60cbae1fb1d9edf043bee07
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
20 import android.content.Context;
21 import android.database.MatrixCursor;
22 import android.os.Bundle;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.MenuItem;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.inputmethod.InputMethodManager;
30 import android.widget.AutoCompleteTextView;
31
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.recyclerview.widget.LinearLayoutManager;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import com.google.android.material.snackbar.Snackbar;
38
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
47 import org.jetbrains.annotations.NotNull;
48
49 import static android.content.Context.INPUT_METHOD_SERVICE;
50 import static net.ktnx.mobileledger.utils.Logger.debug;
51
52 // TODO: support transaction-level comment
53
54 public class TransactionListFragment extends MobileLedgerListFragment {
55     private MenuItem menuTransactionListFilter;
56     private View vAccountFilter;
57     private AutoCompleteTextView accNameFilter;
58     @Override
59     public void onCreate(@Nullable Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61         setHasOptionsMenu(true);
62     }
63     @Override
64     public void onAttach(@NotNull Context context) {
65         super.onAttach(context);
66         mActivity = (MainActivity) context;
67     }
68     @Nullable
69     @Override
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);
73     }
74     @Override
75     public void onResume() {
76         super.onResume();
77         debug("flow", "TransactionListFragment.onResume()");
78     }
79     @Override
80     public void onStop() {
81         super.onStop();
82         debug("flow", "TransactionListFragment.onStop()");
83     }
84     @Override
85     public void onPause() {
86         super.onPause();
87         debug("flow", "TransactionListFragment.onPause()");
88     }
89     @Override
90     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
91         debug("flow", "TransactionListFragment.onActivityCreated called");
92         super.onActivityCreated(savedInstanceState);
93
94         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
95                 this::onBackgroundTaskRunningChanged);
96
97         swiper = mActivity.findViewById(R.id.transaction_swipe);
98         if (swiper == null)
99             throw new RuntimeException("Can't get hold on the swipe layout");
100         root = mActivity.findViewById(R.id.transaction_root);
101         if (root == null)
102             throw new RuntimeException("Can't get hold on the transaction value view");
103         modelAdapter = new TransactionListAdapter();
104         root.setAdapter(modelAdapter);
105
106         mActivity.fabShouldShow();
107
108         manageFabOnScroll();
109
110         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
111
112         llm.setOrientation(RecyclerView.VERTICAL);
113         root.setLayoutManager(llm);
114
115         swiper.setOnRefreshListener(() -> {
116             debug("ui", "refreshing transactions via swipe");
117             Data.scheduleTransactionListRetrieval(mActivity);
118         });
119
120         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
121
122         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
123         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
124
125         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
126         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
127 //                debug("tmp", "direct onItemClick");
128             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
129             Data.accountFilter.setValue(mc.getString(1));
130             Globals.hideSoftKeyboard(mActivity);
131         });
132
133         Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
134
135         TransactionListViewModel.updating.addObserver(
136                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
137         TransactionListViewModel.updateError.addObserver((o, arg) -> {
138             String err = TransactionListViewModel.updateError.get();
139             if (err == null)
140                 return;
141
142             Snackbar.make(this.root, err, Snackbar.LENGTH_LONG)
143                     .show();
144             TransactionListViewModel.updateError.set(null);
145         });
146         Data.transactions.addObserver(
147                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
148
149         mActivity.findViewById(R.id.clearAccountNameFilter)
150                  .setOnClickListener(v -> {
151                      Data.accountFilter.setValue(null);
152                      vAccountFilter.setVisibility(View.GONE);
153                      menuTransactionListFilter.setVisible(true);
154                      Globals.hideSoftKeyboard(mActivity);
155                  });
156     }
157     private void onAccountNameFilterChanged(String accName) {
158         final String fieldText = accNameFilter.getText()
159                                               .toString();
160         if ((accName == null) && (fieldText.equals("")))
161             return;
162
163         if (accNameFilter != null) {
164             accNameFilter.setText(accName, false);
165         }
166         final boolean filterActive = (accName != null) && !accName.isEmpty();
167         if (vAccountFilter != null) {
168             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
169         }
170         if (menuTransactionListFilter != null)
171             menuTransactionListFilter.setVisible(!filterActive);
172
173         TransactionListViewModel.scheduleTransactionListReload();
174
175     }
176     @Override
177     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
178         inflater.inflate(R.menu.transaction_list, menu);
179
180         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
181         if ((menuTransactionListFilter == null))
182             throw new AssertionError();
183
184         if ((Data.accountFilter.getValue() != null) ||
185             (vAccountFilter.getVisibility() == View.VISIBLE))
186         {
187             menuTransactionListFilter.setVisible(false);
188         }
189
190         super.onCreateOptionsMenu(menu, inflater);
191
192         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
193             vAccountFilter.setVisibility(View.VISIBLE);
194             if (menuTransactionListFilter != null)
195                 menuTransactionListFilter.setVisible(false);
196             accNameFilter.requestFocus();
197             InputMethodManager imm =
198                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
199             imm.showSoftInput(accNameFilter, 0);
200
201             return true;
202         });
203     }
204 }