]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
information about transaction id when having trouble parsing details
[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.MLDB;
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 List<LedgerAccount> accounts;
49
50     public AccountSummaryViewModel(@NonNull Application application) {
51         super(application);
52     }
53
54     List<LedgerAccount> getAccounts(Context context) {
55         if (accounts == null) {
56             accounts = new ArrayList<>();
57             reloadAccounts(context);
58         }
59
60         return accounts;
61     }
62
63     void reloadAccounts(Context context) {
64         accounts.clear();
65         boolean showingOnlyStarred =
66                 PreferenceManager.getDefaultSharedPreferences(getApplication())
67                         .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
68         String sql = "SELECT name, hidden FROM accounts";
69         if (showingOnlyStarred) sql += " WHERE hidden = 0";
70         sql += " ORDER BY name";
71
72         try (SQLiteDatabase db = MLDB.getReadableDatabase(context)) {
73             try (Cursor cursor = db
74                     .rawQuery(sql,null))
75             {
76                 while (cursor.moveToNext()) {
77                     LedgerAccount acc = new LedgerAccount(cursor.getString(0));
78                     acc.setHidden(cursor.getInt(1) == 1);
79                     try (Cursor c2 = db.rawQuery(
80                             "SELECT value, currency FROM account_values " + "WHERE account = ?",
81                             new String[]{acc.getName()}))
82                     {
83                         while (c2.moveToNext()) {
84                             acc.addAmount(c2.getFloat(0), c2.getString(1));
85                         }
86                     }
87                     accounts.add(acc);
88                 }
89             }
90         }
91     }
92     void commitSelections(Context context) {
93         try(SQLiteDatabase db = MLDB.getWritableDatabase(context)) {
94             db.beginTransaction();
95             try {
96                 for (LedgerAccount acc : accounts) {
97                     Log.d("db", String.format("Setting %s to %s", acc.getName(),
98                             acc.isHidden() ? "hidden" : "starred"));
99                     db.execSQL("UPDATE accounts SET hidden=? WHERE name=?",
100                             new Object[]{acc.isHiddenToBe() ? 1 : 0, acc.getName()});
101                 }
102                 db.setTransactionSuccessful();
103                 for (LedgerAccount acc : accounts ) { acc.setHidden(acc.isHiddenToBe()); }
104             }
105             finally { db.endTransaction(); }
106         }
107     }
108 }
109
110 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
111     private List<LedgerAccount> accounts;
112     private boolean selectionActive;
113
114     AccountSummaryAdapter(List<LedgerAccount> accounts) {
115         this.accounts = accounts;
116         this.selectionActive = false;
117     }
118
119     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
120         LedgerAccount acc = accounts.get(position);
121         Context ctx = holder.row.getContext();
122         Resources rm = ctx.getResources();
123
124         holder.tvAccountName.setText(acc.getShortName());
125         holder.tvAccountName.setPadding(
126                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
127                 0, 0,
128                 0);
129         holder.tvAccountAmounts.setText(acc.getAmountsString());
130
131         if (acc.isHidden()) {
132             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
133             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
134         }
135         else {
136             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
137             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
138         }
139
140         if (position % 2 == 0) {
141             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
142                     .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
143             else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
144         }
145         else {
146             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
147                     .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
148             else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
149         }
150
151         holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
152         holder.selectionCb.setChecked(!acc.isHiddenToBe());
153
154         holder.row.setTag(R.id.POS, position);
155     }
156
157     @NonNull
158     @Override
159     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
160         View row = LayoutInflater.from(parent.getContext())
161                 .inflate(R.layout.account_summary_row, parent, false);
162         return new LedgerRowHolder(row);
163     }
164
165     @Override
166     public int getItemCount() {
167         return accounts.size();
168     }
169     public void startSelection() {
170         for( LedgerAccount acc : accounts ) acc.setHiddenToBe(acc.isHidden());
171         this.selectionActive = true;
172         notifyDataSetChanged();
173     }
174
175     public void stopSelection() {
176         this.selectionActive = false;
177         notifyDataSetChanged();
178     }
179
180     public boolean isSelectionActive() {
181         return selectionActive;
182     }
183
184     public void selectItem(int position) {
185         LedgerAccount acc = accounts.get(position);
186         acc.toggleHiddenToBe();
187         toggleChildrenOf(acc, acc.isHiddenToBe());
188         notifyDataSetChanged();
189     }
190     void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
191         for (LedgerAccount acc : accounts) {
192             String acc_parent = acc.getParentName();
193             if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
194                 acc.setHiddenToBe(hiddenToBe);
195                 toggleChildrenOf(acc, hiddenToBe);
196             }
197         }
198     }
199     class LedgerRowHolder extends RecyclerView.ViewHolder {
200         CheckBox selectionCb;
201         TextView tvAccountName, tvAccountAmounts;
202         LinearLayout row;
203         public LedgerRowHolder(@NonNull View itemView) {
204             super(itemView);
205             this.row = (LinearLayout) itemView;
206             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
207             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
208             this.selectionCb = itemView.findViewById(R.id.account_row_check);
209         }
210     }
211 }