]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
TODO: transaction-level comments (displaying and entering)
[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 // TODO: support transaction-level comment
52
53 public class TransactionListFragment extends MobileLedgerListFragment {
54     private MenuItem menuTransactionListFilter;
55     private View vAccountFilter;
56     private AutoCompleteTextView accNameFilter;
57     @Override
58     public void onCreate(@Nullable Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setHasOptionsMenu(true);
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         Data.backgroundTasksRunning.observe(getViewLifecycleOwner(),
94                 this::onBackgroundTaskRunningChanged);
95
96         swiper = mActivity.findViewById(R.id.transaction_swipe);
97         if (swiper == null)
98             throw new RuntimeException("Can't get hold on the swipe layout");
99         root = mActivity.findViewById(R.id.transaction_root);
100         if (root == null)
101             throw new RuntimeException("Can't get hold on the transaction value view");
102         modelAdapter = new TransactionListAdapter();
103         root.setAdapter(modelAdapter);
104
105         mActivity.fabShouldShow();
106
107         manageFabOnScroll();
108
109         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
110
111         llm.setOrientation(RecyclerView.VERTICAL);
112         root.setLayoutManager(llm);
113
114         swiper.setOnRefreshListener(() -> {
115             debug("ui", "refreshing transactions via swipe");
116             Data.scheduleTransactionListRetrieval(mActivity);
117         });
118
119         Colors.themeWatch.observe(getViewLifecycleOwner(), this::themeChanged);
120
121         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
122         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
123
124         MLDB.hookAutocompletionAdapter(mActivity, accNameFilter, "accounts", "name");
125         accNameFilter.setOnItemClickListener((parent, view, position, id) -> {
126 //                debug("tmp", "direct onItemClick");
127             MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
128             Data.accountFilter.setValue(mc.getString(1));
129             Globals.hideSoftKeyboard(mActivity);
130         });
131
132         Data.accountFilter.observe(getViewLifecycleOwner(), this::onAccountNameFilterChanged);
133
134         TransactionListViewModel.updating.addObserver(
135                 (o, arg) -> swiper.setRefreshing(TransactionListViewModel.updating.get()));
136         TransactionListViewModel.updateError.addObserver((o, arg) -> {
137             String err = TransactionListViewModel.updateError.get();
138             if (err == null)
139                 return;
140
141             Toast.makeText(mActivity, err, Toast.LENGTH_SHORT)
142                  .show();
143             TransactionListViewModel.updateError.set(null);
144         });
145         Data.transactions.addObserver(
146                 (o, arg) -> mActivity.runOnUiThread(() -> modelAdapter.notifyDataSetChanged()));
147
148         mActivity.findViewById(R.id.clearAccountNameFilter)
149                  .setOnClickListener(v -> {
150                      Data.accountFilter.setValue(null);
151                      vAccountFilter.setVisibility(View.GONE);
152                      menuTransactionListFilter.setVisible(true);
153                      Globals.hideSoftKeyboard(mActivity);
154                  });
155     }
156     private void onAccountNameFilterChanged(String accName) {
157         final String fieldText = accNameFilter.getText()
158                                               .toString();
159         if ((accName == null) && (fieldText.equals("")))
160             return;
161
162         if (accNameFilter != null) {
163             accNameFilter.setText(accName, false);
164         }
165         final boolean filterActive = (accName != null) && !accName.isEmpty();
166         if (vAccountFilter != null) {
167             vAccountFilter.setVisibility(filterActive ? View.VISIBLE : View.GONE);
168         }
169         if (menuTransactionListFilter != null)
170             menuTransactionListFilter.setVisible(!filterActive);
171
172         TransactionListViewModel.scheduleTransactionListReload();
173
174     }
175     @Override
176     public void onCreateOptionsMenu(@NotNull Menu menu, @NotNull MenuInflater inflater) {
177         inflater.inflate(R.menu.transaction_list, menu);
178
179         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
180         if ((menuTransactionListFilter == null))
181             throw new AssertionError();
182
183         if ((Data.accountFilter.getValue() != null) ||
184             (vAccountFilter.getVisibility() == View.VISIBLE))
185         {
186             menuTransactionListFilter.setVisible(false);
187         }
188
189         super.onCreateOptionsMenu(menu, inflater);
190
191         menuTransactionListFilter.setOnMenuItemClickListener(item -> {
192             vAccountFilter.setVisibility(View.VISIBLE);
193             if (menuTransactionListFilter != null)
194                 menuTransactionListFilter.setVisible(false);
195             accNameFilter.requestFocus();
196             InputMethodManager imm =
197                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
198             imm.showSoftInput(accNameFilter, 0);
199
200             return true;
201         });
202     }
203 }