]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
work around trans. list menu items visible with setOffscreenPageLimit(1)
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2021 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.os.AsyncTask;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.inputmethod.InputMethodManager;
29 import android.widget.AutoCompleteTextView;
30
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.lifecycle.ViewModelProvider;
34 import androidx.recyclerview.widget.LinearLayoutManager;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.async.TransactionDateFinder;
39 import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
40 import net.ktnx.mobileledger.db.Profile;
41 import net.ktnx.mobileledger.model.Data;
42 import net.ktnx.mobileledger.ui.DatePickerFragment;
43 import net.ktnx.mobileledger.ui.FabManager;
44 import net.ktnx.mobileledger.ui.MainModel;
45 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
46 import net.ktnx.mobileledger.ui.activity.MainActivity;
47 import net.ktnx.mobileledger.utils.Colors;
48 import net.ktnx.mobileledger.utils.Globals;
49 import net.ktnx.mobileledger.utils.Logger;
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 public class TransactionListFragment extends MobileLedgerListFragment
60         implements DatePickerFragment.DatePickedListener {
61     private MenuItem menuTransactionListFilter;
62     private MenuItem menuGoToDate;
63     private View vAccountFilter;
64     private AutoCompleteTextView accNameFilter;
65     private MainModel model;
66     private boolean fragmentActive = false;
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         fragmentActive = true;
82         toggleMenuItems();
83         debug("flow", "TransactionListFragment.onResume()");
84     }
85     private void toggleMenuItems() {
86         if (menuGoToDate != null)
87             menuGoToDate.setVisible(fragmentActive);
88         if (menuTransactionListFilter != null) {
89             final int filterVisibility = vAccountFilter.getVisibility();
90             menuTransactionListFilter.setVisible(
91                     fragmentActive && filterVisibility != View.VISIBLE);
92         }
93     }
94     @Override
95     public void onStop() {
96         super.onStop();
97         fragmentActive = false;
98         toggleMenuItems();
99         debug("flow", "TransactionListFragment.onStop()");
100     }
101     @Override
102     public void onPause() {
103         super.onPause();
104         fragmentActive = false;
105         toggleMenuItems();
106         debug("flow", "TransactionListFragment.onPause()");
107     }
108     @Override
109     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
110         debug("flow", "TransactionListFragment.onActivityCreated called");
111         super.onViewCreated(view, savedInstanceState);
112
113         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
114                 this::onBackgroundTaskRunningChanged);
115
116         MainActivity mainActivity = getMainActivity();
117
118         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
119
120         refreshLayout = view.findViewById(R.id.transaction_swipe);
121         if (refreshLayout == null)
122             throw new RuntimeException("Can't get hold on the swipe layout");
123         root = view.findViewById(R.id.transaction_root);
124         if (root == null)
125             throw new RuntimeException("Can't get hold on the transaction value view");
126         modelAdapter = new TransactionListAdapter();
127         root.setAdapter(modelAdapter);
128
129         mainActivity.fabShouldShow();
130
131         if (mainActivity instanceof FabManager.FabHandler)
132             FabManager.handle(mainActivity, root);
133
134         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
135
136         llm.setOrientation(RecyclerView.VERTICAL);
137         root.setLayoutManager(llm);
138
139         refreshLayout.setOnRefreshListener(() -> {
140             debug("ui", "refreshing transactions via swipe");
141             model.scheduleTransactionListRetrieval();
142         });
143
144         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
145
146         vAccountFilter = view.findViewById(R.id.transaction_list_account_name_filter);
147         accNameFilter = view.findViewById(R.id.transaction_filter_account_name);
148
149         Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged);
150
151         accNameFilter.setOnItemClickListener((parent, v, position, id) -> {
152 //                debug("tmp", "direct onItemClick");
153             model.getAccountFilter()
154                  .setValue(parent.getItemAtPosition(position)
155                                  .toString());
156             Globals.hideSoftKeyboard(mainActivity);
157         });
158
159         model.getAccountFilter()
160              .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
161
162         model.getUpdatingFlag()
163              .observe(getViewLifecycleOwner(), (flag) -> refreshLayout.setRefreshing(flag));
164         model.getDisplayedTransactions()
165              .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
166
167         view.findViewById(R.id.clearAccountNameFilter)
168             .setOnClickListener(v -> {
169                 model.getAccountFilter()
170                      .setValue(null);
171                 Globals.hideSoftKeyboard(mainActivity);
172             });
173
174         model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
175             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
176             if (pos != null) {
177                 root.scrollToPosition(pos);
178                 // reset the value to avoid re-notification upon reconfiguration or app restart
179                 model.foundTransactionItemIndex.setValue(null);
180             }
181         });
182     }
183     private void onProfileChanged(Profile profile) {
184         if (profile == null)
185             return;
186
187         accNameFilter.setAdapter(new AccountAutocompleteAdapter(getContext(), profile));
188     }
189     private void onAccountNameFilterChanged(String accName) {
190         accNameFilter.setText(accName, false);
191
192         boolean filterActive = (accName != null) && !accName.isEmpty();
193         if (vAccountFilter != null) {
194             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
195         }
196         if (menuTransactionListFilter != null)
197             menuTransactionListFilter.setVisible(!filterActive);
198     }
199     @Override
200     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
201         inflater.inflate(R.menu.transaction_list, menu);
202
203         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
204         if ((menuTransactionListFilter == null))
205             throw new AssertionError();
206         menuGoToDate = menu.findItem(R.id.menu_go_to_date);
207         if ((menuGoToDate == null))
208             throw new AssertionError();
209
210         model.getAccountFilter()
211              .observe(this, v -> menuTransactionListFilter.setVisible(v == null));
212
213         super.onCreateOptionsMenu(menu, inflater);
214
215         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
216             vAccountFilter.setVisibility(View.VISIBLE);
217             menuTransactionListFilter.setVisible(false);
218             accNameFilter.requestFocus();
219             InputMethodManager imm =
220                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
221             imm.showSoftInput(accNameFilter, 0);
222
223             return true;
224         });
225
226         menuGoToDate.setOnMenuItemClickListener(item -> {
227             DatePickerFragment picker = new DatePickerFragment();
228             picker.setOnDatePickedListener(this);
229             picker.setDateRange(model.getFirstTransactionDate(), model.getLastTransactionDate());
230             picker.show(requireActivity().getSupportFragmentManager(), null);
231             return true;
232         });
233
234         toggleMenuItems();
235     }
236     @Override
237     public void onDatePicked(int year, int month, int day) {
238         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
239         AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
240
241         finder.execute(
242                 new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));
243     }
244 }