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;
20 import android.arch.lifecycle.ViewModelProviders;
21 import android.os.Build;
22 import android.os.Bundle;
23 import android.preference.PreferenceManager;
24 import android.support.v4.widget.SwipeRefreshLayout;
25 import android.support.v7.app.ActionBar;
26 import android.support.v7.app.AppCompatActivity;
27 import android.support.v7.widget.LinearLayoutManager;
28 import android.support.v7.widget.RecyclerView;
29 import android.support.v7.widget.Toolbar;
30 import android.util.Log;
31 import android.view.View;
32 import android.widget.ProgressBar;
33 import android.widget.TextView;
35 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
38 import net.ktnx.mobileledger.utils.MLDB;
40 import java.lang.ref.WeakReference;
41 import java.time.ZoneId;
42 import java.time.format.DateTimeFormatter;
43 import java.util.Date;
44 import java.util.List;
46 public class TransactionListActivity extends AppCompatActivity {
47 public TransactionListViewModel model;
48 private SwipeRefreshLayout swiper;
49 private RecyclerView root;
50 private ProgressBar progressBar;
51 private TextView tvLastUpdate;
52 private TransactionListAdapter modelAdapter;
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.transaction_list_activity);
58 Toolbar toolbar = findViewById(R.id.toolbar);
59 setSupportActionBar(toolbar);
63 swiper = findViewById(R.id.transaction_swipe);
64 if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
65 root = findViewById(R.id.transaction_root);
66 if (root == null) throw new RuntimeException("Can't get hold on the transaction list view");
67 progressBar = findViewById(R.id.transaction_progress_bar);
68 if (progressBar == null)
69 throw new RuntimeException("Can't get hold on the transaction list progress bar");
70 tvLastUpdate = findViewById(R.id.transactions_last_update);
71 updateLastUpdateText();
72 model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
73 List<LedgerTransaction> transactions = model.getTransactions(getApplicationContext());
74 modelAdapter = new TransactionListAdapter(transactions);
76 RecyclerView root = findViewById(R.id.transaction_root);
77 root.setAdapter(modelAdapter);
79 LinearLayoutManager llm = new LinearLayoutManager(this);
81 llm.setOrientation(LinearLayoutManager.VERTICAL);
82 root.setLayoutManager(llm);
84 swiper.setOnRefreshListener(() -> {
85 Log.d("ui", "refreshing transactions via swipe");
86 update_transactions();
89 swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
91 // update_transactions();
93 private void setupActionBar() {
94 ActionBar actionBar = getSupportActionBar();
95 if (actionBar != null) {
96 // Show the Up button in the action bar.
97 actionBar.setDisplayHomeAsUpEnabled(true);
101 public void finish() {
103 Log.d("visuals", "finishing");
104 overridePendingTransition(R.anim.dummy, R.anim.slide_out_right);
106 private void update_transactions() {
107 RetrieveTransactionsTask task = new RetrieveTransactionsTask(new WeakReference<>(this));
109 RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
110 PreferenceManager.getDefaultSharedPreferences(this));
112 task.execute(params);
115 public void onRetrieveStart() {
116 progressBar.setIndeterminate(true);
117 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
118 else progressBar.setProgress(0);
119 progressBar.setVisibility(View.VISIBLE);
121 public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
122 if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
123 (progress.getTotal() == 0))
125 progressBar.setIndeterminate(true);
128 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
129 progressBar.setMin(0);
131 progressBar.setMax(progress.getTotal());
132 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
133 progressBar.setProgress(progress.getProgress(), true);
135 else progressBar.setProgress(progress.getProgress());
136 progressBar.setIndeterminate(false);
140 public void onRetrieveDone(boolean success) {
141 progressBar.setVisibility(View.GONE);
142 swiper.setRefreshing(false);
143 updateLastUpdateText();
145 modelAdapter.notifyDataSetChanged();
148 private void updateLastUpdateText() {
150 long last_update = MLDB.get_option_value(this, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
151 Log.d("transactions", String.format("Last update = %d", last_update));
152 if (last_update == 0) tvLastUpdate.setText("never");
154 Date date = new Date(last_update);
155 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
156 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
157 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
160 tvLastUpdate.setText(date.toLocaleString());