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 java.util.ArrayList;
40 import java.util.List;
42 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
44 class AccountSummaryViewModel extends AndroidViewModel {
45 private MobileLedgerDatabase dbh;
46 private List<LedgerAccount> accounts;
48 public AccountSummaryViewModel(@NonNull Application application) {
50 dbh = new MobileLedgerDatabase(application);
53 List<LedgerAccount> getAccounts() {
54 if (accounts == null) {
55 accounts = new ArrayList<>();
62 void reloadAccounts() {
64 boolean showingOnlyStarred =
65 PreferenceManager.getDefaultSharedPreferences(getApplication())
66 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
67 String sql = "SELECT name, hidden FROM accounts";
68 if (showingOnlyStarred) sql += " WHERE hidden = 0";
69 sql += " ORDER BY name";
71 try (SQLiteDatabase db = dbh.getReadableDatabase()) {
72 try (Cursor cursor = db
75 while (cursor.moveToNext()) {
76 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
77 acc.setHidden(cursor.getInt(1) == 1);
78 try (Cursor c2 = db.rawQuery(
79 "SELECT value, currency FROM account_values " + "WHERE account = ?",
80 new String[]{acc.getName()}))
82 while (c2.moveToNext()) {
83 acc.addAmount(c2.getFloat(0), c2.getString(1));
91 void commitSelections() {
92 try(SQLiteDatabase db = dbh.getWritableDatabase()) {
93 db.beginTransaction();
95 for (LedgerAccount acc : accounts) {
96 Log.d("db", String.format("Setting %s to %s", acc.getName(),
97 acc.isHidden() ? "hidden" : "starred"));
98 db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
99 new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
101 db.setTransactionSuccessful();
102 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
104 finally { db.endTransaction(); }
109 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
110 private List<LedgerAccount> accounts;
111 private boolean selectionActive;
113 AccountSummaryAdapter(List<LedgerAccount> accounts) {
114 this.accounts = accounts;
115 this.selectionActive = false;
118 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
119 LedgerAccount acc = accounts.get(position);
120 Context ctx = holder.row.getContext();
121 Resources rm = ctx.getResources();
123 holder.tvAccountName.setText(acc.getShortName());
124 holder.tvAccountName.setPadding(
125 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
128 holder.tvAccountAmounts.setText(acc.getAmountsString());
130 if (acc.isHidden()) {
131 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
132 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
135 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
136 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
139 if (position % 2 == 0) {
140 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
141 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
142 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
145 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
146 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
147 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
150 holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
151 holder.selectionCb.setChecked(!acc.isHiddenToBe());
153 holder.row.setTag(R.id.POS, position);
158 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
159 View row = LayoutInflater.from(parent.getContext())
160 .inflate(R.layout.account_summary_row, parent, false);
161 return new LedgerRowHolder(row);
165 public int getItemCount() {
166 return accounts.size();
168 public void startSelection() {
169 for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
170 this.selectionActive = true;
171 notifyDataSetChanged();
174 public void stopSelection() {
175 this.selectionActive = false;
176 notifyDataSetChanged();
179 public boolean isSelectionActive() {
180 return selectionActive;
183 public void selectItem(int position) {
184 LedgerAccount acc = accounts.get(position);
185 acc.toggleHiddenToBe();
186 toggleChildrenOf(acc, acc.isHiddenToBe());
187 notifyDataSetChanged();
189 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
190 for (LedgerAccount acc : accounts) {
191 String acc_parent = acc.getParentName();
192 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
193 acc.setHiddenToBe(hiddenToBe);
194 toggleChildrenOf(acc, hiddenToBe);
198 class LedgerRowHolder extends RecyclerView.ViewHolder {
199 CheckBox selectionCb;
200 TextView tvAccountName, tvAccountAmounts;
202 public LedgerRowHolder(@NonNull View itemView) {
204 this.row = (LinearLayout) itemView;
205 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
206 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
207 this.selectionCb = itemView.findViewById(R.id.account_row_check);