]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/AccountSummaryViewModel.java
major rework of the async stuff, view model, pull-to-refresh account list
[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
153 //    @NonNull
154 //    @Override
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);
162 //
163 //            if (vi == null)
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);
169 //
170 //            row.setPadding(context.getResources()
171 //                            .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2, dp2px
172 //                            (context, 3),
173 //                    context.getResources()
174 //                            .getDimensionPixelSize(R.dimen.activity_horizontal_margin)/2,
175 //                    dp2px(context, 4));
176 //            View.OnCreateContextMenuListener ccml = new View.OnCreateContextMenuListener() {
177 //                @Override
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));
184 //                }
185 //            };
186 //            row.setOnCreateContextMenuListener(ccml);
187 //
188 //        }
189 //        else holder = (LedgerRowHolder) row.getTag(R.id.VH);
190 //
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());
195 //
196 //        if (acc.isHidden()) {
197 //            holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
198 //            holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
199 //        }
200 //        else {
201 //            holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
202 //            holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
203 //        }
204 //
205 //        int real_pos = ((ListView)parent).getPositionForView(row);
206 //        Log.d("model", String.format("%s: real_pos=%d, position=%d", acc.getName(), real_pos,
207 //                position));
208 //        if (real_pos == -1) real_pos = position+1;
209 //        else real_pos = real_pos + 1;
210 //
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()));
215 //            }
216 //            else {
217 //                row.setBackgroundColor(
218 //                        context.getResources().getColor(R.color.table_row_even_bg));
219 //            }
220 //        }
221 //        else {
222 //            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
223 //                row.setBackgroundColor(context.getResources()
224 //                        .getColor(R.color.drawer_background, context.getTheme()));
225 //            }
226 //            else {
227 //                row.setBackgroundColor(context.getResources().getColor(R.color.drawer_background));
228 //            }
229 //        }
230 //
231 //        row.setTag(R.id.POS, position);
232 //
233 //        return row;
234 //    }
235
236     class LedgerRowHolder extends RecyclerView.ViewHolder {
237         CheckBox selectionCb;
238         TextView tvAccountName, tvAccountAmounts;
239         LinearLayout row;
240
241         public LedgerRowHolder(@NonNull View itemView) {
242             super(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);
247         }
248     }
249 }