2 * Copyright © 2019 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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.account_summary;
20 import android.arch.lifecycle.ViewModel;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.graphics.Typeface;
24 import android.os.Build;
25 import android.preference.PreferenceManager;
26 import android.support.annotation.NonNull;
27 import android.support.v7.widget.RecyclerView;
28 import android.util.Log;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.CheckBox;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.CommitAccountsTask;
38 import net.ktnx.mobileledger.async.CommitAccountsTaskParams;
39 import net.ktnx.mobileledger.async.UpdateAccountsTask;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.LedgerAccount;
43 import java.util.ArrayList;
45 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
47 class AccountSummaryViewModel extends ViewModel {
48 void scheduleAccountListReload(Context context) {
49 boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context)
50 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
53 task.execute(showingOnlyStarred);
56 static void commitSelections(Context context) {
57 boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context)
58 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
60 //noinspection unchecked
61 task.execute(new CommitAccountsTaskParams(Data.accounts.get(), showingOnlyStarred));
63 private static class UAT extends UpdateAccountsTask {
65 protected void onPostExecute(ArrayList<LedgerAccount> list) {
66 super.onPostExecute(list);
68 Log.d("acc", "setting updated account list");
69 Data.accounts.set(list);
73 private static class CAT extends CommitAccountsTask {
75 protected void onPostExecute(ArrayList<LedgerAccount> list) {
76 super.onPostExecute(list);
78 Log.d("acc", "setting new account list");
79 Data.accounts.set(list);
85 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
86 private boolean selectionActive;
88 AccountSummaryAdapter() {
89 this.selectionActive = false;
92 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
93 LedgerAccount acc = Data.accounts.get().get(position);
94 Context ctx = holder.row.getContext();
95 Resources rm = ctx.getResources();
97 holder.tvAccountName.setText(acc.getShortName());
98 holder.tvAccountName.setPadding(
99 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
101 holder.tvAccountAmounts.setText(acc.getAmountsString());
103 if (acc.isHidden()) {
104 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
105 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
108 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
109 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
112 if (position % 2 == 0) {
113 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
114 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
115 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
118 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
119 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
120 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
123 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
124 holder.selectionCb.setChecked(!acc.isHiddenToBe());
126 holder.row.setTag(R.id.POS, position);
131 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
132 View row = LayoutInflater.from(parent.getContext())
133 .inflate(R.layout.account_summary_row, parent, false);
134 return new LedgerRowHolder(row);
138 public int getItemCount() {
139 return Data.accounts.get().size();
141 public void startSelection() {
142 for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
143 this.selectionActive = true;
144 notifyDataSetChanged();
147 public void stopSelection() {
148 this.selectionActive = false;
149 notifyDataSetChanged();
152 public boolean isSelectionActive() {
153 return selectionActive;
156 public void selectItem(int position) {
157 LedgerAccount acc = Data.accounts.get().get(position);
158 acc.toggleHiddenToBe();
159 toggleChildrenOf(acc, acc.isHiddenToBe());
160 notifyDataSetChanged();
162 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
163 for (LedgerAccount acc : Data.accounts.get()) {
164 String acc_parent = acc.getParentName();
165 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
166 acc.setHiddenToBe(hiddenToBe);
167 toggleChildrenOf(acc, hiddenToBe);
172 class LedgerRowHolder extends RecyclerView.ViewHolder {
173 CheckBox selectionCb;
174 TextView tvAccountName, tvAccountAmounts;
176 public LedgerRowHolder(@NonNull View itemView) {
178 this.row = (LinearLayout) itemView;
179 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
180 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
181 this.selectionCb = itemView.findViewById(R.id.account_row_check);