]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java
streamlined database utility, fed with the application context upon startup
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryViewModel.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
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;
38
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.utils.MLDB;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
47
48 class AccountSummaryViewModel extends AndroidViewModel {
49     private List<LedgerAccount> accounts;
50
51     public AccountSummaryViewModel(@NonNull Application application) {
52         super(application);
53     }
54
55     List<LedgerAccount> getAccounts(Context context) {
56         if (accounts == null) {
57             accounts = new ArrayList<>();
58             reloadAccounts(context);
59         }
60
61         return accounts;
62     }
63
64     void reloadAccounts(Context context) {
65         accounts.clear();
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";
72
73         try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
74             try (Cursor cursor = db
75                     .rawQuery(sql,null))
76             {
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()}))
83                     {
84                         while (c2.moveToNext()) {
85                             acc.addAmount(c2.getFloat(0), c2.getString(1));
86                         }
87                     }
88                     accounts.add(acc);
89                 }
90             }
91         }
92     }
93     void commitSelections(Context context) {
94         try(SQLiteDatabase db = MLDB.getWritableDatabase(context)) {
95             db.beginTransaction();
96             try {
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()});
102                 }
103                 db.setTransactionSuccessful();
104                 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
105             }
106             finally { db.endTransaction(); }
107         }
108     }
109 }
110
111 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter
112 .LedgerRowHolder> {
113     private List<LedgerAccount> accounts;
114     private boolean selectionActive;
115
116     AccountSummaryAdapter(List<LedgerAccount> accounts) {
117         this.accounts = accounts;
118         this.selectionActive = false;
119     }
120
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();
125
126         holder.tvAccountName.setText(acc.getShortName());
127         holder.tvAccountName.setPadding(
128                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
129                 0, 0,
130                 0);
131         holder.tvAccountAmounts.setText(acc.getAmountsString());
132
133         if (acc.isHidden()) {
134             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
135             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
136         }
137         else {
138             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
139             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
140         }
141
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));
146         }
147         else {
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));
151         }
152
153         holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
154         holder.selectionCb.setChecked(!acc.isHiddenToBe());
155
156         holder.row.setTag(R.id.POS, position);
157     }
158
159     @NonNull
160     @Override
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);
165     }
166
167     @Override
168     public int getItemCount() {
169         return accounts.size();
170     }
171     public void startSelection() {
172         for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
173         this.selectionActive = true;
174         notifyDataSetChanged();
175     }
176
177     public void stopSelection() {
178         this.selectionActive = false;
179         notifyDataSetChanged();
180     }
181
182     public boolean isSelectionActive() {
183         return selectionActive;
184     }
185
186     public void selectItem(int position) {
187         LedgerAccount acc = accounts.get(position);
188         acc.toggleHiddenToBe();
189         toggleChildrenOf(acc, acc.isHiddenToBe());
190         notifyDataSetChanged();
191     }
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);
198             }
199         }
200     }
201     class LedgerRowHolder extends RecyclerView.ViewHolder {
202         CheckBox selectionCb;
203         TextView tvAccountName, tvAccountAmounts;
204         LinearLayout row;
205         public LedgerRowHolder(@NonNull View itemView) {
206             super(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);
211         }
212     }
213 }