]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
show/hide new transaction fab when transaction list is scrolled too
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListFragment.java
1 /*
2  * Copyright © 2018 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.Build;
24 import android.os.Bundle;
25 import android.preference.PreferenceManager;
26 import android.support.annotation.NonNull;
27 import android.support.annotation.Nullable;
28 import android.support.design.widget.FloatingActionButton;
29 import android.support.v4.app.Fragment;
30 import android.support.v4.widget.SwipeRefreshLayout;
31 import android.support.v7.widget.LinearLayoutManager;
32 import android.support.v7.widget.RecyclerView;
33 import android.util.Log;
34 import android.view.LayoutInflater;
35 import android.view.Menu;
36 import android.view.MenuInflater;
37 import android.view.MenuItem;
38 import android.view.View;
39 import android.view.ViewGroup;
40 import android.view.inputmethod.InputMethodManager;
41 import android.widget.AdapterView;
42 import android.widget.AutoCompleteTextView;
43 import android.widget.LinearLayout;
44 import android.widget.ProgressBar;
45 import android.widget.TextView;
46
47 import net.ktnx.mobileledger.R;
48 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
49 import net.ktnx.mobileledger.ui.activity.MainActivity;
50 import net.ktnx.mobileledger.utils.Globals;
51 import net.ktnx.mobileledger.utils.MLDB;
52
53 import java.lang.ref.WeakReference;
54 import java.time.ZoneId;
55 import java.time.format.DateTimeFormatter;
56 import java.util.Date;
57
58 import static android.content.Context.INPUT_METHOD_SERVICE;
59
60 public class TransactionListFragment extends Fragment {
61     public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name";
62     public TransactionListViewModel model;
63     private String mShowOnlyAccountName;
64     private MainActivity mActivity;
65     private View bTransactionListCancelDownload;
66     private MenuItem menuTransactionListFilter;
67     private View vAccountFilter;
68     private SwipeRefreshLayout swiper;
69     private RecyclerView root;
70     private ProgressBar progressBar;
71     private LinearLayout progressLayout;
72     private TextView tvLastUpdate;
73     private TransactionListAdapter modelAdapter;
74     private RetrieveTransactionsTask retrieveTransactionsTask;
75     private AutoCompleteTextView accNameFilter;
76     public void setShowOnlyAccountName(String mShowOnlyAccountName) {
77         this.mShowOnlyAccountName = mShowOnlyAccountName;
78         if (modelAdapter != null) {
79             modelAdapter.setBoldAccountName(mShowOnlyAccountName);
80         }
81         if (accNameFilter != null) {
82             accNameFilter.setText(mShowOnlyAccountName, false);
83         }
84         if (vAccountFilter != null) {
85             vAccountFilter.setVisibility(
86                     ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
87                     ? View.VISIBLE : View.GONE);
88         }
89     }
90     @Override
91     public void setArguments(@Nullable Bundle args) {
92         super.setArguments(args);
93         mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
94     }
95     @Override
96     public void onCreate(@Nullable Bundle savedInstanceState) {
97         super.onCreate(savedInstanceState);
98         setHasOptionsMenu(true);
99     }
100     @Override
101     public void onAttach(Context context) {
102         super.onAttach(context);
103         mActivity = (MainActivity) context;
104     }
105     @Nullable
106     @Override
107     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
108                              @Nullable Bundle savedInstanceState) {
109         return inflater.inflate(R.layout.transaction_list_fragment, container, false);
110     }
111
112     @Override
113     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
114         Log.d("flow", "TransactionListFragment.onActivityCreated called");
115         super.onActivityCreated(savedInstanceState);
116
117         mActivity.markDrawerItemCurrent(R.id.nav_latest_transactions);
118
119         swiper = mActivity.findViewById(R.id.transaction_swipe);
120         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
121         root = mActivity.findViewById(R.id.transaction_root);
122         if (root == null) throw new RuntimeException("Can't get hold on the transaction list view");
123         progressBar = mActivity.findViewById(R.id.transaction_list_progress_bar);
124         if (progressBar == null)
125             throw new RuntimeException("Can't get hold on the transaction list progress bar");
126         progressLayout = mActivity.findViewById(R.id.transaction_progress_layout);
127         if (progressLayout == null) throw new RuntimeException(
128                 "Can't get hold on the transaction list progress bar layout");
129         tvLastUpdate = mActivity.findViewById(R.id.transactions_last_update);
130         updateLastUpdateText();
131         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
132         modelAdapter = new TransactionListAdapter(model);
133
134         modelAdapter.setBoldAccountName(mShowOnlyAccountName);
135
136         FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
137
138         RecyclerView root = mActivity.findViewById(R.id.transaction_root);
139         root.setAdapter(modelAdapter);
140
141         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
142             @Override
143             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
144                 if (fab != null) {
145                     if (dy < 0) fab.show();
146                     if (dy > 0) fab.hide();
147                 }
148             }
149         });
150
151         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
152
153         llm.setOrientation(LinearLayoutManager.VERTICAL);
154         root.setLayoutManager(llm);
155
156         swiper.setOnRefreshListener(() -> {
157             Log.d("ui", "refreshing transactions via swipe");
158             update_transactions();
159         });
160
161         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
162
163         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
164         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
165         bTransactionListCancelDownload =
166                 mActivity.findViewById(R.id.transaction_list_cancel_download);
167
168         TransactionListFragment me = this;
169         MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name");
170         accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
171             @Override
172             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
173                 Log.d("tmp", "direct onItemClick");
174                 model.reloadTransactions(me);
175                 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
176                 modelAdapter.setBoldAccountName(mc.getString(1));
177                 modelAdapter.notifyDataSetChanged();
178                 Globals.hideSoftKeyboard(mActivity);
179             }
180         });
181
182         updateLastUpdateText();
183         long last_update = MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
184         Log.d("transactions", String.format("Last update = %d", last_update));
185
186         if (mShowOnlyAccountName != null) {
187             accNameFilter.setText(mShowOnlyAccountName, false);
188             onShowFilterClick(null);
189             Log.d("flow", String.format("Account filter set to '%s'", mShowOnlyAccountName));
190         }
191
192         if (last_update == 0) {
193             update_transactions();
194         }
195         else {
196             model.reloadTransactions(this);
197         }
198     }
199     @Override
200     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
201         inflater.inflate(R.menu.transaction_list, menu);
202
203         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
204         if ((menuTransactionListFilter == null)) throw new AssertionError();
205
206         if (mShowOnlyAccountName != null) {
207             menuTransactionListFilter.setVisible(false);
208         }
209
210         super.onCreateOptionsMenu(menu, inflater);
211     }
212     private void update_transactions() {
213         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
214
215         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
216                 PreferenceManager.getDefaultSharedPreferences(mActivity));
217
218         retrieveTransactionsTask.execute(params);
219         bTransactionListCancelDownload.setEnabled(true);
220     }
221     public void onRetrieveStart() {
222         progressBar.setIndeterminate(true);
223         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
224         else progressBar.setProgress(0);
225         progressLayout.setVisibility(View.VISIBLE);
226     }
227     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
228         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
229             (progress.getTotal() == 0))
230         {
231             progressBar.setIndeterminate(true);
232         }
233         else {
234             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
235                 progressBar.setMin(0);
236             }
237             progressBar.setMax(progress.getTotal());
238             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
239                 progressBar.setProgress(progress.getProgress(), true);
240             }
241             else progressBar.setProgress(progress.getProgress());
242             progressBar.setIndeterminate(false);
243         }
244     }
245
246     public void onRetrieveDone(boolean success) {
247         progressLayout.setVisibility(View.GONE);
248         swiper.setRefreshing(false);
249         updateLastUpdateText();
250         if (success) {
251             Log.d("transactions", "calling notifyDataSetChanged()");
252             modelAdapter.notifyDataSetChanged();
253         }
254     }
255     private void updateLastUpdateText() {
256         {
257             long last_update =
258                     MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
259             Log.d("transactions", String.format("Last update = %d", last_update));
260             if (last_update == 0) {
261                 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
262             }
263             else {
264                 Date date = new Date(last_update);
265                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
266                     tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
267                             .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
268                 }
269                 else {
270                     tvLastUpdate.setText(date.toLocaleString());
271                 }
272             }
273         }
274     }
275     public void onClearAccountNameClick(View view) {
276         vAccountFilter.setVisibility(View.GONE);
277         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
278         accNameFilter.setText(null);
279         mShowOnlyAccountName = null;
280         model.reloadTransactions(this);
281         modelAdapter.resetBoldAccountName();
282         modelAdapter.notifyDataSetChanged();
283         Globals.hideSoftKeyboard(mActivity);
284     }
285     public void onShowFilterClick(MenuItem menuItem) {
286         vAccountFilter.setVisibility(View.VISIBLE);
287         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
288         if (menuItem != null) {
289             accNameFilter.requestFocus();
290             InputMethodManager imm =
291                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
292             imm.showSoftInput(accNameFilter, 0);
293         }
294     }
295     public void onStopTransactionRefreshClick(View view) {
296         Log.d("interactive", "Cancelling transactions refresh");
297         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
298         bTransactionListCancelDownload.setEnabled(false);
299     }
300 }