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