]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java
+debug
[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.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;
35
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;
42
43 import java.util.ArrayList;
44
45 import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
46
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);
51
52         UAT task = new UAT();
53         task.execute(showingOnlyStarred);
54
55     }
56     static void commitSelections(Context context) {
57         boolean showingOnlyStarred = PreferenceManager.getDefaultSharedPreferences(context)
58                 .getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false);
59         CAT task = new CAT();
60         //noinspection unchecked
61         task.execute(new CommitAccountsTaskParams(Data.accounts.get(), showingOnlyStarred));
62     }
63     private static class UAT extends UpdateAccountsTask {
64         @Override
65         protected void onPostExecute(ArrayList<LedgerAccount> list) {
66             super.onPostExecute(list);
67             if (list != null) {
68                 Log.d("acc", "setting updated account list");
69                 Data.accounts.set(list);
70             }
71         }
72     }
73     private static class CAT extends CommitAccountsTask {
74         @Override
75         protected void onPostExecute(ArrayList<LedgerAccount> list) {
76             super.onPostExecute(list);
77             if (list != null) {
78                 Log.d("acc", "setting new account list");
79                 Data.accounts.set(list);
80             }
81         }
82     }
83 }
84
85 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
86     private boolean selectionActive;
87
88     AccountSummaryAdapter() {
89         this.selectionActive = false;
90     }
91
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();
96
97         holder.tvAccountName.setText(acc.getShortName());
98         holder.tvAccountName.setPadding(
99                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
100                 0, 0, 0);
101         holder.tvAccountAmounts.setText(acc.getAmountsString());
102
103         if (acc.isHidden()) {
104             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
105             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
106         }
107         else {
108             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
109             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
110         }
111
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));
116         }
117         else {
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));
121         }
122
123         holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
124         holder.selectionCb.setChecked(!acc.isHiddenToBe());
125
126         holder.row.setTag(R.id.POS, position);
127     }
128
129     @NonNull
130     @Override
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);
135     }
136
137     @Override
138     public int getItemCount() {
139         return Data.accounts.get().size();
140     }
141     public void startSelection() {
142         for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
143         this.selectionActive = true;
144         notifyDataSetChanged();
145     }
146
147     public void stopSelection() {
148         this.selectionActive = false;
149         notifyDataSetChanged();
150     }
151
152     public boolean isSelectionActive() {
153         return selectionActive;
154     }
155
156     public void selectItem(int position) {
157         LedgerAccount acc = Data.accounts.get().get(position);
158         acc.toggleHiddenToBe();
159         toggleChildrenOf(acc, acc.isHiddenToBe());
160         notifyDataSetChanged();
161     }
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);
168             }
169         }
170     }
171
172     class LedgerRowHolder extends RecyclerView.ViewHolder {
173         CheckBox selectionCb;
174         TextView tvAccountName, tvAccountAmounts;
175         LinearLayout row;
176         public LedgerRowHolder(@NonNull View itemView) {
177             super(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);
182         }
183     }
184 }