]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
more asynchronous account list (re-)loading
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryAdapter.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe 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  * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.text.TextUtils;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.TextView;
29
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.constraintlayout.widget.ConstraintLayout;
33 import androidx.recyclerview.widget.AsyncListDiffer;
34 import androidx.recyclerview.widget.DiffUtil;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.async.DbOpQueue;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.activity.MainActivity;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import java.util.ArrayList;
46
47 import static net.ktnx.mobileledger.utils.Logger.debug;
48
49 public class AccountSummaryAdapter
50         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
51     public static final int AMOUNT_LIMIT = 3;
52     private MobileLedgerProfile profile;
53     private AsyncListDiffer<LedgerAccount> listDiffer;
54     AccountSummaryAdapter() {
55         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
56             @Override
57             public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
58                                            @NotNull LedgerAccount newItem) {
59                 return TextUtils.equals(oldItem.getName(), newItem.getName());
60             }
61             @Override
62             public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
63                                               @NotNull LedgerAccount newItem) {
64                 return (oldItem.isExpanded() == newItem.isExpanded()) &&
65                        (oldItem.amountsExpanded() == newItem.amountsExpanded());
66             }
67         });
68     }
69
70     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
71         holder.bindToAccount(listDiffer.getCurrentList().get(position));
72     }
73
74     @NonNull
75     @Override
76     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
77         View row = LayoutInflater.from(parent.getContext())
78                                  .inflate(R.layout.account_summary_row, parent, false);
79         return new LedgerRowHolder(row);
80     }
81
82     @Override
83     public int getItemCount() {
84         return listDiffer.getCurrentList().size();
85     }
86     public void setAccounts(MobileLedgerProfile profile, ArrayList<LedgerAccount> newList) {
87         this.profile = profile;
88         listDiffer.submitList(newList);
89     }
90     class LedgerRowHolder extends RecyclerView.ViewHolder {
91         TextView tvAccountName, tvAccountAmounts;
92         ConstraintLayout row;
93         View expanderContainer;
94         ImageView expander;
95         View accountExpanderContainer;
96         public LedgerRowHolder(@NonNull View itemView) {
97             super(itemView);
98             row = itemView.findViewById(R.id.account_summary_row);
99             tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
100             tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
101             expanderContainer = itemView.findViewById(R.id.account_expander_container);
102             expander = itemView.findViewById(R.id.account_expander);
103             accountExpanderContainer =
104                     itemView.findViewById(R.id.account_row_amounts_expander_container);
105
106             itemView.setOnLongClickListener(this::onItemLongClick);
107             tvAccountName.setOnLongClickListener(this::onItemLongClick);
108             tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
109             expanderContainer.setOnLongClickListener(this::onItemLongClick);
110             expander.setOnLongClickListener(this::onItemLongClick);
111             row.setOnLongClickListener(this::onItemLongClick);
112
113             tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
114             expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
115             expander.setOnClickListener(v -> toggleAccountExpanded());
116             tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
117         }
118         private @NonNull
119         LedgerAccount getAccount() {
120             final ArrayList<LedgerAccount> accountList = profile.getAccounts()
121                                                                 .getValue();
122             if (accountList == null)
123                 throw new IllegalStateException("No account list");
124
125             return accountList.get(getAdapterPosition());
126         }
127         private void toggleAccountExpanded() {
128             LedgerAccount acc = getAccount();
129             if (!acc.hasSubAccounts())
130                 return;
131             debug("accounts", "Account expander clicked");
132
133             acc.toggleExpanded();
134             expanderContainer.animate()
135                              .rotation(acc.isExpanded() ? 0 : 180);
136
137             MobileLedgerProfile profile = acc.getProfile();
138             if (profile == null)
139                 return;
140
141             DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
142                     new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
143                     }, profile::scheduleAccountListReload);
144
145         }
146         private void toggleAmountsExpanded() {
147             LedgerAccount acc = getAccount();
148             if (acc.getAmountCount() <= AMOUNT_LIMIT)
149                 return;
150
151             acc.toggleAmountsExpanded();
152             if (acc.amountsExpanded()) {
153                 tvAccountAmounts.setText(acc.getAmountsString());
154                 accountExpanderContainer.setVisibility(View.GONE);
155             }
156             else {
157                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
158                 accountExpanderContainer.setVisibility(View.VISIBLE);
159             }
160
161             MobileLedgerProfile profile = acc.getProfile();
162             if (profile == null)
163                 return;
164
165             DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
166                     new Object[]{acc.amountsExpanded(), acc.getName(), profile.getUuid()
167                     });
168
169         }
170         private boolean onItemLongClick(View v) {
171             MainActivity activity = (MainActivity) v.getContext();
172             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
173             View row;
174             int id = v.getId();
175             switch (id) {
176                 case R.id.account_summary_row:
177                     row = v;
178                     break;
179                 case R.id.account_row_acc_amounts:
180                 case R.id.account_row_amounts_expander_container:
181                     row = (View) v.getParent();
182                     break;
183                 case R.id.account_row_acc_name:
184                 case R.id.account_expander_container:
185                     row = (View) v.getParent()
186                                   .getParent();
187                     break;
188                 case R.id.account_expander:
189                     row = (View) v.getParent()
190                                   .getParent()
191                                   .getParent();
192                     break;
193                 default:
194                     Log.e("error",
195                             String.format("Don't know how to handle long click on id %d", id));
196                     return false;
197             }
198             LedgerAccount acc = getAccount();
199             builder.setTitle(acc.getName());
200             builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
201                 switch (which) {
202                     case 0:
203                         // show transactions
204                         activity.showAccountTransactions(acc.getName());
205                         break;
206                 }
207                 dialog.dismiss();
208             });
209             builder.show();
210             return true;
211         }
212         public void bindToAccount(LedgerAccount acc) {
213             Context ctx = row.getContext();
214             Resources rm = ctx.getResources();
215
216             row.setTag(acc);
217
218             tvAccountName.setText(acc.getShortName());
219
220             ConstraintLayout.LayoutParams lp =
221                     (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
222             lp.setMarginStart(
223                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
224
225             if (acc.hasSubAccounts()) {
226                 expanderContainer.setVisibility(View.VISIBLE);
227                 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
228             }
229             else {
230                 expanderContainer.setVisibility(View.GONE);
231             }
232
233             int amounts = acc.getAmountCount();
234             if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
235                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
236                 accountExpanderContainer.setVisibility(View.VISIBLE);
237             }
238             else {
239                 tvAccountAmounts.setText(acc.getAmountsString());
240                 accountExpanderContainer.setVisibility(View.GONE);
241             }
242         }
243     }
244 }