]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
separate packages for the different aspects of the application
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / 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;
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.model.LedgerAccount;
40 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
46
47 class AccountSummaryViewModel extends AndroidViewModel {
48     private MobileLedgerDatabase dbh;
49     private List<LedgerAccount> accounts;
50
51     public AccountSummaryViewModel(@NonNull Application application) {
52         super(application);
53         dbh = new MobileLedgerDatabase(application);
54     }
55
56     List<LedgerAccount> getAccounts() {
57         if (accounts == null) {
58             accounts = new ArrayList<>();
59             reloadAccounts();
60         }
61
62         return accounts;
63     }
64
65     void reloadAccounts() {
66         accounts.clear();
67         boolean showingOnlyStarred =
68                 PreferenceManager.getDefaultSharedPreferences(getApplication())
69                         .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
70         String sql = "SELECT name, hidden FROM accounts";
71         if (showingOnlyStarred) sql += " WHERE hidden = 0";
72         sql += " ORDER BY name";
73
74         try (SQLiteDatabase db = dbh.getReadableDatabase()) {
75             try (Cursor cursor = db
76                     .rawQuery(sql,null))
77             {
78                 while (cursor.moveToNext()) {
79                     LedgerAccount acc = new LedgerAccount(cursor.getString(0));
80                     acc.setHidden(cursor.getInt(1) == 1);
81                     try (Cursor c2 = db.rawQuery(
82                             "SELECT value, currency FROM account_values " + "WHERE account = ?",
83                             new String[]{acc.getName()}))
84                     {
85                         while (c2.moveToNext()) {
86                             acc.addAmount(c2.getFloat(0), c2.getString(1));
87                         }
88                     }
89                     accounts.add(acc);
90                 }
91             }
92         }
93     }
94     void commitSelections() {
95         try(SQLiteDatabase db = dbh.getWritableDatabase()) {
96             db.beginTransaction();
97             try {
98                 for (LedgerAccount acc : accounts) {
99                     Log.d("db", String.format("Setting %s to %s", acc.getName(),
100                             acc.isHidden() ? "hidden" : "starred"));
101                     db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
102                             new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
103                 }
104                 db.setTransactionSuccessful();
105                 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
106             }
107             finally { db.endTransaction(); }
108         }
109     }
110 }
111
112 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.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 }