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