]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
better account starr-ing control
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummaryViewModel.java
index da1163138eaf19b9579b1c2e027000a3fab625d9..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,98 +164,24 @@ 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);
+            }
+        }
     }
-
-//    @NonNull
-//    @Override
-//    public View getView(int position, @Nullable View row, @NonNull ViewGroup parent) {
-//        LedgerAccount acc = getItem(position);
-//        LedgerRowHolder holder;
-//        if (row == null) {
-//            holder = new LedgerRowHolder();
-//            LayoutInflater vi = getSystemService(this.getContext(), LayoutInflater.class);
-//            MenuInflater mi = getSystemService(this.getContext(), MenuInflater.class);
-//
-//            if (vi == null)
-//                throw new IllegalStateException("Unable to instantiate the inflater " + "service");
-//            row = vi.inflate(R.layout.account_summary_row, parent, false);
-//            holder.tvAccountName = row.findViewById(R.id.account_row_acc_name);
-//            holder.tvAccountAmounts = row.findViewById(R.id.account_row_acc_amounts);
-//            row.setTag(R.id.VH, holder);
-//
-//            row.setPadding(context.getResources()
-//                            .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2, dp2px
-//                            (context, 3),
-//                    context.getResources()
-//                            .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
-//                    dp2px(context, 4));
-//            View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() {
-//                @Override
-//                public void onCreateContextMenu(ContextMenu menu, View v,
-//                                                ContextMenu.ContextMenuInfo menuInfo) {
-//                    final ListView parent = (ListView) v.getParent();
-//                    int pos = parent.getPositionForView(v);
-//                    parent.setItemChecked(pos, true);
-//                    Log.d("list", String.format("checking pos %d", pos));
-//                }
-//            };
-//            row.setOnCreateContextMenuListener(ccml);
-//
-//        }
-//        else holder = (LedgerRowHolder) row.getTag(R.id.VH);
-//
-//        holder.tvAccountName.setText(acc.getShortName());
-//        holder.tvAccountName.setPadding(acc.getLevel() * context.getResources()
-//                .getDimensionPixelSize(R.dimen.activity_horizontal_margin), 0, 0, 0);
-//        holder.tvAccountAmounts.setText(acc.getAmountsString());
-//
-//        if (acc.isHidden()) {
-//            holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
-//            holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
-//        }
-//        else {
-//            holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
-//            holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
-//        }
-//
-//        int real_pos = ((ListView)parent).getPositionForView(row);
-//        Log.d("model", String.format("%s: real_pos=%d, position=%d", acc.getName(), real_pos,
-//                position));
-//        if (real_pos == -1) real_pos = position+1;
-//        else real_pos = real_pos + 1;
-//
-//        if ( real_pos % 2 == 0) {
-//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-//                row.setBackgroundColor(context.getResources()
-//                        .getColor(R.color.table_row_even_bg, context.getTheme()));
-//            }
-//            else {
-//                row.setBackgroundColor(
-//                        context.getResources().getColor(R.color.table_row_even_bg));
-//            }
-//        }
-//        else {
-//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-//                row.setBackgroundColor(context.getResources()
-//                        .getColor(R.color.drawer_background, context.getTheme()));
-//            }
-//            else {
-//                row.setBackgroundColor(context.getResources().getColor(R.color.drawer_background));
-//            }
-//        }
-//
-//        row.setTag(R.id.POS, position);
-//
-//        return row;
-//    }
-
     class LedgerRowHolder extends RecyclerView.ViewHolder {
         CheckBox selectionCb;
         TextView tvAccountName, tvAccountAmounts;
         LinearLayout row;
-
         public LedgerRowHolder(@NonNull View itemView) {
             super(itemView);
             this.row = (LinearLayout) itemView;