X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FAccountSummary.java;h=e19b8add56d0f5a8f3dba2f172fcd7fc9ab60df5;hp=bd73cd075aafbef4ee46b273645ffffdb30daa8d;hb=65c5cdc30c7bcf5cf8d39df1dc20abba8d476670;hpb=ac3b5b52cfdf268b05578acab1c098647543245b diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index bd73cd07..e19b8add 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -1,8 +1,10 @@ package net.ktnx.mobileledger; +import android.annotation.SuppressLint; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.res.Resources; +import android.database.Cursor; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; @@ -13,10 +15,16 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; +import android.util.TypedValue; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.TableLayout; +import android.widget.TableRow; import android.widget.TextView; import java.util.Date; @@ -90,7 +98,7 @@ public class AccountSummary extends AppCompatActivity { public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.account_summary, menu); - mRefresh = (MenuItem) menu.findItem(R.id.menu_acc_summary_refresh); + mRefresh = menu.findItem(R.id.menu_acc_summary_refresh); assert mRefresh != null; return true; } @@ -123,6 +131,8 @@ public class AccountSummary extends AppCompatActivity { } MobileLedgerDB.initDB(); + MobileLedgerDB.applyRevisions(getResources(), getPackageName()); + account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0); } @@ -159,15 +169,70 @@ public class AccountSummary extends AppCompatActivity { pb.setVisibility(GONE); pt.setVisibility(GONE); mRefresh.setVisible(true); - if (this.error != 0) - Snackbar.make(drawer, rm.getString(this.error), Snackbar.LENGTH_LONG ); - else + if (this.error != 0) { + String err_text = rm.getString(this.error); + Log.d("visual", String.format("showing snackbar: %s", err_text)); + Snackbar.make(drawer, err_text, Snackbar.LENGTH_LONG ).show(); + } + else { set_option_value("last_refresh", new Date().getTime() ); + update_account_table(); + } } }; task.setPref(PreferenceManager.getDefaultSharedPreferences(this)); - task.execute(db); + task.execute(); + + } + public int dp2px(float dp) { + return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics())); + } + + @SuppressLint("DefaultLocale") + private void update_account_table() { + LinearLayout root = findViewById(R.id.account_root); + root.removeAllViewsInLayout(); + + try (Cursor cursor = db.rawQuery("SELECT name FROM accounts ORDER BY name;", null)) { + boolean even = false; + while (cursor.moveToNext()) { + String acc_name = cursor.getString(0); + + TableLayout t = new TableLayout(this); + TableRow r = new TableRow(this); + r.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + if (even) + r.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme())); + even = !even; + + TextView acc_tv = new TextView(this, null, R.style.account_summary_account_name); + acc_tv.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 9f)); + acc_tv.setText(acc_name); + r.addView(acc_tv); + + TextView amt_tv = new TextView(this, null, R.style.account_summary_amounts); + amt_tv.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)); + amt_tv.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END); + amt_tv.setMinWidth(dp2px(40f)); + StringBuilder amt_text = new StringBuilder(); + try (Cursor cAmounts = db.rawQuery("SELECT currency, value FROM account_values WHERE account = ?", new String[]{acc_name})) { + while (cAmounts.moveToNext()) { + String curr = cAmounts.getString(0); + Float amt = cAmounts.getFloat(1); + if (amt_text.length() != 0) amt_text.append('\n'); + amt_text.append(String.format("%s %1.2f", curr, amt)); + } + } + amt_tv.setText(amt_text.toString()); + + r.addView(amt_tv); + + t.addView(r); + + root.addView(t); + } + } } }