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=fbdc89f2c659205a8ddaa39b5c0b54d0f202564d;hp=0973590cfa7641a7dcd6b53c322b57dec18b21c5;hb=9ea6b1d4b8092349d1a41049d89c698cb472dc85;hpb=89e0fa95e3fca65277711a762f0643c974481e0a diff --git a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java index 0973590c..fbdc89f2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java +++ b/app/src/main/java/net/ktnx/mobileledger/AccountSummary.java @@ -2,9 +2,11 @@ package net.ktnx.mobileledger; import android.annotation.SuppressLint; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.res.Resources; import android.database.Cursor; +import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; @@ -26,7 +28,6 @@ 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; @@ -41,7 +42,9 @@ public class AccountSummary extends AppCompatActivity { private static long account_list_last_updated; private static boolean account_list_needs_update = true; - private LinearLayout clickedAccountRow; + MenuItem mShowHiddenAccounts; + SharedPreferences.OnSharedPreferenceChangeListener sBindPreferenceSummaryToValueListener; + private AccountRowLayout clickedAccountRow; public static void preferences_changed() { account_list_needs_update = true; @@ -79,14 +82,30 @@ public class AccountSummary extends AppCompatActivity { protected void onStart() { super.onStart(); LinearLayout grp = drawer.findViewById(R.id.nav_actions); - for (int i = 0; i < grp.getChildCount(); i++) - grp.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.drawer_background, getTheme())); - drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme())); + for (int i = 0; i < grp.getChildCount(); i++) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + grp.getChildAt(i).setBackgroundColor( + getResources().getColor(R.color.drawer_background, getTheme())); + } + else { + grp.getChildAt(i) + .setBackgroundColor(getResources().getColor(R.color.drawer_background)); + } + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + drawer.findViewById(R.id.nav_account_summary).setBackgroundColor( + getResources().getColor(R.color.table_row_even_bg, getTheme())); + } + else { + drawer.findViewById(R.id.nav_account_summary) + .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg)); + } } public void fab_new_transaction_clicked(View view) { Intent intent = new Intent(this, NewTransactionActivity.class); startActivity(intent); + overridePendingTransition(R.anim.slide_in_right, R.anim.dummy); } public void nav_exit_clicked(View view) { @@ -115,6 +134,24 @@ public class AccountSummary extends AppCompatActivity { getMenuInflater().inflate(R.menu.account_summary, menu); mRefresh = menu.findItem(R.id.menu_acc_summary_refresh); if (mRefresh == null) throw new AssertionError(); + + mShowHiddenAccounts = menu.findItem(R.id.menu_acc_summary_show_hidden); + if (mShowHiddenAccounts == null) throw new AssertionError(); + + sBindPreferenceSummaryToValueListener = + new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public + void onSharedPreferenceChanged(SharedPreferences preference, String value) { + mShowHiddenAccounts + .setChecked(preference.getBoolean("show_hidden_accounts", false)); + } + }; + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener); + + mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false)); + return true; } @@ -123,7 +160,7 @@ public class AccountSummary extends AppCompatActivity { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); +// int id = item.getItemId(); //noinspection SimplifiableIfStatement //if (id == R.id.action_settings) { @@ -137,16 +174,28 @@ public class AccountSummary extends AppCompatActivity { update_accounts(true); } + public + void onShowHiddenAccountsClicked(MenuItem mi) { + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); + boolean flag = pref.getBoolean("show_hidden_accounts", false); + + SharedPreferences.Editor editor = pref.edit(); + editor.putBoolean("show_hidden_accounts", !flag); + Log.d("pref", "Setting show_hidden_accounts to " + (flag ? "false" : "true")); + editor.apply(); + + update_account_table(); + } + 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.setDb_filename( + this.getApplicationInfo().dataDir + "/" + MobileLedgerDB.DATABASE_NAME); } - MobileLedgerDB.initDB(); - - MobileLedgerDB.applyRevisions(getResources(), getPackageName()); + MobileLedgerDB.initDB(getResources(), getPackageName()); account_list_last_updated = MobileLedgerDB.get_option_value("last_refresh", (long) 0); @@ -222,8 +271,9 @@ public class AccountSummary extends AppCompatActivity { } public void hideAccountClicked(MenuItem item) { - TextView textView = (TextView) clickedAccountRow.getChildAt(0); - Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show(); + db.execSQL("update accounts set hidden=1 where name=?", + new Object[]{clickedAccountRow.getAccountName()}); + update_account_table(); } @SuppressLint("DefaultLocale") @@ -234,24 +284,58 @@ public class AccountSummary extends AppCompatActivity { View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { - clickedAccountRow = (LinearLayout) v; + clickedAccountRow = (AccountRowLayout) v; getMenuInflater().inflate(R.menu.account_summary_account_menu, menu); } }; - try (Cursor cursor = db.rawQuery("SELECT name FROM accounts ORDER BY name;", null)) { + int rowHeight = + (int) (getTheme().obtainStyledAttributes(new int[]{android.R.attr.actionBarSize}) + .getDimensionPixelSize(0, dp2px(56)) * 0.75); + + boolean showingHiddenAccounts = PreferenceManager.getDefaultSharedPreferences(this) + .getBoolean("show_hidden_accounts", false); + Log.d("pref", "show_hidden_accounts is " + (showingHiddenAccounts ? "true" : "false")); + + try (Cursor cursor = db + .rawQuery("SELECT name, hidden FROM accounts ORDER BY name;", null)) + { boolean even = false; + String skippingAccountName = null; while (cursor.moveToNext()) { String acc_name = cursor.getString(0); + if (skippingAccountName != null) { + if (acc_name.startsWith(skippingAccountName + ":")) continue; + + skippingAccountName = null; + } + + boolean is_hidden = cursor.getInt(1) == 1; - LinearLayout r = new LinearLayout(this); + if (!showingHiddenAccounts && is_hidden) { + skippingAccountName = acc_name; + continue; + } + + LinearLayout r = new AccountRowLayout(this, acc_name); 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())); + r.setMinimumHeight(rowHeight); + + if (even) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + r.setBackgroundColor( + getResources().getColor(R.color.table_row_even_bg, getTheme())); + } + else { + r.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg)); + } + } even = !even; - r.setContextClickable(true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + r.setContextClickable(true); + } r.setOnCreateContextMenuListener(ccml); @@ -262,6 +346,7 @@ public class AccountSummary extends AppCompatActivity { 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); + if (is_hidden) acc_tv.setTypeface(null, Typeface.ITALIC); r.addView(acc_tv); TextView amt_tv = new TextView(this, null, R.style.account_summary_amounts); @@ -276,10 +361,11 @@ public class AccountSummary extends AppCompatActivity { 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_text.append(String.format("%s %,1.2f", curr, amt)); } } amt_tv.setText(amt_text.toString()); + if (is_hidden) amt_tv.setTypeface(null, Typeface.ITALIC); r.addView(amt_tv);