]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java
43f58e556c3559498b3ef9deed795cf74ecf63aa
[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             e.printStackTrace();
55         }
56
57         update_accounts();
58     }
59
60     public void fab_new_transaction_clicked(View view) {
61         Intent intent = new Intent(this, NewTransactionActivity.class);
62         startActivity(intent);
63     }
64
65     public void nav_exit_clicked(View view) {
66         Log.w("mobileledger", "exiting");
67         finish();
68     }
69
70     public void nav_settings_clicked(View view) {
71         Intent intent = new Intent(this, SettingsActivity.class);
72         startActivity(intent);
73     }
74
75     @Override
76     public void onBackPressed() {
77         DrawerLayout drawer = findViewById(R.id.drawer_layout);
78         if (drawer.isDrawerOpen(GravityCompat.START)) {
79             drawer.closeDrawer(GravityCompat.START);
80         } else {
81             super.onBackPressed();
82         }
83     }
84
85     @Override
86     public boolean onCreateOptionsMenu(Menu menu) {
87         // Inflate the menu; this adds items to the action bar if it is present.
88         //getMenuInflater().inflate(R.menu.latest_transactions, menu);
89         return true;
90     }
91
92     @Override
93     public boolean onOptionsItemSelected(MenuItem item) {
94         // Handle action bar item clicks here. The action bar will
95         // automatically handle clicks on the Home/Up button, so long
96         // as you specify a parent activity in AndroidManifest.xml.
97         int id = item.getItemId();
98
99         //noinspection SimplifiableIfStatement
100         //if (id == R.id.action_settings) {
101         //    return true;
102         // }
103
104         return super.onOptionsItemSelected(item);
105     }
106
107     private void prepare_db() {
108         MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME);
109         MobileLedgerDB.initDB();
110     }
111     private void update_accounts() {
112         prepare_db();
113
114         Activity activity = this;
115
116         ProgressBar pb = findViewById(R.id.progressBar);
117         TextView pt = findViewById(R.id.textProgress);
118         pb.setIndeterminate(true);
119
120         RetrieveAccountsTask task = new RetrieveAccountsTask() {
121             @Override
122             protected void onProgressUpdate(Integer... values) {
123                 if ( values[0] == 0 )
124                     pt.setText(R.string.progress_connecting);
125                 else
126                     pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
127             }
128
129             @Override
130             protected void onPostExecute(Void result) {
131                 pb.setVisibility(GONE);
132                 pt.setVisibility(GONE);
133                 if (this.error != 0)
134                     Snackbar.make(drawer, activity.getResources().getString(this.error), Snackbar.LENGTH_LONG );
135             }
136         };
137
138         task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
139         task.execute(db);
140
141     }
142 }