]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java
load transaction list from DB upon activity start
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / TransactionListActivity.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;
19
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.LinearLayout;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
35
36 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
37 import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel;
38 import net.ktnx.mobileledger.utils.MLDB;
39
40 import java.lang.ref.WeakReference;
41 import java.time.ZoneId;
42 import java.time.format.DateTimeFormatter;
43 import java.util.Date;
44
45 public class TransactionListActivity extends AppCompatActivity {
46     public TransactionListViewModel model;
47     private SwipeRefreshLayout swiper;
48     private RecyclerView root;
49     private ProgressBar progressBar;
50     private LinearLayout progressLayout;
51     private TextView tvLastUpdate;
52     private TransactionListAdapter modelAdapter;
53     private RetrieveTransactionsTask retrieveTransactionsTask;
54     @Override
55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57         setContentView(R.layout.transaction_list_activity);
58
59         Toolbar toolbar = findViewById(R.id.toolbar);
60         setSupportActionBar(toolbar);
61
62         setupActionBar();
63
64         swiper = findViewById(R.id.transaction_swipe);
65         if (swiper == null) throw new RuntimeException("Can't get hold on the swipe layout");
66         root = findViewById(R.id.transaction_root);
67         if (root == null) throw new RuntimeException("Can't get hold on the transaction list view");
68         progressBar = findViewById(R.id.transaction_list_progress_bar);
69         if (progressBar == null)
70             throw new RuntimeException("Can't get hold on the transaction list progress bar");
71         progressLayout = findViewById(R.id.transaction_progress_layout);
72         if (progressLayout == null) throw new RuntimeException(
73                 "Can't get hold on the transaction list progress bar layout");
74         tvLastUpdate = findViewById(R.id.transactions_last_update);
75         updateLastUpdateText();
76         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
77         modelAdapter = new TransactionListAdapter(model);
78
79         RecyclerView root = findViewById(R.id.transaction_root);
80         root.setAdapter(modelAdapter);
81
82         LinearLayoutManager llm = new LinearLayoutManager(this);
83
84         llm.setOrientation(LinearLayoutManager.VERTICAL);
85         root.setLayoutManager(llm);
86
87         swiper.setOnRefreshListener(() -> {
88             Log.d("ui", "refreshing transactions via swipe");
89             update_transactions();
90         });
91
92         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
93
94         updateLastUpdateText();
95         long last_update = MLDB.get_option_value(this, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
96         Log.d("transactions", String.format("Last update = %d", last_update));
97         if (last_update == 0) {
98             update_transactions();
99         }
100         else {
101             model.reloadTransactions(this);
102         }
103     }
104     private void setupActionBar() {
105         ActionBar actionBar = getSupportActionBar();
106         if (actionBar != null) {
107             // Show the Up button in the action bar.
108             actionBar.setDisplayHomeAsUpEnabled(true);
109         }
110     }
111     @Override
112     public void finish() {
113         super.finish();
114         Log.d("visuals", "finishing");
115         overridePendingTransition(R.anim.dummy, R.anim.slide_out_right);
116     }
117     private void update_transactions() {
118         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
119
120         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
121                 PreferenceManager.getDefaultSharedPreferences(this));
122
123         retrieveTransactionsTask.execute(params);
124         findViewById(R.id.transaction_list_cancel_download).setEnabled(true);
125     }
126
127     public void onRetrieveStart() {
128         progressBar.setIndeterminate(true);
129         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
130         else progressBar.setProgress(0);
131         progressLayout.setVisibility(View.VISIBLE);
132     }
133     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
134         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
135             (progress.getTotal() == 0))
136         {
137             progressBar.setIndeterminate(true);
138         }
139         else {
140             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
141                 progressBar.setMin(0);
142             }
143             progressBar.setMax(progress.getTotal());
144             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
145                 progressBar.setProgress(progress.getProgress(), true);
146             }
147             else progressBar.setProgress(progress.getProgress());
148             progressBar.setIndeterminate(false);
149         }
150     }
151
152     public void onRetrieveDone(boolean success) {
153         progressLayout.setVisibility(View.GONE);
154         swiper.setRefreshing(false);
155         updateLastUpdateText();
156         if (success) {
157             Log.d("transactions", "calling notifyDataSetChanged()");
158             modelAdapter.notifyDataSetChanged();
159         }
160     }
161     private void updateLastUpdateText() {
162         {
163             long last_update = MLDB.get_option_value(this, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
164             Log.d("transactions", String.format("Last update = %d", last_update));
165             if (last_update == 0) {
166                 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
167             }
168             else {
169                 Date date = new Date(last_update);
170                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
171                     tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
172                             .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
173                 }
174                 else {
175                     tvLastUpdate.setText(date.toLocaleString());
176                 }
177             }
178         }
179     }
180     public void onStopTransactionRefreshClick(View view) {
181         Log.d("interactive", "Cancelling transactions refresh");
182         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
183         findViewById(R.id.transaction_list_cancel_download).setEnabled(false);
184     }
185 }