]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java
abandon the new transaction FAB and use a menu item
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / LatestTransactions.java
1 package net.ktnx.mobileledger;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.content.pm.PackageInfo;
6 import android.os.Bundle;
7 import android.preference.PreferenceManager;
8 import android.support.design.widget.Snackbar;
9 import android.support.v4.view.GravityCompat;
10 import android.support.v4.widget.DrawerLayout;
11 import android.support.v7.app.ActionBarDrawerToggle;
12 import android.support.v7.app.AppCompatActivity;
13 import android.support.v7.widget.Toolbar;
14 import android.util.Log;
15 import android.view.Menu;
16 import android.view.MenuItem;
17 import android.view.View;
18 import android.widget.ProgressBar;
19 import android.widget.TextView;
20
21 import java.util.Date;
22
23 import static android.view.View.GONE;
24 import static net.ktnx.mobileledger.MobileLedgerDB.db;
25
26 public class LatestTransactions extends AppCompatActivity {
27     DrawerLayout drawer;
28
29     private static Date account_list_last_updated;
30     private static boolean account_list_needs_update = true;
31     public static void preferences_changed() {
32         account_list_needs_update = true;
33     }
34
35     @Override
36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setContentView(R.layout.activity_latest_transactions);
39         Toolbar toolbar = findViewById(R.id.toolbar);
40         setSupportActionBar(toolbar);
41
42         drawer = findViewById(R.id.drawer_layout);
43         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
44                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
45         drawer.addDrawerListener(toggle);
46         toggle.syncState();
47
48         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
49
50         try {
51             PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
52             ver.setText(pi.versionName);
53         } catch (Exception e) {
54         }
55
56         update_accounts();
57     }
58
59     public void fab_new_transaction_clicked(View view) {
60         Intent intent = new Intent(this, NewTransactionActivity.class);
61         startActivity(intent);
62     }
63
64     public void nav_exit_clicked(View view) {
65         Log.w("mobileledger", "exiting");
66         finish();
67     }
68
69     public void nav_settings_clicked(View view) {
70         Intent intent = new Intent(this, SettingsActivity.class);
71         startActivity(intent);
72     }
73
74     @Override
75     public void onBackPressed() {
76         DrawerLayout drawer = findViewById(R.id.drawer_layout);
77         if (drawer.isDrawerOpen(GravityCompat.START)) {
78             drawer.closeDrawer(GravityCompat.START);
79         } else {
80             super.onBackPressed();
81         }
82     }
83
84     @Override
85     public boolean onCreateOptionsMenu(Menu menu) {
86         // Inflate the menu; this adds items to the action bar if it is present.
87         //getMenuInflater().inflate(R.menu.latest_transactions, menu);
88         return true;
89     }
90
91     @Override
92     public boolean onOptionsItemSelected(MenuItem item) {
93         // Handle action bar item clicks here. The action bar will
94         // automatically handle clicks on the Home/Up button, so long
95         // as you specify a parent activity in AndroidManifest.xml.
96         int id = item.getItemId();
97
98         //noinspection SimplifiableIfStatement
99         //if (id == R.id.action_settings) {
100         //    return true;
101         // }
102
103         return super.onOptionsItemSelected(item);
104     }
105
106     private void prepare_db() {
107         MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME);
108         MobileLedgerDB.initDB();
109     }
110     private void update_accounts() {
111         prepare_db();
112
113         Activity activity = this;
114
115         ProgressBar pb = findViewById(R.id.progressBar);
116         TextView pt = findViewById(R.id.textProgress);
117         pb.setIndeterminate(true);
118
119         RetrieveAccountsTask task = new RetrieveAccountsTask() {
120             @Override
121             protected void onProgressUpdate(Integer... values) {
122                 if ( values[0] == 0 )
123                     pt.setText(R.string.progress_connecting);
124                 else
125                     pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
126             }
127
128             @Override
129             protected void onPostExecute(Void result) {
130                 pb.setVisibility(GONE);
131                 pt.setVisibility(GONE);
132                 if (this.error != 0)
133                     Snackbar.make(drawer, activity.getResources().getString(this.error), Snackbar.LENGTH_LONG );
134             }
135         };
136
137         task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
138         task.execute(db);
139
140     }
141 }