]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
whitespace
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2019 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.content.Context;
21 import android.database.MatrixCursor;
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 import android.widget.Toast;
32
33 import androidx.annotation.NonNull;
34 import androidx.annotation.Nullable;
35 import androidx.recyclerview.widget.LinearLayoutManager;
36 import androidx.recyclerview.widget.RecyclerView;
37
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
41 import net.ktnx.mobileledger.ui.activity.MainActivity;
42 import net.ktnx.mobileledger.utils.Colors;
43 import net.ktnx.mobileledger.utils.Globals;
44 import net.ktnx.mobileledger.utils.MLDB;
45
46 import org.jetbrains.annotations.NotNull;
47
48 import static android.content.Context.INPUT_METHOD_SERVICE;
49 import static net.ktnx.mobileledger.utils.Logger.debug;
50
51 public class TransactionListFragment extends MobileLedgerListFragment {
52     private MenuItem menuTransactionListFilter;
53     private View vAccountFilter;
54     private AutoCompleteTextView accNameFilter;
55     @Override
56     public void onCreate(@Nullable Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setHasOptionsMenu(true);
59         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
60                 this::onBackgroundTaskRunningChanged);
61     }
62     @Override
63     public void onAttach(@NotNull Context context) {
64         super.onAttach(context);
65         mActivity = (MainActivity) context;
66     }
67     @Nullable
68     @Override
69     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
70                              @Nullable Bundle savedInstanceState) {
71         return inflater.inflate(R.layout.transaction_list_fragment, container, false);
72     }
73     @Override
74     public void onResume() {
75         super.onResume();
76         debug("flow", "TransactionListFragment.onResume()");
77     }
78     @Override
79     public void onStop() {
80         super.onStop();
81         debug("flow", "TransactionListFragment.onStop()");
82     }
83     @Override
84     public void onPause() {
85         super.onPause();
86         debug("flow", "TransactionListFragment.onPause()");
87     }
88     @Override
89     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
90         debug("flow", "TransactionListFragment.onActivityCreated called");
91         super.onActivityCreated(savedInstanceState);
92
93         swiper = mActivity.findViewById(R.id.transaction_swipe);
94         if (swiper == null)
95             throw new RuntimeException("Can't get hold on the swipe layout");
96         root = mActivity.findViewById(R.id.transaction_root);
97         if (root == null)
98             throw new RuntimeException("Can't get hold on the transaction value view");
99         modelAdapter = new TransactionListAdapter();
100         root.setAdapter(modelAdapter);
101
102         mActivity.fabShouldShow();
103
104         manageFabOnScroll();
105
106         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
107
108         llm.setOrientation(RecyclerView.VERTICAL);
109         root.setLayoutManager(llm);
110
111         swiper.setOnRefreshListener(() -> {
112             debug("ui", "refreshing transactions via swipe");
113             Data.scheduleTransactionListRetrieval(mActivity);
114         });
115
116         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
117
118         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
119         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
120
121         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
122         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
123 //                debug("tmp", "direct onItemClick");
124             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
125             Data.accountFilter.setValue(mc.getString(1));
126             Globals.hideSoftKeyboard(mActivity);
127         });
128
129         Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
130
131         TransactionListViewModel.updating.addObserver(
132                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
133         TransactionListViewModel.updateError.addObserver((o, arg) -> {
134             String err = TransactionListViewModel.updateError.get();
135             if (err == null)
136                 return;
137
138             Toast.makeText(mActivity, err, Toast.LENGTH_SHORT)
139                  .show();
140             TransactionListViewModel.updateError.set(null);
141         });
142         Data.transactions.addObserver(
143                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
144
145         mActivity.findViewById(R.id.clearAccountNameFilter)
146                  .setOnClickListener(v -> {
147                      Data.accountFilter.setValue(null);
148                      vAccountFilter.setVisibility(View.GONE);
149                      menuTransactionListFilter.setVisible(true);
150                      Globals.hideSoftKeyboard(mActivity);
151                  });
152     }
153     private void onAccountNameFilterChanged(String accName) {
154         final String fieldText = accNameFilter.getText()
155                                               .toString();
156         if ((accName == null) && (fieldText.equals("")))
157             return;
158
159         if (accNameFilter != null) {
160             accNameFilter.setText(accName, false);
161         }
162         final boolean filterActive = (accName != null) && !accName.isEmpty();
163         if (vAccountFilter != null) {
164             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
165         }
166         if (menuTransactionListFilter != null)
167             menuTransactionListFilter.setVisible(!filterActive);
168
169         TransactionListViewModel.scheduleTransactionListReload();
170
171     }
172     @Override
173     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
174         inflater.inflate(R.menu.transaction_list, menu);
175
176         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
177         if ((menuTransactionListFilter == null))
178             throw new AssertionError();
179
180         if ((Data.accountFilter.getValue() != null) ||
181             (vAccountFilter.getVisibility() == View.VISIBLE))
182         {
183             menuTransactionListFilter.setVisible(false);
184         }
185
186         super.onCreateOptionsMenu(menu, inflater);
187
188         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
189             vAccountFilter.setVisibility(View.VISIBLE);
190             if (menuTransactionListFilter != null)
191                 menuTransactionListFilter.setVisible(false);
192             accNameFilter.requestFocus();
193             InputMethodManager imm =
194                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
195             imm.showSoftInput(accNameFilter, 0);
196
197             return true;
198         });
199     }
200 }