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