]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
0a523c7fa94266c02245186a85d2aa9c380dfea2
[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 java.util.ArrayList;
40 import java.util.List;
41
42 import static net.ktnx.mobileledger.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
43
44 class AccountSummaryViewModel extends AndroidViewModel {
45     private MobileLedgerDatabase dbh;
46     private List<LedgerAccount> accounts;
47
48     public AccountSummaryViewModel(@NonNull Application application) {
49         super(application);
50         dbh = new MobileLedgerDatabase(application);
51     }
52
53     List<LedgerAccount> getAccounts() {
54         if (accounts == null) {
55             accounts = new ArrayList<>();
56             reloadAccounts();
57         }
58
59         return accounts;
60     }
61
62     void reloadAccounts() {
63         accounts.clear();
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";
70
71         try (SQLiteDatabase db = dbh.getReadableDatabase()) {
72             try (Cursor cursor = db
73                     .rawQuery(sql,null))
74             {
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()}))
81                     {
82                         while (c2.moveToNext()) {
83                             acc.addAmount(c2.getFloat(0), c2.getString(1));
84                         }
85                     }
86                     accounts.add(acc);
87                 }
88             }
89         }
90     }
91     void commitSelections() {
92         try(SQLiteDatabase db = dbh.getWritableDatabase()) {
93             db.beginTransaction();
94             try {
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()});
100                 }
101                 db.setTransactionSuccessful();
102                 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
103             }
104             finally { db.endTransaction(); }
105         }
106     }
107 }
108
109 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
110     private List<LedgerAccount> accounts;
111     private boolean selectionActive;
112
113     AccountSummaryAdapter(List<LedgerAccount> accounts) {
114         this.accounts = accounts;
115         this.selectionActive = false;
116     }
117
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();
122
123         holder.tvAccountName.setText(acc.getShortName());
124         holder.tvAccountName.setPadding(
125                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
126                 0, 0,
127                 0);
128         holder.tvAccountAmounts.setText(acc.getAmountsString());
129
130         if (acc.isHidden()) {
131             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
132             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
133         }
134         else {
135             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
136             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
137         }
138
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));
143         }
144         else {
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));
148         }
149
150         holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
151         holder.selectionCb.setChecked(!acc.isHiddenToBe());
152
153         holder.row.setTag(R.id.POS, position);
154     }
155
156     @NonNull
157     @Override
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);
162     }
163
164     @Override
165     public int getItemCount() {
166         return accounts.size();
167     }
168     public void startSelection() {
169         for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
170         this.selectionActive = true;
171         notifyDataSetChanged();
172     }
173
174     public void stopSelection() {
175         this.selectionActive = false;
176         notifyDataSetChanged();
177     }
178
179     public boolean isSelectionActive() {
180         return selectionActive;
181     }
182
183     public void selectItem(int position) {
184         LedgerAccount acc = accounts.get(position);
185         acc.toggleHiddenToBe();
186         toggleChildrenOf(acc, acc.isHiddenToBe());
187         notifyDataSetChanged();
188     }
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);
195             }
196         }
197     }
198     class LedgerRowHolder extends RecyclerView.ViewHolder {
199         CheckBox selectionCb;
200         TextView tvAccountName, tvAccountAmounts;
201         LinearLayout row;
202         public LedgerRowHolder(@NonNull View itemView) {
203             super(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);
208         }
209     }
210 }