]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
move DB stuff into a static class
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummaryViewModel.java
index c1f1625812a91baab8aa8bdbe932e7e7970f7bca..f9b5d74ff30c69f2a534e92d58672b55ea3c2eaf 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * 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/>.
+ */
+
 package net.ktnx.mobileledger;
 
 import android.app.Application;
@@ -11,6 +28,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;
@@ -18,37 +36,40 @@ import android.widget.CheckBox;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.utils.MLDB;
+
 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;
 
     public AccountSummaryViewModel(@NonNull Application application) {
         super(application);
-        dbh = new MobileLedgerDatabase(application);
     }
 
-    List<LedgerAccount> getAccounts() {
+    List<LedgerAccount> getAccounts(Context context) {
         if (accounts == null) {
             accounts = new ArrayList<>();
-            reloadAccounts();
+            reloadAccounts(context);
         }
 
         return accounts;
     }
 
-    void reloadAccounts() {
+    void reloadAccounts(Context context) {
         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()) {
+        try (SQLiteDatabase db = MLDB.getReadableDatabase(context)) {
             try (Cursor cursor = db
                     .rawQuery(sql,null))
             {
@@ -68,6 +89,22 @@ class AccountSummaryViewModel extends AndroidViewModel {
             }
         }
     }
+    void commitSelections(Context context) {
+        try(SQLiteDatabase db = MLDB.getWritableDatabase(context)) {
+            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 +149,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 +166,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 +182,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;