]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java
fix traversing children when starring/un-starring accounts
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryViewModel.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
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.support.annotation.NonNull;
26 import android.support.v7.widget.RecyclerView;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.CheckBox;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
34
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.async.CommitAccountsTask;
37 import net.ktnx.mobileledger.async.CommitAccountsTaskParams;
38 import net.ktnx.mobileledger.async.UpdateAccountsTask;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41
42 import java.util.ArrayList;
43
44 class AccountSummaryViewModel extends ViewModel {
45     static void commitSelections(Context context) {
46         CAT task = new CAT();
47         task.execute(
48                 new CommitAccountsTaskParams(Data.accounts.get(), Data.optShowOnlyStarred.get()));
49     }
50     void scheduleAccountListReload() {
51         UAT task = new UAT();
52         task.execute();
53
54     }
55
56     private static class UAT extends UpdateAccountsTask {
57         @Override
58         protected void onPostExecute(ArrayList<LedgerAccount> list) {
59             super.onPostExecute(list);
60             if (list != null) {
61                 Log.d("acc", "setting updated account list");
62                 Data.accounts.set(list);
63             }
64         }
65     }
66
67     private static class CAT extends CommitAccountsTask {
68         @Override
69         protected void onPostExecute(ArrayList<LedgerAccount> list) {
70             super.onPostExecute(list);
71             if (list != null) {
72                 Log.d("acc", "setting new account list");
73                 Data.accounts.set(list);
74             }
75         }
76     }
77 }
78
79 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
80     private boolean selectionActive;
81
82     AccountSummaryAdapter() {
83         this.selectionActive = false;
84     }
85
86     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
87         LedgerAccount acc = Data.accounts.get().get(position);
88         Context ctx = holder.row.getContext();
89         Resources rm = ctx.getResources();
90
91         holder.tvAccountName.setText(acc.getShortName());
92         holder.tvAccountName.setPadding(
93                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
94                 0, 0, 0);
95         holder.tvAccountAmounts.setText(acc.getAmountsString());
96
97         if (acc.isHidden()) {
98             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
99             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
100         }
101         else {
102             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
103             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
104         }
105
106         if (position % 2 == 0) {
107             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
108                     .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
109             else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
110         }
111         else {
112             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
113                     .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
114             else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
115         }
116
117         holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
118         holder.selectionCb.setChecked(!acc.isHiddenToBe());
119
120         holder.row.setTag(R.id.POS, position);
121     }
122
123     @NonNull
124     @Override
125     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
126         View row = LayoutInflater.from(parent.getContext())
127                 .inflate(R.layout.account_summary_row, parent, false);
128         return new LedgerRowHolder(row);
129     }
130
131     @Override
132     public int getItemCount() {
133         return Data.accounts.get().size();
134     }
135     public void startSelection() {
136         for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
137         this.selectionActive = true;
138         notifyDataSetChanged();
139     }
140
141     public void stopSelection() {
142         this.selectionActive = false;
143         notifyDataSetChanged();
144     }
145
146     public boolean isSelectionActive() {
147         return selectionActive;
148     }
149
150     public void selectItem(int position) {
151         LedgerAccount acc = Data.accounts.get().get(position);
152         acc.toggleHiddenToBe();
153         toggleChildrenOf(acc, acc.isHiddenToBe(), position);
154         notifyItemChanged(position);
155     }
156     void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
157         int i = parentPosition + 1;
158         for (LedgerAccount acc : Data.accounts.get()) {
159             if (acc.getName().startsWith(parent.getName() + ":")) {
160                 acc.setHiddenToBe(hiddenToBe);
161                 notifyItemChanged(i);
162                 toggleChildrenOf(acc, hiddenToBe, i);
163                 i++;
164             }
165         }
166     }
167
168     class LedgerRowHolder extends RecyclerView.ViewHolder {
169         CheckBox selectionCb;
170         TextView tvAccountName, tvAccountAmounts;
171         LinearLayout row;
172         public LedgerRowHolder(@NonNull View itemView) {
173             super(itemView);
174             this.row = (LinearLayout) itemView;
175             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
176             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
177             this.selectionCb = itemView.findViewById(R.id.account_row_check);
178         }
179     }
180 }