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.ui.account_summary;
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.R;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.utils.MLDB;
43 import java.util.ArrayList;
44 import java.util.List;
46 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
48 class AccountSummaryViewModel extends AndroidViewModel {
49 private List<LedgerAccount> accounts;
51 public AccountSummaryViewModel(@NonNull Application application) {
55 List<LedgerAccount> getAccounts(Context context) {
56 if (accounts == null) {
57 accounts = new ArrayList<>();
58 reloadAccounts(context);
64 void reloadAccounts(Context context) {
66 boolean showingOnlyStarred =
67 PreferenceManager.getDefaultSharedPreferences(getApplication())
68 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
69 String sql = "SELECT name, hidden FROM accounts";
70 if (showingOnlyStarred) sql += " WHERE hidden = 0";
71 sql += " ORDER BY name";
73 try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
74 try (Cursor cursor = db
77 while (cursor.moveToNext()) {
78 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
79 acc.setHidden(cursor.getInt(1) == 1);
80 try (Cursor c2 = db.rawQuery(
81 "SELECT value, currency FROM account_values " + "WHERE account = ?",
82 new String[]{acc.getName()}))
84 while (c2.moveToNext()) {
85 acc.addAmount(c2.getFloat(0), c2.getString(1));
93 void commitSelections(Context context) {
94 try(SQLiteDatabase db = MLDB.getWritableDatabase(context)) {
95 db.beginTransaction();
97 for (LedgerAccount acc : accounts) {
98 Log.d("db", String.format("Setting %s to %s", acc.getName(),
99 acc.isHidden() ? "hidden" : "starred"));
100 db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
101 new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
103 db.setTransactionSuccessful();
104 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
106 finally { db.endTransaction(); }
111 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter
113 private List<LedgerAccount> accounts;
114 private boolean selectionActive;
116 AccountSummaryAdapter(List<LedgerAccount> accounts) {
117 this.accounts = accounts;
118 this.selectionActive = false;
121 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
122 LedgerAccount acc = accounts.get(position);
123 Context ctx = holder.row.getContext();
124 Resources rm = ctx.getResources();
126 holder.tvAccountName.setText(acc.getShortName());
127 holder.tvAccountName.setPadding(
128 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
131 holder.tvAccountAmounts.setText(acc.getAmountsString());
133 if (acc.isHidden()) {
134 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
135 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
138 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
139 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
142 if (position % 2 == 0) {
143 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
144 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
145 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
148 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
149 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
150 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
153 holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
154 holder.selectionCb.setChecked(!acc.isHiddenToBe());
156 holder.row.setTag(R.id.POS, position);
161 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
162 View row = LayoutInflater.from(parent.getContext())
163 .inflate(R.layout.account_summary_row, parent, false);
164 return new LedgerRowHolder(row);
168 public int getItemCount() {
169 return accounts.size();
171 public void startSelection() {
172 for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
173 this.selectionActive = true;
174 notifyDataSetChanged();
177 public void stopSelection() {
178 this.selectionActive = false;
179 notifyDataSetChanged();
182 public boolean isSelectionActive() {
183 return selectionActive;
186 public void selectItem(int position) {
187 LedgerAccount acc = accounts.get(position);
188 acc.toggleHiddenToBe();
189 toggleChildrenOf(acc, acc.isHiddenToBe());
190 notifyDataSetChanged();
192 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
193 for (LedgerAccount acc : accounts) {
194 String acc_parent = acc.getParentName();
195 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
196 acc.setHiddenToBe(hiddenToBe);
197 toggleChildrenOf(acc, hiddenToBe);
201 class LedgerRowHolder extends RecyclerView.ViewHolder {
202 CheckBox selectionCb;
203 TextView tvAccountName, tvAccountAmounts;
205 public LedgerRowHolder(@NonNull View itemView) {
207 this.row = (LinearLayout) itemView;
208 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
209 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
210 this.selectionCb = itemView.findViewById(R.id.account_row_check);