1 package net.ktnx.mobileledger;
3 import android.app.Application;
4 import android.arch.lifecycle.AndroidViewModel;
5 import android.content.Context;
6 import android.content.res.Resources;
7 import android.database.Cursor;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.graphics.Typeface;
10 import android.os.Build;
11 import android.preference.PreferenceManager;
12 import android.support.annotation.NonNull;
13 import android.support.v7.widget.RecyclerView;
14 import android.util.Log;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.CheckBox;
19 import android.widget.LinearLayout;
20 import android.widget.TextView;
22 import java.util.ArrayList;
23 import java.util.List;
25 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
27 class AccountSummaryViewModel extends AndroidViewModel {
28 private MobileLedgerDatabase dbh;
29 private List<LedgerAccount> accounts;
31 public AccountSummaryViewModel(@NonNull Application application) {
33 dbh = new MobileLedgerDatabase(application);
36 List<LedgerAccount> getAccounts() {
37 if (accounts == null) {
38 accounts = new ArrayList<>();
45 void reloadAccounts() {
47 boolean showingOnlyStarred =
48 PreferenceManager.getDefaultSharedPreferences(getApplication())
49 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
50 String sql = "SELECT name, hidden FROM accounts";
51 if (showingOnlyStarred) sql += " WHERE hidden = 0";
52 sql += " ORDER BY name";
54 try (SQLiteDatabase db = dbh.getReadableDatabase()) {
55 try (Cursor cursor = db
58 while (cursor.moveToNext()) {
59 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
60 acc.setHidden(cursor.getInt(1) == 1);
61 try (Cursor c2 = db.rawQuery(
62 "SELECT value, currency FROM account_values " + "WHERE account = ?",
63 new String[]{acc.getName()}))
65 while (c2.moveToNext()) {
66 acc.addAmount(c2.getFloat(0), c2.getString(1));
74 void commitSelections() {
75 try(SQLiteDatabase db = dbh.getWritableDatabase()) {
76 db.beginTransaction();
78 for (LedgerAccount acc : accounts) {
79 Log.d("db", String.format("Setting %s to %s", acc.getName(),
80 acc.isHidden() ? "hidden" : "starred"));
81 db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
82 new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
84 db.setTransactionSuccessful();
85 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
87 finally { db.endTransaction(); }
92 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
93 private List<LedgerAccount> accounts;
94 private boolean selectionActive;
96 AccountSummaryAdapter(List<LedgerAccount> accounts) {
97 this.accounts = accounts;
98 this.selectionActive = false;
101 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
102 LedgerAccount acc = accounts.get(position);
103 Context ctx = holder.row.getContext();
104 Resources rm = ctx.getResources();
106 holder.tvAccountName.setText(acc.getShortName());
107 holder.tvAccountName.setPadding(
108 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
111 holder.tvAccountAmounts.setText(acc.getAmountsString());
113 if (acc.isHidden()) {
114 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
115 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
118 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
119 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
122 if (position % 2 == 0) {
123 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
124 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
125 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
128 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
129 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
130 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
133 holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
134 holder.selectionCb.setChecked(!acc.isHiddenToBe());
136 holder.row.setTag(R.id.POS, position);
141 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
142 View row = LayoutInflater.from(parent.getContext())
143 .inflate(R.layout.account_summary_row, parent, false);
144 return new LedgerRowHolder(row);
148 public int getItemCount() {
149 return accounts.size();
151 public void startSelection() {
152 for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
153 this.selectionActive = true;
154 notifyDataSetChanged();
157 public void stopSelection() {
158 this.selectionActive = false;
159 notifyDataSetChanged();
162 public boolean isSelectionActive() {
163 return selectionActive;
166 public void selectItem(int position) {
167 LedgerAccount acc = accounts.get(position);
168 acc.toggleHiddenToBe();
169 toggleChildrenOf(acc, acc.isHiddenToBe());
170 notifyDataSetChanged();
172 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
173 for (LedgerAccount acc : accounts) {
174 String acc_parent = acc.getParentName();
175 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
176 acc.setHiddenToBe(hiddenToBe);
177 toggleChildrenOf(acc, hiddenToBe);
181 class LedgerRowHolder extends RecyclerView.ViewHolder {
182 CheckBox selectionCb;
183 TextView tvAccountName, tvAccountAmounts;
185 public LedgerRowHolder(@NonNull View itemView) {
187 this.row = (LinearLayout) itemView;
188 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
189 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
190 this.selectionCb = itemView.findViewById(R.id.account_row_check);