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);
67 if (list != null) Data.accounts.set(list);
70 private static class CAT extends CommitAccountsTask {
72 protected void onPostExecute(ArrayList<LedgerAccount> list) {
73 super.onPostExecute(list);
75 Log.d("acc", "setting new account list");
76 Data.accounts.set(list);
82 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
83 private boolean selectionActive;
85 AccountSummaryAdapter() {
86 this.selectionActive = false;
89 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
90 LedgerAccount acc = Data.accounts.get().get(position);
91 Context ctx = holder.row.getContext();
92 Resources rm = ctx.getResources();
94 holder.tvAccountName.setText(acc.getShortName());
95 holder.tvAccountName.setPadding(
96 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
98 holder.tvAccountAmounts.setText(acc.getAmountsString());
100 if (acc.isHidden()) {
101 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
102 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
105 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
106 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
109 if (position % 2 == 0) {
110 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
111 .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
112 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
115 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
116 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
117 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
120 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
121 holder.selectionCb.setChecked(!acc.isHiddenToBe());
123 holder.row.setTag(R.id.POS, position);
128 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
129 View row = LayoutInflater.from(parent.getContext())
130 .inflate(R.layout.account_summary_row, parent, false);
131 return new LedgerRowHolder(row);
135 public int getItemCount() {
136 return Data.accounts.get().size();
138 public void startSelection() {
139 for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
140 this.selectionActive = true;
141 notifyDataSetChanged();
144 public void stopSelection() {
145 this.selectionActive = false;
146 notifyDataSetChanged();
149 public boolean isSelectionActive() {
150 return selectionActive;
153 public void selectItem(int position) {
154 LedgerAccount acc = Data.accounts.get().get(position);
155 acc.toggleHiddenToBe();
156 toggleChildrenOf(acc, acc.isHiddenToBe());
157 notifyDataSetChanged();
159 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe) {
160 for (LedgerAccount acc : Data.accounts.get()) {
161 String acc_parent = acc.getParentName();
162 if ((acc_parent != null) && acc.getParentName().equals(parent.getName())) {
163 acc.setHiddenToBe(hiddenToBe);
164 toggleChildrenOf(acc, hiddenToBe);
169 class LedgerRowHolder extends RecyclerView.ViewHolder {
170 CheckBox selectionCb;
171 TextView tvAccountName, tvAccountAmounts;
173 public LedgerRowHolder(@NonNull View itemView) {
175 this.row = (LinearLayout) itemView;
176 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
177 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
178 this.selectionCb = itemView.findViewById(R.id.account_row_check);