]> git.ktnx.net Git - mobile-ledger.git/commitdiff
better account starr-ing control
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 14 Dec 2018 05:58:00 +0000 (05:58 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 14 Dec 2018 05:58:00 +0000 (05:58 +0000)
13 files changed:
app/src/main/java/net/ktnx/mobileledger/AccountSummary.java
app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
app/src/main/java/net/ktnx/mobileledger/LedgerAccount.java
app/src/main/java/net/ktnx/mobileledger/SettingsActivity.java
app/src/main/res/drawable/checkbox_star_black.xml [new file with mode: 0644]
app/src/main/res/drawable/checkbox_star_white.xml [new file with mode: 0644]
app/src/main/res/drawable/ic_star_black_24dp.xml [new file with mode: 0644]
app/src/main/res/drawable/ic_star_border_black_24dp.xml [new file with mode: 0644]
app/src/main/res/drawable/ic_star_border_white_24dp.xml [new file with mode: 0644]
app/src/main/res/layout/account_summary_row.xml
app/src/main/res/menu/account_summary.xml
app/src/main/res/values/strings.xml
app/src/main/res/xml/pref_interface.xml

index 77a60074164a85fdad88c2e49d92de395b8a585b..e34aa6b71d054c45bd838e598cd740f41eaa41ee 100644 (file)
@@ -28,6 +28,8 @@ import java.lang.ref.WeakReference;
 import java.util.Date;
 import java.util.List;
 
+import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
+
 public class AccountSummary extends AppCompatActivity {
     DrawerLayout drawer;
 
@@ -94,7 +96,12 @@ public class AccountSummary extends AppCompatActivity {
                 modelAdapter.startSelection();
                 if (optMenu != null) {
                     optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(true);
-                    optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(true);
+                    optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(true);
+                    optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(false);
+                }
+                {
+                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_transaction);
+                    if (fab != null) fab.hide();
                 }
             }
         }));
