]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
Room-based profile management
[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 View vAccountFilter;
63     private AutoCompleteTextView accNameFilter;
64     private MainModel model;
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 onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
93         debug("flow", "TransactionListFragment.onActivityCreated called");
94         super.onViewCreated(view, savedInstanceState);
95
96         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
97                 this::onBackgroundTaskRunningChanged);
98
99         MainActivity mainActivity = getMainActivity();
100
101         model = new ViewModelProvider(requireActivity()).get(MainModel.class);
102
103         refreshLayout = view.findViewById(R.id.transaction_swipe);
104         if (refreshLayout == null)
105             throw new RuntimeException("Can't get hold on the swipe layout");
106         root = view.findViewById(R.id.transaction_root);
107         if (root == null)
108             throw new RuntimeException("Can't get hold on the transaction value view");
109         modelAdapter = new TransactionListAdapter(model);
110         root.setAdapter(modelAdapter);
111
112         mainActivity.fabShouldShow();
113
114         if (mainActivity instanceof FabManager.FabHandler)
115             FabManager.handle(mainActivity, root);
116
117         LinearLayoutManager llm = new LinearLayoutManager(mainActivity);
118
119         llm.setOrientation(RecyclerView.VERTICAL);
120         root.setLayoutManager(llm);
121
122         refreshLayout.setOnRefreshListener(() -> {
123             debug("ui", "refreshing transactions via swipe");
124             model.scheduleTransactionListRetrieval();
125         });
126
127         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
128
129         vAccountFilter = view.findViewById(R.id.transaction_list_account_name_filter);
130         accNameFilter = view.findViewById(R.id.transaction_filter_account_name);
131
132         Profile profile = Data.getProfile();
133         accNameFilter.setAdapter(new AccountAutocompleteAdapter(mainActivity, profile));
134         accNameFilter.setOnItemClickListener((parent, v, position, id) -> {
135 //                debug("tmp", "direct onItemClick");
136             model.getAccountFilter()
137                  .setValue(parent.getItemAtPosition(position)
138                                  .toString());
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         model.getDisplayedTransactions()
148              .observe(getViewLifecycleOwner(), list -> modelAdapter.setTransactions(list));
149
150         view.findViewById(R.id.clearAccountNameFilter)
151             .setOnClickListener(v -> {
152                 model.getAccountFilter()
153                      .setValue(null);
154                 vAccountFilter.setVisibility(View.GONE);
155                 menuTransactionListFilter.setVisible(true);
156                 Globals.hideSoftKeyboard(mainActivity);
157             });
158
159         model.foundTransactionItemIndex.observe(getViewLifecycleOwner(), pos -> {
160             Logger.debug("go-to-date", String.format(Locale.US, "Found pos %d", pos));
161             if (pos != null) {
162                 root.scrollToPosition(pos);
163                 // reset the value to avoid re-notification upon reconfiguration or app restart
164                 model.foundTransactionItemIndex.setValue(null);
165             }
166         });
167     }
168     private void onAccountNameFilterChanged(String accName) {
169         final String fieldText = accNameFilter.getText()
170                                               .toString();
171         if ((accName == null) && (fieldText.equals("")))
172             return;
173
174         if (accNameFilter != null) {
175             accNameFilter.setText(accName, false);
176         }
177         final boolean filterActive = (accName != null) && !accName.isEmpty();
178         if (vAccountFilter != null) {
179             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
180         }
181         if (menuTransactionListFilter != null)
182             menuTransactionListFilter.setVisible(!filterActive);
183
184         model.scheduleTransactionListReload();
185
186     }
187     @Override
188     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
189         inflater.inflate(R.menu.transaction_list, menu);
190
191         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
192         if ((menuTransactionListFilter == null))
193             throw new AssertionError();
194
195         if ((model.getAccountFilter()
196                   .getValue() != null) || (vAccountFilter.getVisibility() == View.VISIBLE))
197         {
198             menuTransactionListFilter.setVisible(false);
199         }
200
201         super.onCreateOptionsMenu(menu, inflater);
202
203         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
204             vAccountFilter.setVisibility(View.VISIBLE);
205             if (menuTransactionListFilter != null)
206                 menuTransactionListFilter.setVisible(false);
207             accNameFilter.requestFocus();
208             InputMethodManager imm =
209                     (InputMethodManager) getMainActivity().getSystemService(INPUT_METHOD_SERVICE);
210             imm.showSoftInput(accNameFilter, 0);
211
212             return true;
213         });
214
215         menu.findItem(R.id.menu_go_to_date)
216             .setOnMenuItemClickListener(item -> {
217                 DatePickerFragment picker = new DatePickerFragment();
218                 picker.setOnDatePickedListener(this);
219                 picker.setDateRange(model.getFirstTransactionDate(),
220                         model.getLastTransactionDate());
221                 picker.show(requireActivity().getSupportFragmentManager(), null);
222                 return true;
223             });
224     }
225     @Override
226     public void onDatePicked(int year, int month, int day) {
227         RecyclerView list = requireActivity().findViewById(R.id.transaction_root);
228         AsyncTask<TransactionDateFinder.Params, Void, Integer> finder = new TransactionDateFinder();
229
230         finder.execute(
231                 new TransactionDateFinder.Params(model, new SimpleDate(year, month + 1, day)));
232     }
233 }