]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
more pronounced day/month delimiters in the transaction list
[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.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuInflater;
24 import android.view.MenuItem;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.inputmethod.InputMethodManager;
28
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.lifecycle.ViewModelProvider;
32 import androidx.recyclerview.widget.LinearLayoutManager;
33 import androidx.recyclerview.widget.RecyclerView;
34 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.TransactionDateFinder;
38 import net.ktnx.mobileledger.databinding.TransactionListFragmentBinding;
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 MainModel model;
64     private boolean fragmentActive = false;
65     private TransactionListFragmentBinding b;
66     @Override
67     public void onCreate(@Nullable Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         setHasOptionsMenu(true);
70     }
71     @Nullable
72     @Override
73     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
74                              @Nullable Bundle savedInstanceState) {
75         b = TransactionListFragmentBinding.inflate(inflater, container, false);
76         return b.getRoot();
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 = b.transactionListAccountNameFilter.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 SwipeRefreshLayout getRefreshLayout() {
110         return b.transactionSwipe;
111     }
112     @Override
113     public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
114         debug("flow", "TransactionListFragment.onActivityCreated called");
115         super.onViewCreated(view, savedInstanceState);
116         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
117                 this::onBackgroundTaskRunningChanged);
118
119         MainActivity mainActivity = getMainActivity();
120
121         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
122
123         modelAdapter = new TransactionListAdapter();
124         b.transactionRoot.setAdapter(modelAdapter);
125
126         mainActivity.fabShouldShow();
127
128         if (mainActivity instanceof FabManager.FabHandler)
129             FabManager.handle(mainActivity, b.transactionRoot);
130
131         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
132
133         llm.setOrientation(RecyclerView.VERTICAL);
134         b.transactionRoot.setLayoutManager(llm);
135
136         b.transactionSwipe.setOnRefreshListener(() -> {
137             debug("ui", "refreshing transactions via swipe");
138             model.scheduleTransactionListRetrieval();
139         });
140
141         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
142
143         Data.observeProfile(getViewLifecycleOwner(), this::onProfileChanged);
144
145         b.transactionFilterAccountName.setOnItemClickListener((parent, v, position, id) -> {
146 //                debug("tmp", "direct onItemClick");
147             model.getAccountFilter()
148                  .setValue(parent.getItemAtPosition(position)
149                                  .toString());
150             Globals.hideSoftKeyboard(mainActivity);
151         });
152
153         model.getAccountFilter()
154              .observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
155
156         model.getUpdatingFlag()
157              .observe(getViewLifecycleOwner(), (flag) -> b.transactionSwipe.setRefreshing(flag));
158         model.getDisplayedTransactions()
159              .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
160
161         view.findViewById(R.id.clearAccountNameFilter)
162             .setOnClickListener(v -> {
163                 model.getAccountFilter()
164                      .setValue(null);
165                 Globals.hideSoftKeyboard(mainActivity);
166             });
167
168         model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
169             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
170             if (pos != null) {
171                 b.transactionRoot.scrollToPosition(pos);
172                 // reset the value to avoid re-notification upon reconfiguration or app restart
173                 model.foundTransactionItemIndex.setValue(null);
174             }
175         });
176     }
177     private void onProfileChanged(Profile profile) {
178         if (profile == null)
179             return;
180
181         b.transactionFilterAccountName.setAdapter(
182                 new AccountAutocompleteAdapter(getContext(), profile));
183     }
184     private void onAccountNameFilterChanged(String accName) {
185         b.transactionFilterAccountName.setText(accName, false);
186
187         boolean filterActive = (accName != null) && !accName.isEmpty();
188         b.transactionListAccountNameFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
189         if (menuTransactionListFilter != null)
190             menuTransactionListFilter.setVisible(!filterActive);
191     }
192     @Override
193     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
194         inflater.inflate(R.menu.transaction_list, menu);
195
196         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
197         if ((menuTransactionListFilter == null))
198             throw new AssertionError();
199         menuGoToDate = menu.findItem(R.id.menu_go_to_date);
200         if ((menuGoToDate == null))
201             throw new AssertionError();
202
203         model.getAccountFilter()
204              .observe(this, v -> menuTransactionListFilter.setVisible(v == null));
205
206         super.onCreateOptionsMenu(menu, inflater);
207
208         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
209             b.transactionListAccountNameFilter.setVisibility(View.VISIBLE);
210             menuTransactionListFilter.setVisible(false);
211             b.transactionFilterAccountName.requestFocus();
212             InputMethodManager imm =
213                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
214             imm.showSoftInput(b.transactionFilterAccountName, 0);
215
216             return true;
217         });
218
219         menuGoToDate.setOnMenuItemClickListener(item -> {
220             DatePickerFragment picker = new DatePickerFragment();
221             picker.setOnDatePickedListener(this);
222             picker.setDateRange(model.getFirstTransactionDate(), model.getLastTransactionDate());
223             picker.show(requireActivity().getSupportFragmentManager(), null);
224             return true;
225         });
226
227         toggleMenuItems();
228     }
229     @Override
230     public void onDatePicked(int year, int month, int day) {
231         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
232         TransactionDateFinder finder = new TransactionDateFinder(model, new SimpleDate(year, month + 1, day));
233
234         finder.start();
235     }
236 }