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.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;
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;
42 import java.util.ArrayList;
44 class AccountSummaryViewModel extends ViewModel {
45 static void commitSelections(Context context) {
48 new CommitAccountsTaskParams(Data.accounts.get(), Data.optShowOnlyStarred.get()));
50 void scheduleAccountListReload() {
56 private static class UAT extends UpdateAccountsTask {
58 protected void onPostExecute(ArrayList<LedgerAccount> list) {
59 super.onPostExecute(list);
61 Log.d("acc", "setting updated account list");
62 Data.accounts.set(list);
67 private static class CAT extends CommitAccountsTask {
69 protected void onPostExecute(ArrayList<LedgerAccount> list) {
70 super.onPostExecute(list);
72 Log.d("acc", "setting new account list");
73 Data.accounts.set(list);
79 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
80 private boolean selectionActive;
82 AccountSummaryAdapter() {
83 this.selectionActive = false;
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();
91 holder.tvAccountName.setText(acc.getShortName());
92 holder.tvAccountName.setPadding(
93 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
95 holder.tvAccountAmounts.setText(acc.getAmountsString());
98 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
99 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
102 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
103 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
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));
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));
117 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
118 holder.selectionCb.setChecked(!acc.isHiddenToBe());
120 holder.row.setTag(R.id.POS, position);
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);
132 public int getItemCount() {
133 return Data.accounts.get().size();
135 public void startSelection() {
136 for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
137 this.selectionActive = true;
138 notifyDataSetChanged();
141 public void stopSelection() {
142 this.selectionActive = false;
143 notifyDataSetChanged();
146 public boolean isSelectionActive() {
147 return selectionActive;
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);
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);
168 class LedgerRowHolder extends RecyclerView.ViewHolder {
169 CheckBox selectionCb;
170 TextView tvAccountName, tvAccountAmounts;
172 public LedgerRowHolder(@NonNull View 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);