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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
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.v4.app.Fragment;
29 import android.support.v4.widget.SwipeRefreshLayout;
30 import android.support.v7.widget.LinearLayoutManager;
31 import android.support.v7.widget.RecyclerView;
32 import android.util.Log;
33 import android.view.LayoutInflater;
34 import android.view.Menu;
35 import android.view.MenuInflater;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.view.inputmethod.InputMethodManager;
40 import android.widget.AdapterView;
41 import android.widget.AutoCompleteTextView;
42 import android.widget.LinearLayout;
43 import android.widget.ProgressBar;
44 import android.widget.TextView;
46 import net.ktnx.mobileledger.ui.activity.MainActivity;
47 import net.ktnx.mobileledger.R;
48 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
49 import net.ktnx.mobileledger.utils.Globals;
50 import net.ktnx.mobileledger.utils.MLDB;
52 import java.lang.ref.WeakReference;
53 import java.time.ZoneId;
54 import java.time.format.DateTimeFormatter;
55 import java.util.Date;
57 import static android.content.Context.INPUT_METHOD_SERVICE;
59 public class TransactionListFragment extends Fragment {
60 public TransactionListViewModel model;
61 private MainActivity mActivity;
62 private View bTransactionListCancelDownload;
63 private MenuItem menuTransactionListFilter;
64 private View vAccountFilter;
65 private SwipeRefreshLayout swiper;
66 private RecyclerView root;
67 private ProgressBar progressBar;
68 private LinearLayout progressLayout;
69 private TextView tvLastUpdate;
70 private TransactionListAdapter modelAdapter;
71 private RetrieveTransactionsTask retrieveTransactionsTask;
72 private AutoCompleteTextView accNameFilter;
74 public void onCreate(@Nullable Bundle savedInstanceState) {
75 super.onCreate(savedInstanceState);
76 setHasOptionsMenu(true);
79 public void onAttach(Context context) {
80 super.onAttach(context);
81 mActivity = (MainActivity) context;
85 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
86 @Nullable Bundle savedInstanceState) {
87 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
91 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
92 Log.d("flow", "TransactionListFragment.onActivityCreated called");
93 super.onActivityCreated(savedInstanceState);
95 swiper = mActivity.findViewById(R.id.transaction_swipe);
96 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
97 root = mActivity.findViewById(R.id.transaction_root);
98 if (root == null) throw new RuntimeException("Can't get hold on the transaction list view");
99 progressBar = mActivity.findViewById(R.id.transaction_list_progress_bar);
100 if (progressBar == null)
101 throw new RuntimeException("Can't get hold on the transaction list progress bar");
102 progressLayout = mActivity.findViewById(R.id.transaction_progress_layout);
103 if (progressLayout == null) throw new RuntimeException(
104 "Can't get hold on the transaction list progress bar layout");
105 tvLastUpdate = mActivity.findViewById(R.id.transactions_last_update);
106 updateLastUpdateText();
107 model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
108 modelAdapter = new TransactionListAdapter(model);
110 RecyclerView root = mActivity.findViewById(R.id.transaction_root);
111 root.setAdapter(modelAdapter);
113 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
115 llm.setOrientation(LinearLayoutManager.VERTICAL);
116 root.setLayoutManager(llm);
118 swiper.setOnRefreshListener(() -> {
119 Log.d("ui", "refreshing transactions via swipe");
120 update_transactions();
123 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
125 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
126 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
127 bTransactionListCancelDownload =
128 mActivity.findViewById(R.id.transaction_list_cancel_download);
130 TransactionListFragment me = this;
131 MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name");
132 accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
134 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
135 Log.d("tmp", "direct onItemClick");
136 model.reloadTransactions(me);
137 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
138 modelAdapter.setBoldAccountName(mc.getString(1));
139 modelAdapter.notifyDataSetChanged();
140 Globals.hideSoftKeyboard(mActivity);
144 updateLastUpdateText();
145 long last_update = MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
146 Log.d("transactions", String.format("Last update = %d", last_update));
147 if (last_update == 0) {
148 update_transactions();
151 model.reloadTransactions(this);
155 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
156 inflater.inflate(R.menu.transaction_list, menu);
158 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
159 if ((menuTransactionListFilter == null)) throw new AssertionError();
161 super.onCreateOptionsMenu(menu, inflater);
163 private void update_transactions() {
164 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
166 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
167 PreferenceManager.getDefaultSharedPreferences(mActivity));
169 retrieveTransactionsTask.execute(params);
170 bTransactionListCancelDownload.setEnabled(true);
172 public void onRetrieveStart() {
173 progressBar.setIndeterminate(true);
174 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
175 else progressBar.setProgress(0);
176 progressLayout.setVisibility(View.VISIBLE);
178 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
179 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
180 (progress.getTotal() == 0))
182 progressBar.setIndeterminate(true);
185 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
186 progressBar.setMin(0);
188 progressBar.setMax(progress.getTotal());
189 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
190 progressBar.setProgress(progress.getProgress(), true);
192 else progressBar.setProgress(progress.getProgress());
193 progressBar.setIndeterminate(false);
197 public void onRetrieveDone(boolean success) {
198 progressLayout.setVisibility(View.GONE);
199 swiper.setRefreshing(false);
200 updateLastUpdateText();
202 Log.d("transactions", "calling notifyDataSetChanged()");
203 modelAdapter.notifyDataSetChanged();
206 private void updateLastUpdateText() {
209 MLDB.get_option_value(mActivity, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
210 Log.d("transactions", String.format("Last update = %d", last_update));
211 if (last_update == 0) {
212 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
215 Date date = new Date(last_update);
216 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
217 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
218 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
221 tvLastUpdate.setText(date.toLocaleString());
226 public void onClearAccountNameClick(View view) {
227 vAccountFilter.setVisibility(View.GONE);
228 menuTransactionListFilter.setVisible(true);
229 accNameFilter.setText(null);
230 model.reloadTransactions(this);
231 modelAdapter.resetBoldAccountName();
232 modelAdapter.notifyDataSetChanged();
233 Globals.hideSoftKeyboard(mActivity);
235 public void onShowFilterClick(MenuItem menuItem) {
236 vAccountFilter.setVisibility(View.VISIBLE);
237 menuTransactionListFilter.setVisible(false);
238 accNameFilter.requestFocus();
239 InputMethodManager imm =
240 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
241 imm.showSoftInput(accNameFilter, 0);
243 public void onStopTransactionRefreshClick(View view) {
244 Log.d("interactive", "Cancelling transactions refresh");
245 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
246 bTransactionListCancelDownload.setEnabled(false);