]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java
update account list once every 24 hours
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / LatestTransactions.java
index 111b71e9f6ddceb36249116ac245a8cc7224ce95..27ea5cd27efd047b624e2f2590ce82b58c517134 100644 (file)
@@ -1,9 +1,11 @@
 package net.ktnx.mobileledger;
 
+import android.content.Intent;
 import android.content.pm.PackageInfo;
+import android.content.res.Resources;
+import android.os.Build;
 import android.os.Bundle;
-import android.support.design.widget.FloatingActionButton;
-import android.support.design.widget.NavigationView;
+import android.preference.PreferenceManager;
 import android.support.design.widget.Snackbar;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
@@ -14,9 +16,23 @@ import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
+import android.widget.ProgressBar;
+import android.widget.TextView;
 
-public class LatestTransactions extends AppCompatActivity
-        implements NavigationView.OnNavigationItemSelectedListener {
+import java.util.Date;
+
+import static android.view.View.GONE;
+import static net.ktnx.mobileledger.MobileLedgerDB.db;
+import static net.ktnx.mobileledger.MobileLedgerDB.set_option_value;
+
+public class LatestTransactions extends AppCompatActivity {
+    DrawerLayout drawer;
+
+    private static long account_list_last_updated;
+    private static boolean account_list_needs_update = true;
+    public static void preferences_changed() {
+        account_list_needs_update = true;
+    }
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -25,32 +41,28 @@ public class LatestTransactions extends AppCompatActivity
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
-        FloatingActionButton fab = findViewById(R.id.btn_add_transaction);
-        fab.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
-                        .setAction("Action", null).show();
-            }
-        });
-
-        DrawerLayout drawer = findViewById(R.id.drawer_layout);
+        drawer = findViewById(R.id.drawer_layout);
         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
         drawer.addDrawerListener(toggle);
         toggle.syncState();
 
-        NavigationView navigationView = findViewById(R.id.nav_view);
-        navigationView.setNavigationItemSelectedListener(this);
-
         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
 
         try {
             PackageInfo pi = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
             ver.setText(pi.versionName);
         } catch (Exception e) {
-            ver.setText("version");
+            e.printStackTrace();
         }
+
+        prepare_db();
+        update_accounts(false);
+    }
+
+    public void fab_new_transaction_clicked(View view) {
+        Intent intent = new Intent(this, NewTransactionActivity.class);
+        startActivity(intent);
     }
 
     public void nav_exit_clicked(View view) {
@@ -58,6 +70,11 @@ public class LatestTransactions extends AppCompatActivity
         finish();
     }
 
+    public void nav_settings_clicked(View view) {
+        Intent intent = new Intent(this, SettingsActivity.class);
+        startActivity(intent);
+    }
+
     @Override
     public void onBackPressed() {
         DrawerLayout drawer = findViewById(R.id.drawer_layout);
@@ -71,7 +88,7 @@ public class LatestTransactions extends AppCompatActivity
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
-        getMenuInflater().inflate(R.menu.latest_transactions, menu);
+        //getMenuInflater().inflate(R.menu.latest_transactions, menu);
         return true;
     }
 
@@ -83,31 +100,65 @@ public class LatestTransactions extends AppCompatActivity
         int id = item.getItemId();
 
         //noinspection SimplifiableIfStatement
-        if (id == R.id.action_settings) {
-            return true;
-        }
+        //if (id == R.id.action_settings) {
+        //    return true;
+        // }
 
         return super.onOptionsItemSelected(item);
     }
 
-    @SuppressWarnings("StatementWithEmptyBody")
-    @Override
-    public boolean onNavigationItemSelected(MenuItem item) {
-        // Handle navigation view item clicks here.
-        int id = item.getItemId();
-
-        if (id == R.id.nav_latest_transactions) {
-            // Handle the camera action
-        } else if (id == R.id.nav_reports) {
+    private void prepare_db() {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+            MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME);
+        }
+        else {
+            MobileLedgerDB.setDb_filename(MobileLedgerDB.DATABASE_NAME);
+        }
+        MobileLedgerDB.initDB();
 
-        } else if (id == R.id.nav_settings) {
+        account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0);
 
-        } else if (id == R.id.nav_exit) {
+    }
 
+    private void update_accounts(boolean force) {
+        long now = new Date().getTime();
+        if ((now > (account_list_last_updated + (24 * 3600*1000))) || force) {
+            Log.d("db", "accounts last updated at " + account_list_last_updated+" and now is " + now+". re-fetching");
+            update_accounts();
         }
+    }
+
+    private void update_accounts() {
+        Resources rm = getResources();
+
+        ProgressBar pb = findViewById(R.id.progressBar);
+        pb.setVisibility(View.VISIBLE);
+        TextView pt = findViewById(R.id.textProgress);
+        pt.setVisibility(View.VISIBLE);
+        pb.setIndeterminate(true);
+
+        RetrieveAccountsTask task = new RetrieveAccountsTask() {
+            @Override
+            protected void onProgressUpdate(Integer... values) {
+                if ( values[0] == 0 )
+                    pt.setText(R.string.progress_connecting);
+                else
+                    pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
+            }
+
+            @Override
+            protected void onPostExecute(Void result) {
+                pb.setVisibility(GONE);
+                pt.setVisibility(GONE);
+                if (this.error != 0)
+                    Snackbar.make(drawer, rm.getString(this.error), Snackbar.LENGTH_LONG );
+                else
+                    set_option_value("last_refresh", new Date().getTime() );
+            }
+        };
+
+        task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
+        task.execute(db);
 
-        DrawerLayout drawer = findViewById(R.id.drawer_layout);
-        drawer.closeDrawer(GravityCompat.START);
-        return true;
     }
 }