]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
19945505c86dbb524bb529f41f759071be840c5b
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2020 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.database.Cursor;
21 import android.os.AsyncTask;
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.async.TransactionDateFinder;
41 import net.ktnx.mobileledger.model.Data;
42 import net.ktnx.mobileledger.ui.DatePickerFragment;
43 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
44 import net.ktnx.mobileledger.ui.activity.MainActivity;
45 import net.ktnx.mobileledger.utils.Colors;
46 import net.ktnx.mobileledger.utils.Globals;
47 import net.ktnx.mobileledger.utils.Logger;
48 import net.ktnx.mobileledger.utils.MLDB;
49 import net.ktnx.mobileledger.utils.SimpleDate;
50
51 import org.jetbrains.annotations.NotNull;
52
53 import java.util.Locale;
54
55 import static android.content.Context.INPUT_METHOD_SERVICE;
56 import static net.ktnx.mobileledger.utils.Logger.debug;
57
58 // TODO: support transaction-level comment
59
60 public class TransactionListFragment extends MobileLedgerListFragment
61         implements DatePickerFragment.DatePickedListener {
62     private MenuItem menuTransactionListFilter;
63     private View vAccountFilter;
64     private AutoCompleteTextView accNameFilter;
65     @Override
66     public void onCreate(@Nullable Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         setHasOptionsMenu(true);
69     }
70     @Nullable
71     @Override
72     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
73                              @Nullable Bundle savedInstanceState) {
74         return inflater.inflate(R.layout.transaction_list_fragment, container, false);
75     }
76     @Override
77     public void onResume() {
78         super.onResume();
79         debug("flow", "TransactionListFragment.onResume()");
80     }
81     @Override
82     public void onStop() {
83         super.onStop();
84         debug("flow", "TransactionListFragment.onStop()");
85     }
86     @Override
87     public void onPause() {
88         super.onPause();
89         debug("flow", "TransactionListFragment.onPause()");
90     }
91     @Override
92     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
93         debug("flow", "TransactionListFragment.onActivityCreated called");
94         super.onActivityCreated(savedInstanceState);
95
96         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
97                 this::onBackgroundTaskRunningChanged);
98
99         MainActivity mainActivity = getMainActivity();
100
101         swiper = mainActivity.findViewById(R.id.transaction_swipe);
102         if (swiper == null)
103             throw new RuntimeException("Can't get hold on the swipe layout");
104         root = mainActivity.findViewById(R.id.transaction_root);
105         if (root == null)
106             throw new RuntimeException("Can't get hold on the transaction value view");
107         modelAdapter = new TransactionListAdapter();
108         root.setAdapter(modelAdapter);
109
110         mainActivity.fabShouldShow();
111
112         manageFabOnScroll();
113
114         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
115
116         llm.setOrientation(RecyclerView.VERTICAL);
117         root.setLayoutManager(llm);
118
119         swiper.setOnRefreshListener(() -> {
120             debug("ui", "refreshing transactions via swipe");
121             Data.scheduleTransactionListRetrieval(mainActivity);
122         });
123
124         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
125
126         vAccountFilter = mainActivity.findViewById(R.id.transaction_list_account_name_filter);
127         accNameFilter = mainActivity.findViewById(R.id.transaction_filter_account_name);
128
129         MLDB.hookAutocompletionAdapter(mainActivity, accNameFilter, "accounts", "name");
130         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
131 //                debug("tmp", "direct onItemClick");
132             Cursor c = (Cursor) parent.getItemAtPosition(position);
133             Data.accountFilter.setValue(c.getString(1));
134             Globals.hideSoftKeyboard(mainActivity);
135         });
136
137         Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
138
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)
144                 return;
145
146             Snackbar.make(this.root, err, Snackbar.LENGTH_LONG)
147                     .show();
148             TransactionListViewModel.updateError.set(null);
149         });
150         Data.transactions.addObserver(
151                 (o, arg) -> mainActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
152
153         mainActivity.findViewById(R.id.clearAccountNameFilter)
154                     .setOnClickListener(v -> {
155                         Data.accountFilter.setValue(null);
156                         vAccountFilter.setVisibility(View.GONE);
157                         menuTransactionListFilter.setVisible(true);
158                         Globals.hideSoftKeyboard(mainActivity);
159                     });
160
161         Data.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
162             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
163             if (pos != null) {
164                 root.scrollToPosition(pos);
165                 // reset the value to avoid re-notification upon reconfiguration or app restart
166                 Data.foundTransactionItemIndex.setValue(null);
167             }
168         });
169     }
170     private void onAccountNameFilterChanged(String accName) {
171         final String fieldText = accNameFilter.getText()
172                                               .toString();
173         if ((accName == null) && (fieldText.equals("")))
174             return;
175
176         if (accNameFilter != null) {
177             accNameFilter.setText(accName, false);
178         }
179         final boolean filterActive = (accName != null) && !accName.isEmpty();
180         if (vAccountFilter != null) {
181             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
182         }
183         if (menuTransactionListFilter != null)
184             menuTransactionListFilter.setVisible(!filterActive);
185
186         TransactionListViewModel.scheduleTransactionListReload();
187
188     }
189     @Override
190     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
191         inflater.inflate(R.menu.transaction_list, menu);
192
193         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
194         if ((menuTransactionListFilter == null))
195             throw new AssertionError();
196
197         if ((Data.accountFilter.getValue() != null) ||
198             (vAccountFilter.getVisibility() == View.VISIBLE))
199         {
200             menuTransactionListFilter.setVisible(false);
201         }
202
203         super.onCreateOptionsMenu(menu, inflater);
204
205         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
206             vAccountFilter.setVisibility(View.VISIBLE);
207             if (menuTransactionListFilter != null)
208                 menuTransactionListFilter.setVisible(false);
209             accNameFilter.requestFocus();
210             InputMethodManager imm =
211                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
212             imm.showSoftInput(accNameFilter, 0);
213
214             return true;
215         });
216
217         menu.findItem(R.id.menu_go_to_date)
218             .setOnMenuItemClickListener(item -> {
219                 DatePickerFragment picker = new DatePickerFragment();
220                 picker.setOnDatePickedListener(this);
221                 picker.setDateRange(Data.earliestTransactionDate.getValue(),
222                         Data.latestTransactionDate.getValue());
223                 picker.show(requireActivity().getSupportFragmentManager(), null);
224                 return true;
225             });
226     }
227     @Override
228     public void onDatePicked(int year, int month, int day) {
229         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
230         AsyncTask<SimpleDate, Void, Integer> finder = new TransactionDateFinder();
231
232         finder.execute(new SimpleDate(year, month + 1, day));
233     }
234 }