1 package net.ktnx.mobileledger;
3 import android.app.Application;
4 import android.arch.lifecycle.AndroidViewModel;
5 import android.content.Context;
6 import android.content.res.Resources;
7 import android.database.Cursor;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.graphics.Typeface;
10 import android.os.Build;
11 import android.preference.PreferenceManager;
12 import android.support.annotation.NonNull;
13 import android.support.v7.widget.RecyclerView;
14 import android.view.LayoutInflater;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.CheckBox;
18 import android.widget.LinearLayout;
19 import android.widget.TextView;
21 import java.util.ArrayList;
22 import java.util.List;
24 class AccountSummaryViewModel extends AndroidViewModel {
25 private MobileLedgerDatabase dbh;
26 private List<LedgerAccount> accounts;
28 public AccountSummaryViewModel(@NonNull Application application) {
30 dbh = new MobileLedgerDatabase(application);
33 List<LedgerAccount> getAccounts() {
34 if (accounts == null) {
35 accounts = new ArrayList<>();
42 void reloadAccounts() {
44 boolean showingHiddenAccounts =
45 PreferenceManager.getDefaultSharedPreferences(getApplication())
46 .getBoolean("show_hidden_accounts", false);
47 String sql = "SELECT name, hidden FROM accounts";
48 if (!showingHiddenAccounts) sql += " WHERE hidden = 0";
49 sql += " ORDER BY name";
51 try (SQLiteDatabase db = dbh.getReadableDatabase()) {
52 try (Cursor cursor = db
55 while (cursor.moveToNext()) {
56 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
57 acc.setHidden(cursor.getInt(1) == 1);
58 try (Cursor c2 = db.rawQuery(
59 "SELECT value, currency FROM account_values " + "WHERE account = ?",
60 new String[]{acc.getName()}))
62 while (c2.moveToNext()) {
63 acc.addAmount(c2.getFloat(0), c2.getString(1));
73 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
74 private List<LedgerAccount> accounts;
75 private boolean selectionActive;
77 AccountSummaryAdapter(List<LedgerAccount> accounts) {
78 this.accounts = accounts;
79 this.selectionActive = false;
82 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
83 LedgerAccount acc = accounts.get(position);
84 Context ctx = holder.row.getContext();
85 Resources rm = ctx.getResources();
87 holder.tvAccountName.setText(acc.getShortName());
88 holder.tvAccountName.setPadding(
89 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
92 holder.tvAccountAmounts.setText(acc.getAmountsString());
95 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
96 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
99 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
100 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
103 if (position % 2 == 0) {
104 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
105 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
106 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
109 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
110 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
111 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
114 holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
115 holder.selectionCb.setChecked(acc.isSelected());
117 holder.row.setTag(R.id.POS, position);
122 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
123 View row = LayoutInflater.from(parent.getContext())
124 .inflate(R.layout.account_summary_row, parent, false);
125 return new LedgerRowHolder(row);
129 public int getItemCount() {
130 return accounts.size();
133 public void startSelection() {
134 for( LedgerAccount acc : accounts ) acc.setSelected(false);
135 this.selectionActive = true;
136 notifyDataSetChanged();
139 public void stopSelection() {
140 this.selectionActive = false;
141 notifyDataSetChanged();
144 public boolean isSelectionActive() {
145 return selectionActive;
148 public void selectItem(int position) {
149 accounts.get(position).toggleSelected();
150 notifyItemChanged(position);
152 class LedgerRowHolder extends RecyclerView.ViewHolder {
153 CheckBox selectionCb;
154 TextView tvAccountName, tvAccountAmounts;
156 public LedgerRowHolder(@NonNull View itemView) {
158 this.row = (LinearLayout) itemView;
159 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
160 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
161 this.selectionCb = itemView.findViewById(R.id.account_row_check);