fix snack-bar showing in case of account update errors
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummary.java
1 package net.ktnx.mobileledger;
2
3 import android.content.Intent;
4 import android.content.pm.PackageInfo;
5 import android.content.res.Resources;
6 import android.os.Build;
7 import android.os.Bundle;
8 import android.preference.PreferenceManager;
9 import android.support.design.widget.Snackbar;
10 import android.support.v4.view.GravityCompat;
11 import android.support.v4.widget.DrawerLayout;
12 import android.support.v7.app.ActionBarDrawerToggle;
13 import android.support.v7.app.AppCompatActivity;
14 import android.support.v7.widget.Toolbar;
15 import android.util.Log;
16 import android.view.Menu;
17 import android.view.MenuItem;
18 import android.view.View;
19 import android.widget.ProgressBar;
20 import android.widget.TextView;
21
22 import java.util.Date;
23
24 import static android.view.View.GONE;
25 import static net.ktnx.mobileledger.MobileLedgerDB.db;
26 import static net.ktnx.mobileledger.MobileLedgerDB.set_option_value;
27
28 public class AccountSummary extends AppCompatActivity {
29     DrawerLayout drawer;
30
31     private static long account_list_last_updated;
32     private static boolean account_list_needs_update = true;
33     public static void preferences_changed() {
34         account_list_needs_update = true;
35     }
36     MenuItem mRefresh;
37
38     @Override
39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.activity_latest_transactions);
42         Toolbar toolbar = findViewById(R.id.toolbar);
43         setSupportActionBar(toolbar);
44
45         drawer = findViewById(R.id.drawer_layout);
46         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
47                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
48         drawer.addDrawerListener(toggle);
49         toggle.syncState();
50
51         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
52
53         try {
54             PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
55             ver.setText(pi.versionName);
56         } catch (Exception e) {
57             e.printStackTrace();
58         }
59
60         prepare_db();
61         update_accounts(false);
62     }
63
64     public void fab_new_transaction_clicked(View view) {
65         Intent intent = new Intent(this, NewTransactionActivity.class);
66         startActivity(intent);
67     }
68
69     public void nav_exit_clicked(View view) {
70         Log.w("mobileledger", "exiting");
71         finish();
72     }
73
74     public void nav_settings_clicked(View view) {
75         Intent intent = new Intent(this, SettingsActivity.class);
76         startActivity(intent);
77     }
78
79     @Override
80     public void onBackPressed() {
81         DrawerLayout drawer = findViewById(R.id.drawer_layout);
82         if (drawer.isDrawerOpen(GravityCompat.START)) {
83             drawer.closeDrawer(GravityCompat.START);
84         } else {
85             super.onBackPressed();
86         }
87     }
88
89     @Override
90     public boolean onCreateOptionsMenu(Menu menu) {
91         // Inflate the menu; this adds items to the action bar if it is present.
92         getMenuInflater().inflate(R.menu.account_summary, menu);
93         mRefresh = (MenuItem) menu.findItem(R.id.menu_acc_summary_refresh);
94         assert mRefresh != null;
95         return true;
96     }
97
98     @Override
99     public boolean onOptionsItemSelected(MenuItem item) {
100         // Handle action bar item clicks here. The action bar will
101         // automatically handle clicks on the Home/Up button, so long
102         // as you specify a parent activity in AndroidManifest.xml.
103         int id = item.getItemId();
104
105         //noinspection SimplifiableIfStatement
106         //if (id == R.id.action_settings) {
107         //    return true;
108         // }
109
110         return super.onOptionsItemSelected(item);
111     }
112
113     public void onRefreshAccountSummaryClicked(MenuItem mi) {
114         update_accounts(true);
115     }
116
117     private void prepare_db() {
118         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
119             MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME);
120         }
121         else {
122             MobileLedgerDB.setDb_filename(MobileLedgerDB.DATABASE_NAME);
123         }
124         MobileLedgerDB.initDB();
125
126         MobileLedgerDB.applyRevisions(getResources(), getPackageName());
127
128         account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0);
129
130     }
131
132     private void update_accounts(boolean force) {
133         long now = new Date().getTime();
134         if ((now > (account_list_last_updated + (24 * 3600*1000))) || force) {
135             Log.d("db", "accounts last updated at " + account_list_last_updated+" and now is " + now+". re-fetching");
136             update_accounts();
137         }
138     }
139
140     private void update_accounts() {
141         mRefresh.setVisible(false);
142         Resources rm = getResources();
143
144         ProgressBar pb = findViewById(R.id.progressBar);
145         pb.setVisibility(View.VISIBLE);
146         TextView pt = findViewById(R.id.textProgress);
147         pt.setVisibility(View.VISIBLE);
148         pb.setIndeterminate(true);
149
150         RetrieveAccountsTask task = new RetrieveAccountsTask() {
151             @Override
152             protected void onProgressUpdate(Integer... values) {
153                 if ( values[0] == 0 )
154                     pt.setText(R.string.progress_connecting);
155                 else
156                     pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
157             }
158
159             @Override
160             protected void onPostExecute(Void result) {
161                 pb.setVisibility(GONE);
162                 pt.setVisibility(GONE);
163                 mRefresh.setVisible(true);
164                 if (this.error != 0) {
165                     String err_text = rm.getString(this.error);
166                     Log.d("visual", String.format("showing snackbar: %s", err_text));
167                     Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG ).show();
168                 } else
169                     set_option_value("last_refresh", new Date().getTime() );
170             }
171         };
172
173         task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
174         task.execute(db);
175
176     }
177 }