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