2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.app.Application;
21 import android.arch.lifecycle.AndroidViewModel;
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.database.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.graphics.Typeface;
27 import android.os.Build;
28 import android.preference.PreferenceManager;
29 import android.support.annotation.NonNull;
30 import android.support.v7.widget.RecyclerView;
31 import android.util.Log;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.CheckBox;
36 import android.widget.LinearLayout;
37 import android.widget.TextView;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.utils.MLDB;
42 import java.util.ArrayList;
43 import java.util.List;
45 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
47 class AccountSummaryViewModel extends AndroidViewModel {
48 private List<LedgerAccount> accounts;
50 public AccountSummaryViewModel(@NonNull Application application) {
54 List<LedgerAccount> getAccounts(Context context) {
55 if (accounts == null) {
56 accounts = new ArrayList<>();
57 reloadAccounts(context);
63 void reloadAccounts(Context context) {
65 boolean showingOnlyStarred =
66 PreferenceManager.getDefaultSharedPreferences(getApplication())
67 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
68 String sql = "SELECT name, hidden FROM accounts";
69 if (showingOnlyStarred) sql += " WHERE hidden = 0";
70 sql += " ORDER BY name";
72 try (SQLiteDatabase db = MLDB.getReadableDatabase(context)) {
73 try (Cursor cursor = db
76 while (cursor.moveToNext()) {
77 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
78 acc.setHidden(cursor.getInt(1) == 1);
79 try (Cursor c2 = db.rawQuery(
80 "SELECT value, currency FROM account_values " + "WHERE account = ?",
81 new String[]{acc.getName()}))
83 while (c2.moveToNext()) {
84 acc.addAmount(c2.getFloat(0), c2.getString(1));
92 void commitSelections(Context context) {
93 try(SQLiteDatabase db = MLDB.getWritableDatabase(context)) {
94 db.beginTransaction();
96 for (LedgerAccount acc : accounts) {
97 Log.d("db", String.format("Setting %s to %s", acc.getName(),
98 acc.isHidden() ? "hidden" : "starred"));
99 db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
100 new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
102 db.setTransactionSuccessful();
103 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
105 finally { db.endTransaction(); }
110 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
111 private List<LedgerAccount> accounts;
112 private boolean selectionActive;
114 AccountSummaryAdapter(List<LedgerAccount> accounts) {
115 this.accounts = accounts;
116 this.selectionActive = false;
119 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
120 LedgerAccount acc = accounts.get(position);
121 Context ctx = holder.row.getContext();
122 Resources rm = ctx.getResources();
124 holder.tvAccountName.setText(acc.getShortName());
125 holder.tvAccountName.setPadding(
126 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
129 holder.tvAccountAmounts.setText(acc.getAmountsString());
131 if (acc.isHidden()) {
132 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
133 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
136 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
137 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
140 if (position % 2 == 0) {
141 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
142 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
143 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
146 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
147 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
148 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
151 holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
152 holder.selectionCb.setChecked(!acc.isHiddenToBe());
154 holder.row.setTag(R.id.POS, position);
159 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
160 View row = LayoutInflater.from(parent.getContext())
161 .inflate(R.layout.account_summary_row, parent, false);
162 return new LedgerRowHolder(row);
166 public int getItemCount() {
167 return accounts.size();
169 public void startSelection() {
170 for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
171 this.selectionActive = true;
172 notifyDataSetChanged();
175 public void stopSelection() {
176 this.selectionActive = false;
177 notifyDataSetChanged();
180 public boolean isSelectionActive() {
181 return selectionActive;
184 public void selectItem(int position) {
185 LedgerAccount acc = accounts.get(position);
186 acc.toggleHiddenToBe();
187 toggleChildrenOf(acc, acc.isHiddenToBe());
188 notifyDataSetChanged();
190 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
191 for (LedgerAccount acc : accounts) {
192 String acc_parent = acc.getParentName();
193 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
194 acc.setHiddenToBe(hiddenToBe);
195 toggleChildrenOf(acc, hiddenToBe);
199 class LedgerRowHolder extends RecyclerView.ViewHolder {
200 CheckBox selectionCb;
201 TextView tvAccountName, tvAccountAmounts;
203 public LedgerRowHolder(@NonNull View itemView) {
205 this.row = (LinearLayout) itemView;
206 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
207 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
208 this.selectionCb = itemView.findViewById(R.id.account_row_check);