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);
155 // public View getView(int position, @Nullable View row, @NonNull ViewGroup parent) {
156 // LedgerAccount acc = getItem(position);
157 // LedgerRowHolder holder;
158 // if (row == null) {
159 // holder = new LedgerRowHolder();
160 // LayoutInflater vi = getSystemService(this.getContext(), LayoutInflater.class);
161 // MenuInflater mi = getSystemService(this.getContext(), MenuInflater.class);
164 // throw new IllegalStateException("Unable to instantiate the inflater " + "service");
165 // row = vi.inflate(R.layout.account_summary_row, parent, false);
166 // holder.tvAccountName = row.findViewById(R.id.account_row_acc_name);
167 // holder.tvAccountAmounts = row.findViewById(R.id.account_row_acc_amounts);
168 // row.setTag(R.id.VH, holder);
170 // row.setPadding(context.getResources()
171 // .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2, dp2px
173 // context.getResources()
174 // .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
175 // dp2px(context, 4));
176 // View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() {
178 // public void onCreateContextMenu(ContextMenu menu, View v,
179 // ContextMenu.ContextMenuInfo menuInfo) {
180 // final ListView parent = (ListView) v.getParent();
181 // int pos = parent.getPositionForView(v);
182 // parent.setItemChecked(pos, true);
183 // Log.d("list", String.format("checking pos %d", pos));
186 // row.setOnCreateContextMenuListener(ccml);
189 // else holder = (LedgerRowHolder) row.getTag(R.id.VH);
191 // holder.tvAccountName.setText(acc.getShortName());
192 // holder.tvAccountName.setPadding(acc.getLevel() * context.getResources()
193 // .getDimensionPixelSize(R.dimen.activity_horizontal_margin), 0, 0, 0);
194 // holder.tvAccountAmounts.setText(acc.getAmountsString());
196 // if (acc.isHidden()) {
197 // holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
198 // holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
201 // holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
202 // holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
205 // int real_pos = ((ListView)parent).getPositionForView(row);
206 // Log.d("model", String.format("%s: real_pos=%d, position=%d", acc.getName(), real_pos,
208 // if (real_pos == -1) real_pos = position+1;
209 // else real_pos = real_pos + 1;
211 // if ( real_pos % 2 == 0) {
212 // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
213 // row.setBackgroundColor(context.getResources()
214 // .getColor(R.color.table_row_even_bg, context.getTheme()));
217 // row.setBackgroundColor(
218 // context.getResources().getColor(R.color.table_row_even_bg));
222 // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
223 // row.setBackgroundColor(context.getResources()
224 // .getColor(R.color.drawer_background, context.getTheme()));
227 // row.setBackgroundColor(context.getResources().getColor(R.color.drawer_background));
231 // row.setTag(R.id.POS, position);
236 class LedgerRowHolder extends RecyclerView.ViewHolder {
237 CheckBox selectionCb;
238 TextView tvAccountName, tvAccountAmounts;
241 public LedgerRowHolder(@NonNull View itemView) {
243 this.row = (LinearLayout) itemView;
244 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
245 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
246 this.selectionCb = itemView.findViewById(R.id.account_row_check);