From 65c5cdc30c7bcf5cf8d39df1dc20abba8d476670 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 7 Dec 2018 18:37:52 +0000 Subject: [PATCH] update accounts table visual upon refreshilg the account data from the backend --- .../net/ktnx/mobileledger/AccountSummary.java | 66 ++++++++++++++++++- .../res/layout/content_account_summary.xml | 44 ++++++------- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/styles.xml | 21 ++++++ 4 files changed, 108 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index ec122cd4..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,15 +15,22 @@ 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; import static android.view.View.GONE; +import static net.ktnx.mobileledger.MobileLedgerDB.db; import static net.ktnx.mobileledger.MobileLedgerDB.set_option_value; public class AccountSummary extends AppCompatActivity { @@ -89,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; } @@ -164,8 +173,11 @@ public class AccountSummary extends AppCompatActivity { 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 + } + else { set_option_value("last_refresh", new Date().getTime() ); + update_account_table(); + } } }; @@ -173,4 +185,54 @@ public class AccountSummary extends AppCompatActivity { 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); + } + } + } } diff --git a/app/src/main/res/layout/content_account_summary.xml b/app/src/main/res/layout/content_account_summary.xml index dfda28d1..89114417 100644 --- a/app/src/main/res/layout/content_account_summary.xml +++ b/app/src/main/res/layout/content_account_summary.xml @@ -2,6 +2,7 @@ + app:layout_constraintStart_toStartOf="parent" + tools:ignore="HardcodedText" /> - + - + android:layout_height="match_parent" + android:background="@color/table_row_even_bg"> - + - - - - + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index c9f43903..0b00bf98 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,5 @@ #9c27b0 #4a148c #4db6ac + #289d29b1 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 2ed9b110..e08c79c2 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -27,4 +27,25 @@ @style/TextAppearance.AppCompat.Body1 + + + + + + -- 2.39.5