]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java
refresh the view when the transaction list is updated
[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         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         {
72             long last_update = (new MobileLedgerDatabase(this))
73                     .get_option_value("transaction_list_last_update", 0);
74             if (last_update == 0) tvLastUpdate.setText("never");
75             else {
76                 Date date = new Date(last_update);
77                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
78                     tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
79                             .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
80                 }
81                 else {
82                     tvLastUpdate.setText(date.toLocaleString());
83                 }
84             }
85         }
86         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
87         List<LedgerTransaction> transactions =
88                 model.getTransactions(new MobileLedgerDatabase(this));
89         modelAdapter = new TransactionListAdapter(transactions);
90
91         RecyclerView root = findViewById(R.id.transaction_root);
92         root.setAdapter(modelAdapter);
93
94         LinearLayoutManager llm = new LinearLayoutManager(this);
95         llm.setOrientation(LinearLayoutManager.VERTICAL);
96         root.setLayoutManager(llm);
97
98         swiper.setOnRefreshListener(() -> {
99             Log.d("ui", "refreshing transactions via swipe");
100             update_transactions();
101         });
102
103         swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent);
104
105 //        update_transactions();
106     }
107     private void setupActionBar() {
108         ActionBar actionBar = getSupportActionBar();
109         if (actionBar != null) {
110             // Show the Up button in the action bar.
111             actionBar.setDisplayHomeAsUpEnabled(true);
112         }
113     }
114     @Override
115     public void finish() {
116         super.finish();
117         Log.d("visuals", "finishing");
118         overridePendingTransition(R.anim.dummy, R.anim.slide_out_right);
119     }
120     private void update_transactions() {
121         RetrieveTransactionsTask task = new RetrieveTransactionsTask(new WeakReference<>(this));
122
123         RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params(
124                 PreferenceManager.getDefaultSharedPreferences(this));
125
126         task.execute(params);
127     }
128
129     public void onRetrieveStart() {
130         progressBar.setIndeterminate(true);
131         progressBar.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             progressBar.setIndeterminate(false);
141             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
142                 progressBar.setMin(0);
143             }
144             progressBar.setMax(progress.getTotal());
145             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
146                 progressBar.setProgress(progress.getProgress(), true);
147             }
148             else progressBar.setProgress(progress.getProgress());
149         }
150     }
151
152     public void onRetrieveDone(boolean success) {
153         progressBar.setVisibility(View.GONE);
154         swiper.setRefreshing(false);
155         if (success) {
156             MobileLedgerDatabase dbh = new MobileLedgerDatabase(this);
157             Date now = new Date();
158             dbh.set_option_value("transaction_list_last_update", now.getTime());
159             updateLastUpdateText(now);
160             modelAdapter.notifyDataSetChanged();
161         }
162     }
163     private void updateLastUpdateText(Date now) {
164         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
165             tvLastUpdate.setText(now.toInstant().atZone(ZoneId.systemDefault()).toString());
166         }
167         else {
168             tvLastUpdate.setText(now.toLocaleString());
169         }
170     }
171 }