]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryViewModel.java
async account list
[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) Data.accounts.set(list);
68         }
69     }
70     private static class CAT extends CommitAccountsTask {
71         @Override
72         protected void onPostExecute(ArrayList<LedgerAccount> list) {
73             super.onPostExecute(list);
74             if (list != null) {
75                 Log.d("acc", "setting new account list");
76                 Data.accounts.set(list);
77             }
78         }
79     }
80 }
81
82 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
83     private boolean selectionActive;
84
85     AccountSummaryAdapter() {
86         this.selectionActive = false;
87     }
88
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();
93
94         holder.tvAccountName.setText(acc.getShortName());
95         holder.tvAccountName.setPadding(
96                 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
97                 0, 0, 0);
98         holder.tvAccountAmounts.setText(acc.getAmountsString());
99
100         if (acc.isHidden()) {
101             holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
102             holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
103         }
104         else {
105             holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
106             holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
107         }
108
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));
113         }
114         else {
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));
118         }
119
120         holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
121         holder.selectionCb.setChecked(!acc.isHiddenToBe());
122
123         holder.row.setTag(R.id.POS, position);
124     }
125
126     @NonNull
127     @Override
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);
132     }
133
134     @Override
135     public int getItemCount() {
136         return Data.accounts.get().size();
137     }
138     public void startSelection() {
139         for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
140         this.selectionActive = true;
141         notifyDataSetChanged();
142     }
143
144     public void stopSelection() {
145         this.selectionActive = false;
146         notifyDataSetChanged();
147     }
148
149     public boolean isSelectionActive() {
150         return selectionActive;
151     }
152
153     public void selectItem(int position) {
154         LedgerAccount acc = Data.accounts.get().get(position);
155         acc.toggleHiddenToBe();
156         toggleChildrenOf(acc, acc.isHiddenToBe());
157         notifyDataSetChanged();
158     }
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);
165             }
166         }
167     }
168
169     class LedgerRowHolder extends RecyclerView.ViewHolder {
170         CheckBox selectionCb;
171         TextView tvAccountName, tvAccountAmounts;
172         LinearLayout row;
173         public LedgerRowHolder(@NonNull View itemView) {
174             super(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);
179         }
180     }
181 }