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