]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java
72e07b27ba9229ea08599834f72dad2357b970e1
[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.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;
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 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;
73     @Override
74     public void onCreate(@Nullable Bundle savedInstanceState) {
75         super.onCreate(savedInstanceState);
76         setHasOptionsMenu(true);
77     }
78     @Override
79     public void onAttach(Context context) {
80         super.onAttach(context);
81         mActivity = (MainActivity) context;
82     }
83     @Nullable
84     @Override
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);
88     }
89
90     @Override
91     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
92         Log.d("flow", "TransactionListFragment.onActivityCreated called");
93         super.onActivityCreated(savedInstanceState);
94
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);
109
110         RecyclerView root = mActivity.findViewById(R.id.transaction_root);
111         root.setAdapter(modelAdapter);
112
113         LinearLayoutManager llm = new LinearLayoutManager(mActivity);
114
115         llm.setOrientation(LinearLayoutManager.VERTICAL);
116         root.setLayoutManager(llm);
117
118         swiper.setOnRefreshListener(() -> {
119             Log.d("ui", "refreshing transactions via swipe");
120             update_transactions();
121         });
122
123         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
124
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);
129
130         TransactionListFragment me = this;
131         MLDB.hook_autocompletion_adapter(mActivity, accNameFilter, "accounts", "name");
132         accNameFilter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
133             @Override
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);
141             }
142         });
143
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();
149         }
150         else {
151             model.reloadTransactions(this);
152         }
153     }
154     @Override
155     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
156         inflater.inflate(R.menu.transaction_list, menu);
157
158         menuTransactionListFilter = menu.findItem(R.id.menu_transaction_list_filter);
159         if ((menuTransactionListFilter == null)) throw new AssertionError();
160
161         super.onCreateOptionsMenu(menu, inflater);
162     }
163     private void update_transactions() {
164         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
165
166         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
167                 PreferenceManager.getDefaultSharedPreferences(mActivity));
168
169         retrieveTransactionsTask.execute(params);
170         bTransactionListCancelDownload.setEnabled(true);
171     }
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);
177     }
178     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
179         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
180             (progress.getTotal() == 0))
181         {
182             progressBar.setIndeterminate(true);
183         }
184         else {
185             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
186                 progressBar.setMin(0);
187             }
188             progressBar.setMax(progress.getTotal());
189             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
190                 progressBar.setProgress(progress.getProgress(), true);
191             }
192             else progressBar.setProgress(progress.getProgress());
193             progressBar.setIndeterminate(false);
194         }
195     }
196
197     public void onRetrieveDone(boolean success) {
198         progressLayout.setVisibility(View.GONE);
199         swiper.setRefreshing(false);
200         updateLastUpdateText();
201         if (success) {
202             Log.d("transactions", "calling notifyDataSetChanged()");
203             modelAdapter.notifyDataSetChanged();
204         }
205     }
206     private void updateLastUpdateText() {
207         {
208             long last_update =
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));
213             }
214             else {
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));
219                 }
220                 else {
221                     tvLastUpdate.setText(date.toLocaleString());
222                 }
223             }
224         }
225     }
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);
234     }
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);
242     }
243     public void onStopTransactionRefreshClick(View view) {
244         Log.d("interactive", "Cancelling transactions refresh");
245         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
246         bTransactionListCancelDownload.setEnabled(false);
247     }
248 }