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