]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
c1f1625812a91baab8aa8bdbe932e7e7970f7bca
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / AccountSummaryViewModel.java
1 package net.ktnx.mobileledger;
2
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;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 class AccountSummaryViewModel extends AndroidViewModel {
25     private MobileLedgerDatabase dbh;
26     private List<LedgerAccount> accounts;
27
28     public AccountSummaryViewModel(@NonNull Application application) {
29         super(application);
30         dbh = new MobileLedgerDatabase(application);
31     }
32
33     List<LedgerAccount> getAccounts() {
34         if (accounts == null) {
35             accounts = new ArrayList<>();
36             reloadAccounts();
37         }
38
39         return accounts;
40     }
41
42     void reloadAccounts() {
43         accounts.clear();
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";
50
51         try (SQLiteDatabase db = dbh.getReadableDatabase()) {
52             try (Cursor cursor = db
53                     .rawQuery(sql,null))
54             {
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()}))
61                     {
62                         while (c2.moveToNext()) {
63                             acc.addAmount(c2.getFloat(0), c2.getString(1));
64                         }
65                     }
66                     accounts.add(acc);
67                 }
68             }
69         }
70     }
71 }
72
73 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
74     private List<LedgerAccount> accounts;
75     private boolean selectionActive;
76
77     AccountSummaryAdapter(List<LedgerAccount> accounts) {
78         this.accounts = accounts;
79         this.selectionActive = false;
80     }
81
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();
86
87         holder.tvAccountName.setText(acc.getShortName());
88         holder.tvAccountName.setPadding(
89                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
90                 0, 0,
91                 0);
92         holder.tvAccountAmounts.setText(acc.getAmountsString());
93
94         if (acc.isHidden()) {
95             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
96             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
97         }
98         else {
99             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
100             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
101         }
102
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));
107         }
108         else {
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));
112         }
113
114         holder.selectionCb.setVisibility( selectionActive ? View.VISIBLE : View.GONE);
115         holder.selectionCb.setChecked(acc.isSelected());
116
117         holder.row.setTag(R.id.POS, position);
118     }
119
120     @NonNull
121     @Override
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);
126     }
127
128     @Override
129     public int getItemCount() {
130         return accounts.size();
131     }
132
133     public void startSelection() {
134         for( LedgerAccount acc : accounts ) acc.setSelected(false);
135         this.selectionActive = true;
136         notifyDataSetChanged();
137     }
138
139     public void stopSelection() {
140         this.selectionActive = false;
141         notifyDataSetChanged();
142     }
143
144     public boolean isSelectionActive() {
145         return selectionActive;
146     }
147
148     public void selectItem(int position) {
149         accounts.get(position).toggleSelected();
150         notifyItemChanged(position);
151     }
152     class LedgerRowHolder extends RecyclerView.ViewHolder {
153         CheckBox selectionCb;
154         TextView tvAccountName, tvAccountAmounts;
155         LinearLayout row;
156         public LedgerRowHolder(@NonNull View itemView) {
157             super(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);
162         }
163     }
164 }