X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FLatestTransactions.java;h=27ea5cd27efd047b624e2f2590ce82b58c517134;hp=3cd6e10aaa5fdff51fda7b9ac32c539182d1d13c;hb=5e2dc873b9c8cb41c1065bd14379ab2dfeb0dac1;hpb=978edb3b522d7af4dbeea8c3eab1d3f199a24f08 diff --git a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java index 3cd6e10a..27ea5cd2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java +++ b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java @@ -1,8 +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.preference.PreferenceManager; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; @@ -13,8 +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; + +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) { @@ -23,16 +41,7 @@ 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); @@ -44,8 +53,16 @@ public class LatestTransactions extends AppCompatActivity { 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) { @@ -53,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); @@ -66,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; } @@ -78,11 +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); } + 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(); + + account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0); + + } + + 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); + + } }