update accounts table visual upon refreshilg the account data from the backend
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 7 Dec 2018 18:37:52 +0000 (18:37 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 7 Dec 2018 18:37:52 +0000 (18:37 +0000)
app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
app/src/main/res/layout/content_account_summary.xml
app/src/main/res/values/colors.xml
app/src/main/res/values/styles.xml

index ec122cd44627a88475f0d57d57b06a7fb42d006a..e19b8add56d0f5a8f3dba2f172fcd7fc9ab60df5 100644 (file)
@@ -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);
+            }
+        }
+    }
 }
index dfda28d1ac219868162dd4050eabb2db056a9225..89114417cafa253d98b87b3510c8265df2bd3e4e 100644 (file)
@@ -2,6 +2,7 @@
 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/content_account_summary_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
@@ -16,7 +17,8 @@
         android:visibility="gone"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent" />
+        app:layout_constraintStart_toStartOf="parent"
+        tools:ignore="HardcodedText" />
 
     <ProgressBar
         android:id="@+id/progressBar"
         app:layout_constraintStart_toStartOf="parent" />
 
     <ScrollView
+        android:id="@+id/account_root_scroller"
         android:layout_width="match_parent"
-        android:layout_height="325dp"
-        android:layout_marginTop="8dp"
-        android:layout_marginBottom="40dp"
+        android:layout_height="match_parent"
+        android:paddingStart="@dimen/activity_horizontal_margin"
+        android:paddingEnd="@dimen/activity_horizontal_margin"
         app:layout_constraintBottom_toTopOf="@+id/textProgress"
         app:layout_constraintTop_toTopOf="parent">
 
         <LinearLayout
+            android:id="@+id/account_root"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="vertical">
 
-            <ScrollView
-                android:layout_width="match_parent"
-                android:layout_height="match_parent"
-                android:paddingLeft="@dimen/activity_horizontal_margin">
+            <TableLayout style="@style/account_summary_account_entry_table">
 
-                <LinearLayout
+                <TableRow
                     android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
-                    android:orientation="vertical">
+                    android:layout_height="match_parent"
+                    android:background="@color/table_row_even_bg">
 
-                    <ScrollView
-                        android:layout_width="match_parent"
-                        android:layout_height="match_parent"
-                        android:paddingLeft="@dimen/activity_horizontal_margin">
+                    <TextView
+                        android:id="@+id/textView"
+                        style="@style/account_summary_account_name"
+                        android:text="TextView" />
 
-                        <LinearLayout
-                            android:layout_width="match_parent"
-                            android:layout_height="wrap_content"
-                            android:orientation="vertical" />
-                    </ScrollView>
-                </LinearLayout>
-            </ScrollView>
+                    <TextView
+                        android:id="@+id/textView2"
+                        style="@style/account_summary_amounts"
+                        android:text="123.45" />
+                </TableRow>
 
+            </TableLayout>
         </LinearLayout>
     </ScrollView>
 
index c9f4390355c32fa6b457c93188d2ab2d6a4aa8df..0b00bf98bc147ff4bdb59c607ea378dba20b4113 100644 (file)
@@ -3,4 +3,5 @@
     <color name="colorPrimary">#9c27b0</color>
     <color name="colorPrimaryDark">#4a148c</color>
     <color name="colorAccent">#4db6ac</color>
+    <color name="table_row_even_bg">#289d29b1</color>
 </resources>
index 2ed9b11029451526ac37408e4589e56f0cc727c5..e08c79c2141082426bbecfa97e225f96534ebcab 100644 (file)
         <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
     </style>
 
+    <style name="account_summary_account_name">
+        <item name="android:layout_width">match_parent</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:layout_weight">9</item>
+    </style>
+
+    <style name="account_summary_amounts">
+        <item name="android:layout_width">wrap_content</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:layout_marginEnd">0dp</item>
+        <item name="android:layout_weight">1</item>
+        <item name="android:width">0dp</item>
+        <item name="android:minWidth">40dp</item>
+        <item name="android:textAlignment">viewEnd</item>
+    </style>
+
+    <style name="account_summary_account_entry_table">
+        <item name="android:layout_width">match_parent</item>
+        <item name="android:layout_height">match_parent</item>
+    </style>
+
 </resources>