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=f8c70604c99b5c2f6c1dfce45b9aeca672fcba6f;hb=5e2dc873b9c8cb41c1065bd14379ab2dfeb0dac1;hpb=33c7adcf50991dfa6796dd042d26d46839aaee43 diff --git a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java index f8c70604..27ea5cd2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java +++ b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java @@ -1,21 +1,38 @@ 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; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +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) { @@ -24,32 +41,38 @@ 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) { + Log.w("mobileledger", "exiting"); + finish(); + } + + public void nav_settings_clicked(View view) { + Intent intent = new Intent(this, SettingsActivity.class); + startActivity(intent); } @Override @@ -65,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; } @@ -77,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; } }