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