@@ -175,11 +182,11 @@ public class AccountSummary extends AppCompatActivity {
         if (mShowHiddenAccounts == null) throw new AssertionError();
 
         sBindPreferenceSummaryToValueListener = (preference, value) -> mShowHiddenAccounts
-                .setChecked(preference.getBoolean("show_hidden_accounts", false));
+                .setChecked(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
         pref.registerOnSharedPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
 
-        mShowHiddenAccounts.setChecked(pref.getBoolean("show_hidden_accounts", false));
+        mShowHiddenAccounts.setChecked(pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
 
         return true;
     }
@@ -206,11 +213,11 @@ public class AccountSummary extends AppCompatActivity {
     public
     void onShowOnlyStarredClicked(MenuItem mi) {
         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
-        boolean flag = pref.getBoolean("show_hidden_accounts", false);
+        boolean flag = pref.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_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.putBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, !flag);
+        Log.d("pref", "Setting show only starred accounts pref to " + (flag ? "false" : "true"));
         editor.apply();
 
         update_account_table();
@@ -252,11 +259,23 @@ public class AccountSummary extends AppCompatActivity {
         model.reloadAccounts();
         modelAdapter.notifyDataSetChanged();
     }
-    public void onCancelAccSelection(MenuItem item) {
+    void stopSelection() {
         modelAdapter.stopSelection();
         if (optMenu != null) {
             optMenu.findItem(R.id.menu_acc_summary_cancel_selection).setVisible(false);
-            optMenu.findItem(R.id.menu_acc_summary_hide_selected).setVisible(false);
+            optMenu.findItem(R.id.menu_acc_summary_confirm_selection).setVisible(false);
+            optMenu.findItem(R.id.menu_acc_summary_only_starred).setVisible(true);
+        }
+        {
+            FloatingActionButton fab = findViewById(R.id.btn_add_transaction);
+            if (fab != null) fab.show();
         }
     }
+    public void onCancelAccSelection(MenuItem item) {
+        stopSelection();
+    }
+    public void onConfirmAccSelection(MenuItem item) {
+        model.commitSelections();
+        stopSelection();
+    }
 }
index c1f1625812a91baab8aa8bdbe932e7e7970f7bca..b283d7558f6f65a485e7948f054951a0c560a10d 100644 (file)
@@ -11,6 +11,7 @@ import android.os.Build;
 import android.preference.PreferenceManager;
 import android.support.annotation.NonNull;
 import android.support.v7.widget.RecyclerView;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -21,6 +22,8 @@ import android.widget.TextView;
 import java.util.ArrayList;
 import java.util.List;
 
+import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
+
 class AccountSummaryViewModel extends AndroidViewModel {
     private MobileLedgerDatabase dbh;
     private List<LedgerAccount> accounts;
@@ -41,11 +44,11 @@ class AccountSummaryViewModel extends AndroidViewModel {
 
     void reloadAccounts() {
         accounts.clear();
-        boolean showingHiddenAccounts =
+        boolean showingOnlyStarred =
                 PreferenceManager.getDefaultSharedPreferences(getApplication())
-                        .getBoolean("show_hidden_accounts", false);
+                        .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
         String sql = "SELECT name, hidden FROM accounts";
-        if (!showingHiddenAccounts) sql += " WHERE hidden = 0";
+        if (showingOnlyStarred) sql += " WHERE hidden = 0";
         sql += " ORDER BY name";
 
         try (SQLiteDatabase db = dbh.getReadableDatabase()) {
@@ -68,6 +71,22 @@ class AccountSummaryViewModel extends AndroidViewModel {
             }
         }
     }
+    void commitSelections() {
+        try(SQLiteDatabase db = dbh.getWritableDatabase()) {
+            db.beginTransaction();
+            try {
+                for (LedgerAccount acc : accounts) {
+                    Log.d("db", String.format("Setting %s to %s", acc.getName(),
+                            acc.isHidden() ? "hidden" : "starred"));
+                    db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
+                            new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
+                }
+                db.setTransactionSuccessful();
+                for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
+            }
+            finally { db.endTransaction(); }
+        }
+    }
 }
 
 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
@@ -112,7 +131,7 @@ class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.L
         }
 
         holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
-        holder.selectionCb.setChecked(acc.isSelected());
+        holder.selectionCb.setChecked(!acc.isHiddenToBe());
 
         holder.row.setTag(R.id.POS, position);
     }
@@ -129,9 +148,8 @@ class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.L
     public int getItemCount() {
         return accounts.size();
     }
-
     public void startSelection() {
-        for( LedgerAccount acc : accounts ) acc.setSelected(false);
+        for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
         this.selectionActive = true;
         notifyDataSetChanged();
     }
@@ -146,8 +164,19 @@ class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.L
     }
 
     public void selectItem(int position) {
-        accounts.get(position).toggleSelected();
-        notifyItemChanged(position);
+        LedgerAccount acc = accounts.get(position);
+        acc.toggleHiddenToBe();
+        toggleChildrenOf(acc, acc.isHiddenToBe());
+        notifyDataSetChanged();
+    }
+    void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
+        for (LedgerAccount acc : accounts) {
+            String acc_parent = acc.getParentName();
+            if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
+                acc.setHiddenToBe(hiddenToBe);
+                toggleChildrenOf(acc, hiddenToBe);
+            }
+        }
     }
     class LedgerRowHolder extends RecyclerView.ViewHolder {
         CheckBox selectionCb;
index 27ab1d82f4d9303f3a205ae48835eff7ac4a1d8c..518dd2694b1a99ed1ff6ad05dacc8c213ac12f15 100644 (file)
@@ -13,14 +13,13 @@ class LedgerAccount {
     private int level;
     private String parentName;
     private boolean hidden;
+    private boolean hiddenToBe;
     private List<LedgerAmount> amounts;
-    private boolean selected;
     static Pattern higher_account = Pattern.compile("^[^:]+:");
 
     LedgerAccount(String name) {
         this.setName(name);
         hidden = false;
-        selected = false;
     }
 
     public boolean isHidden() {
@@ -98,16 +97,17 @@ class LedgerAccount {
     public String getParentName() {
         return parentName;
     }
-
-    public boolean isSelected() {
-        return selected;
+    public void togglehidden() {
+        hidden = !hidden;
     }
 
-    public void setSelected(boolean selected) {
-        this.selected = selected;
+    public boolean isHiddenToBe() {
+        return hiddenToBe;
     }
-
-    public void toggleSelected() {
-        selected = !selected;
+    public void setHiddenToBe(boolean hiddenToBe) {
+        this.hiddenToBe = hiddenToBe;
+    }
+    public void toggleHiddenToBe() {
+        setHiddenToBe(!hiddenToBe);
     }
 }
index 0f8246a2075008390e92e5c41ab79c1b4088cb14..ca70fe0b836ca3e11eb0c84c4b3ebf6e8665089a 100644 (file)
@@ -34,6 +34,7 @@ import java.util.List;
  * API Guide</a> for more information on developing a Settings UI.
  */
 public class SettingsActivity extends AppCompatPreferenceActivity {
+    public static String PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS = "pref_show_only_starred_accounts";
 
     /**
      * A preference value change listener that updates the preference's summary
@@ -224,7 +225,6 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
             // to their values. When their values change, their summaries are
             // updated to reflect the new value, per the Android Design
             // guidelines.
-//            bindPreferenceSummaryToValue(findPreference("show_hidden_accounts"));
 
         }
 
diff --git a/app/src/main/res/drawable/checkbox_star_black.xml b/app/src/main/res/drawable/checkbox_star_black.xml
new file mode 100644 (file)
index 0000000..92eace4
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright © 2018 Damyan Ivanov.
+  ~ This file is part of Mobile-Ledger.
+  ~ Mobile-Ledger is free software: you can distribute it and/or modify it
+  ~ under the term of the GNU General Public License as published by
+  ~ the Free Software Foundation, either version 3 of the License, or
+  ~ (at your opinion), any later version.
+  ~
+  ~ Mobile-Ledger is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  ~ GNU General Public License terms for details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:drawable="@drawable/ic_star_border_black_24dp"
+        android:state_checked="false"/>
+    <item android:drawable="@drawable/ic_star_black_24dp"
+        android:state_checked="true"/>
+    <item android:drawable="@drawable/ic_star_border_black_24dp" />
+</selector>
\ No newline at end of file
diff --git a/app/src/main/res/drawable/checkbox_star_white.xml b/app/src/main/res/drawable/checkbox_star_white.xml
new file mode 100644 (file)
index 0000000..6e2dcc1
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright © 2018 Damyan Ivanov.
+  ~ This file is part of Mobile-Ledger.
+  ~ Mobile-Ledger is free software: you can distribute it and/or modify it
+  ~ under the term of the GNU General Public License as published by
+  ~ the Free Software Foundation, either version 3 of the License, or
+  ~ (at your opinion), any later version.
+  ~
+  ~ Mobile-Ledger is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  ~ GNU General Public License terms for details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:drawable="@drawable/ic_star_border_white_24dp"
+        android:state_checked="false"/>
+    <item android:drawable="@drawable/ic_star_white_24dp"
+        android:state_checked="true"/>
+    <item android:drawable="@drawable/ic_star_border_white_24dp" />
+</selector>
\ No newline at end of file
diff --git a/app/src/main/res/drawable/ic_star_black_24dp.xml b/app/src/main/res/drawable/ic_star_black_24dp.xml
new file mode 100644 (file)
index 0000000..742bacd
--- /dev/null
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#313131"
+    android:viewportHeight="24.0" android:viewportWidth="24.0"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
+</vector>
diff --git a/app/src/main/res/drawable/ic_star_border_black_24dp.xml b/app/src/main/res/drawable/ic_star_border_black_24dp.xml
new file mode 100644 (file)
index 0000000..c5822f9
--- /dev/null
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#313131"
+    android:viewportHeight="24.0" android:viewportWidth="24.0"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="#FF000000" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
+</vector>
diff --git a/app/src/main/res/drawable/ic_star_border_white_24dp.xml b/app/src/main/res/drawable/ic_star_border_white_24dp.xml
new file mode 100644 (file)
index 0000000..818483a
--- /dev/null
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#EEEEEE"
+    android:viewportHeight="24.0" android:viewportWidth="24.0"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="#FF000000" android:pathData="M22,9.24l-7.19,-0.62L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21 12,17.27 18.18,21l-1.63,-7.03L22,9.24zM12,15.4l-3.76,2.27 1,-4.28 -3.32,-2.88 4.38,-0.38L12,6.1l1.71,4.04 4.38,0.38 -3.32,2.88 1,4.28L12,15.4z"/>
+</vector>
index 9617db65f4450fd52000bfbe8ca0d0d6971d1a13..478b6e6dc3fd7c8a2bfb7330d0fd9e6cd10a48d0 100644 (file)
@@ -16,7 +16,8 @@
     <CheckBox
         android:id="@+id/account_row_check"
         android:layout_width="wrap_content"
-        android:layout_height="match_parent" />
+        android:layout_height="match_parent"
+        android:button="@drawable/checkbox_star_black" />
 
     <TextView
         android:id="@+id/account_row_acc_name"
index 65abb6dd6fc4c357af7ef99a357983f4a471523c..00ed9c0dcb8c7747163cb2e614670d7053e8300c 100644 (file)
         android:visible="false"
         android:onClick="onCancelAccSelection"
         android:icon="@drawable/ic_cancel_white_24dp" />
+    <item android:id="@+id/menu_acc_summary_confirm_selection"
+        android:title="@string/menu_acc_summary_confirm_selection_title"
+        app:showAsAction="always"
+        android:visible="false"
+        android:onClick="onConfirmAccSelection"
+        android:icon="@drawable/ic_check_white_24dp" />
 </menu>
\ No newline at end of file
index a57104ea32997b557b69bdd894d0acaa0239e05a..4b9a8ef35ad3bead7728253e815eebfd9ccc1c7d 100644 (file)
@@ -81,4 +81,5 @@
     <string name="pref_show_only_starred_on_summary">Only starred accounts are shown</string>
     <string name="menu_acc_summary_hide_selected_title">Hide selected accounts</string>
     <string name="menu_acc_summary_cancel_selection_title">Cancel selection</string>
+    <string name="menu_acc_summary_confirm_selection_title">Confirm selectin</string>
 </resources>
index bc832f8820d1217946ac58843656dde71cb7a0d9..62ed9c69db88ab5195633b635fa154e535efa649 100644 (file)
@@ -2,9 +2,9 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 
     <SwitchPreference
-        android:id="@+id/pref_show_hidden_accounts"
+        android:id="@+id/pref_show_only_starred_accounts"
         android:defaultValue="false"
-        android:key="show_hidden_accounts"
+        android:key="pref_show_only_starred_accounts"
         android:summaryOff="@string/pref_show_only_starred_off_summary"
         android:summaryOn="@string/pref_show_only_starred_on_summary"
         android:title="@string/menu_acc_summary_show_only_starred_title" />