]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
rework account symmary activity using linear layouts
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummary.java
index bd73cd075aafbef4ee46b273645ffffdb30daa8d..2b05c1b3b2559435208dc6455c4db3b1d4b6e67a 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,13 +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.ContextMenu;
+import android.view.Gravity;
 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.TextView;
+import android.widget.Toast;
 
 import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import static android.view.View.GONE;
 import static net.ktnx.mobileledger.MobileLedgerDB.db;
@@ -30,6 +41,8 @@ public class AccountSummary extends AppCompatActivity {
 
     private static long account_list_last_updated;
     private static boolean account_list_needs_update = true;
+    private LinearLayout clickedAccountRow;
+
     public static void preferences_changed() {
         account_list_needs_update = true;
     }
@@ -38,7 +51,7 @@ public class AccountSummary extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_latest_transactions);
+        setContentView(R.layout.activity_account_summary);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
@@ -58,6 +71,7 @@ public class AccountSummary extends AppCompatActivity {
         }
 
         prepare_db();
+        update_account_table();
         update_accounts(false);
     }
 
@@ -90,8 +104,8 @@ 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);
-        assert mRefresh != null;
+        mRefresh = menu.findItem(R.id.menu_acc_summary_refresh);
+        if (mRefresh == null) throw new AssertionError();
         return true;
     }
 
@@ -123,6 +137,8 @@ public class AccountSummary extends AppCompatActivity {
         }
         MobileLedgerDB.initDB();
 
+        MobileLedgerDB.applyRevisions(getResources(), getPackageName());
+
         account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0);
 
     }
@@ -136,7 +152,7 @@ public class AccountSummary extends AppCompatActivity {
     }
 
     private void update_accounts() {
-        mRefresh.setVisible(false);
+        if (mRefresh != null) mRefresh.setVisible(false);
         Resources rm = getResources();
 
         ProgressBar pb = findViewById(R.id.progressBar);
@@ -158,16 +174,108 @@ public class AccountSummary extends AppCompatActivity {
             protected void onPostExecute(Void result) {
                 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 (mRefresh != null) mRefresh.setVisible(true);
+                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()));
+    }
+
+    Pattern higher_account = Pattern.compile("^[^:]+:");
+
+    private String strip_higher_accounts(String acc_name, int[] count) {
+        count[0] = 0;
+        while (true) {
+            Matcher m = higher_account.matcher(acc_name);
+            if (m.find()) {
+                count[0]++;
+                acc_name = m.replaceFirst("");
+            }
+            else break;
+        }
+
+        return acc_name;
+    }
+
+    public void hideAccountClicked(MenuItem item) {
+        TextView textView = (TextView) clickedAccountRow.getChildAt(0);
+        Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show();
+    }
+
+    @SuppressLint("DefaultLocale")
+    private void update_account_table() {
+        LinearLayout root = findViewById(R.id.account_root);
+        root.removeAllViewsInLayout();
+
+        View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() {
+            @Override
+            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
+                clickedAccountRow = (LinearLayout) v;
+                getMenuInflater().inflate(R.menu.account_summary_account_menu, menu);
+            }
+        };
+
+        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);
 
+                LinearLayout r = new LinearLayout(this);
+                r.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
+                r.setGravity(Gravity.CENTER_VERTICAL);
+                r.setPadding(getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin), dp2px(3), getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin), dp2px(4));
+                if (even)
+                    r.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
+                even = !even;
+                r.setContextClickable(true);
+                r.setOnCreateContextMenuListener(ccml);
+
+
+                TextView acc_tv = new TextView(this, null, R.style.account_summary_account_name);
+                acc_tv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 5f));
+                acc_tv.setGravity(Gravity.CENTER_VERTICAL);
+                int[] indent_level = new int[]{0};
+                String short_acc_name = strip_higher_accounts(acc_name, indent_level);
+                acc_tv.setPadding(indent_level[0] * getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2, 0, 0, 0);
+                acc_tv.setText(short_acc_name);
+                r.addView(acc_tv);
+
+                TextView amt_tv = new TextView(this, null, R.style.account_summary_amounts);
+                amt_tv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f));
+                amt_tv.setTextAlignment(EditText.TEXT_ALIGNMENT_VIEW_END);
+                amt_tv.setGravity(Gravity.CENTER_VERTICAL);
+//                amt_tv.setGravity(Gravity.CENTER);
+                amt_tv.setMinWidth(dp2px(60f));
+                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);
+
+                root.addView(r);
+            }
+        }
     }
 }