]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
45e6767b30a5984e17dbd7aaa0f970a0d62e25da
[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         fab.show();
142         root.addOnScrollListener(new RecyclerView.OnScrollListener() {
143             @Override
144             public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
145                 if (fab != null) {
146                     if (dy < 0) fab.show();
147                     if (dy > 0) fab.hide();
148                 }
149             }
150         });
151
152         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
153
154         llm.setOrientation(LinearLayoutManager.VERTICAL);
155         root.setLayoutManager(llm);
156
157         swiper.setOnRefreshListener(() -> {
158             Log.d("ui", "refreshing transactions via swipe");
159             update_transactions();
160         });
161
162         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
163
164         vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
165         accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
166         bTransactionListCancelDownload =
167                 mActivity.findViewById(R.id.transaction_list_cancel_download);
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                 model.reloadTransactions(me);
176                 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
177                 modelAdapter.setBoldAccountName(mc.getString(1));
178                 modelAdapter.notifyDataSetChanged();
179                 Globals.hideSoftKeyboard(mActivity);
180             }
181         });
182
183         updateLastUpdateText();
184         long last_update = MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
185         Log.d("transactions", String.format("Last update = %d", last_update));
186
187         if (mShowOnlyAccountName != null) {
188             accNameFilter.setText(mShowOnlyAccountName, false);
189             onShowFilterClick(null);
190             Log.d("flow", String.format("Account filter set to '%s'", mShowOnlyAccountName));
191         }
192
193         if (last_update == 0) {
194             update_transactions();
195         }
196         else {
197             model.reloadTransactions(this);
198         }
199     }
200     @Override
201     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
202         inflater.inflate(R.menu.transaction_list, menu);
203
204         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
205         if ((menuTransactionListFilter == null)) throw new AssertionError();
206
207         if (mShowOnlyAccountName != null) {
208             menuTransactionListFilter.setVisible(false);
209         }
210
211         super.onCreateOptionsMenu(menu, inflater);
212     }
213     private void update_transactions() {
214         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
215
216         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
217                 PreferenceManager.getDefaultSharedPreferences(mActivity));
218
219         retrieveTransactionsTask.execute(params);
220         bTransactionListCancelDownload.setEnabled(true);
221     }
222     public void onRetrieveStart() {
223         progressBar.setIndeterminate(true);
224         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
225         else progressBar.setProgress(0);
226         progressLayout.setVisibility(View.VISIBLE);
227     }
228     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
229         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
230             (progress.getTotal() == 0))
231         {
232             progressBar.setIndeterminate(true);
233         }
234         else {
235             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
236                 progressBar.setMin(0);
237             }
238             progressBar.setMax(progress.getTotal());
239             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
240                 progressBar.setProgress(progress.getProgress(), true);
241             }
242             else progressBar.setProgress(progress.getProgress());
243             progressBar.setIndeterminate(false);
244         }
245     }
246
247     public void onRetrieveDone(boolean success) {
248         progressLayout.setVisibility(View.GONE);
249         swiper.setRefreshing(false);
250         updateLastUpdateText();
251         if (success) {
252             Log.d("transactions", "calling notifyDataSetChanged()");
253             modelAdapter.notifyDataSetChanged();
254         }
255     }
256     private void updateLastUpdateText() {
257         {
258             long last_update =
259                     MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
260             Log.d("transactions", String.format("Last update = %d", last_update));
261             if (last_update == 0) {
262                 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
263             }
264             else {
265                 Date date = new Date(last_update);
266                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
267                     tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
268                             .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
269                 }
270                 else {
271                     tvLastUpdate.setText(date.toLocaleString());
272                 }
273             }
274         }
275     }
276     public void onClearAccountNameClick(View view) {
277         vAccountFilter.setVisibility(View.GONE);
278         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
279         accNameFilter.setText(null);
280         mShowOnlyAccountName = null;
281         model.reloadTransactions(this);
282         modelAdapter.resetBoldAccountName();
283         modelAdapter.notifyDataSetChanged();
284         Globals.hideSoftKeyboard(mActivity);
285     }
286     public void onShowFilterClick(MenuItem menuItem) {
287         vAccountFilter.setVisibility(View.VISIBLE);
288         if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
289         if (menuItem != null) {
290             accNameFilter.requestFocus();
291             InputMethodManager imm =
292                     (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
293             imm.showSoftInput(accNameFilter, 0);
294         }
295     }
296     public void onStopTransactionRefreshClick(View view) {
297         Log.d("interactive", "Cancelling transactions refresh");
298         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
299         bTransactionListCancelDownload.setEnabled(false);
300     }
301 }