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.
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.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;
46 import net.ktnx.mobileledger.R;
47 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
48 import net.ktnx.mobileledger.ui.activity.MainActivity;
49 import net.ktnx.mobileledger.utils.Globals;
50 import net.ktnx.mobileledger.utils.MLDB;
52 import java.lang.ref.WeakReference;
54 import static android.content.Context.INPUT_METHOD_SERVICE;
56 public class TransactionListFragment extends Fragment {
57 public static final String BUNDLE_KEY_FILTER_ACCOUNT_NAME = "filter_account_name";
58 public TransactionListViewModel model;
59 private String mShowOnlyAccountName;
60 private MainActivity mActivity;
61 private View bTransactionListCancelDownload;
62 private MenuItem menuTransactionListFilter;
63 private View vAccountFilter;
64 private SwipeRefreshLayout swiper;
65 private RecyclerView root;
66 private ProgressBar progressBar;
67 private LinearLayout progressLayout;
68 private TransactionListAdapter modelAdapter;
69 private RetrieveTransactionsTask retrieveTransactionsTask;
70 private AutoCompleteTextView accNameFilter;
71 public void setShowOnlyAccountName(String mShowOnlyAccountName) {
72 this.mShowOnlyAccountName = mShowOnlyAccountName;
73 if (modelAdapter != null) {
74 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
76 if (accNameFilter != null) {
77 accNameFilter.setText(mShowOnlyAccountName, false);
79 if (vAccountFilter != null) {
80 vAccountFilter.setVisibility(
81 ((mShowOnlyAccountName != null) && !mShowOnlyAccountName.isEmpty())
82 ? View.VISIBLE : View.GONE);
86 public void setArguments(@Nullable Bundle args) {
87 super.setArguments(args);
88 mShowOnlyAccountName = args.getString(BUNDLE_KEY_FILTER_ACCOUNT_NAME);
91 public void onCreate(@Nullable Bundle savedInstanceState) {
92 super.onCreate(savedInstanceState);
93 setHasOptionsMenu(true);
96 public void onAttach(Context context) {
97 super.onAttach(context);
98 mActivity = (MainActivity) context;
102 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
103 @Nullable Bundle savedInstanceState) {
104 return inflater.inflate(R.layout.transaction_list_fragment, container, false);
108 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
109 Log.d("flow", "TransactionListFragment.onActivityCreated called");
110 super.onActivityCreated(savedInstanceState);
112 mActivity.markDrawerItemCurrent(R.id.nav_latest_transactions);
114 swiper = mActivity.findViewById(R.id.transaction_swipe);
115 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
116 root = mActivity.findViewById(R.id.transaction_root);
117 if (root == null) throw new RuntimeException("Can't get hold on the transaction list view");
118 progressBar = mActivity.findViewById(R.id.transaction_list_progress_bar);
119 if (progressBar == null)
120 throw new RuntimeException("Can't get hold on the transaction list progress bar");
121 progressLayout = mActivity.findViewById(R.id.transaction_progress_layout);
122 if (progressLayout == null) throw new RuntimeException(
123 "Can't get hold on the transaction list progress bar layout");
124 model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
125 modelAdapter = new TransactionListAdapter(model);
127 modelAdapter.setBoldAccountName(mShowOnlyAccountName);
129 FloatingActionButton fab = mActivity.findViewById(R.id.btn_add_transaction);
131 RecyclerView root = mActivity.findViewById(R.id.transaction_root);
132 root.setAdapter(modelAdapter);
135 root.addOnScrollListener(new RecyclerView.OnScrollListener() {
137 public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
139 if (dy < 0) fab.show();
140 if (dy > 0) fab.hide();
145 LinearLayoutManager llm = new LinearLayoutManager(mActivity);
147 llm.setOrientation(LinearLayoutManager.VERTICAL);
148 root.setLayoutManager(llm);
150 swiper.setOnRefreshListener(() -> {
151 Log.d("ui", "refreshing transactions via swipe");
152 update_transactions();
155 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
157 vAccountFilter = mActivity.findViewById(R.id.transaction_list_account_name_filter);
158 accNameFilter = mActivity.findViewById(R.id.transaction_filter_account_name);
159 bTransactionListCancelDownload =
160 mActivity.findViewById(R.id.transaction_list_cancel_download);
162 TransactionListFragment me = this;
163 MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name");
164 accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
166 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
167 Log.d("tmp", "direct onItemClick");
168 model.reloadTransactions(me);
169 MatrixCursor mc = (MatrixCursor) parent.getItemAtPosition(position);
170 modelAdapter.setBoldAccountName(mc.getString(1));
171 modelAdapter.notifyDataSetChanged();
172 Globals.hideSoftKeyboard(mActivity);
176 if (mShowOnlyAccountName != null) {
177 accNameFilter.setText(mShowOnlyAccountName, false);
178 onShowFilterClick(null);
179 Log.d("flow", String.format("Account filter set to '%s'", mShowOnlyAccountName));
182 model.reloadTransactions(this);
185 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
186 inflater.inflate(R.menu.transaction_list, menu);
188 menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
189 if ((menuTransactionListFilter == null)) throw new AssertionError();
191 if (mShowOnlyAccountName != null) {
192 menuTransactionListFilter.setVisible(false);
195 super.onCreateOptionsMenu(menu, inflater);
197 private void update_transactions() {
198 retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
200 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
201 PreferenceManager.getDefaultSharedPreferences(mActivity));
203 retrieveTransactionsTask.execute(params);
204 bTransactionListCancelDownload.setEnabled(true);
206 public void onRetrieveStart() {
207 progressBar.setIndeterminate(true);
208 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
209 else progressBar.setProgress(0);
210 progressLayout.setVisibility(View.VISIBLE);
212 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
213 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
214 (progress.getTotal() == 0))
216 progressBar.setIndeterminate(true);
219 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
220 progressBar.setMin(0);
222 progressBar.setMax(progress.getTotal());
223 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
224 progressBar.setProgress(progress.getProgress(), true);
226 else progressBar.setProgress(progress.getProgress());
227 progressBar.setIndeterminate(false);
231 public void onRetrieveDone(boolean success) {
232 progressLayout.setVisibility(View.GONE);
233 swiper.setRefreshing(false);
234 mActivity.updateLastUpdateText();
236 Log.d("transactions", "calling notifyDataSetChanged()");
237 modelAdapter.notifyDataSetChanged();
240 public void onClearAccountNameClick(View view) {
241 vAccountFilter.setVisibility(View.GONE);
242 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(true);
243 accNameFilter.setText(null);
244 mShowOnlyAccountName = null;
245 model.reloadTransactions(this);
246 modelAdapter.resetBoldAccountName();
247 modelAdapter.notifyDataSetChanged();
248 Globals.hideSoftKeyboard(mActivity);
250 public void onShowFilterClick(MenuItem menuItem) {
251 vAccountFilter.setVisibility(View.VISIBLE);
252 if (menuTransactionListFilter != null) menuTransactionListFilter.setVisible(false);
253 if (menuItem != null) {
254 accNameFilter.requestFocus();
255 InputMethodManager imm =
256 (InputMethodManager) mActivity.getSystemService(INPUT_METHOD_SERVICE);
257 imm.showSoftInput(accNameFilter, 0);
260 public void onStopTransactionRefreshClick(View view) {
261 Log.d("interactive", "Cancelling transactions refresh");
262 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
263 bTransactionListCancelDownload.setEnabled(false);