]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
drop unneeded profile parameter
[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.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28
29 import androidx.annotation.NonNull;
30 import androidx.appcompat.app.AlertDialog;
31 import androidx.constraintlayout.widget.ConstraintLayout;
32 import androidx.recyclerview.widget.AsyncListDiffer;
33 import androidx.recyclerview.widget.DiffUtil;
34 import androidx.recyclerview.widget.RecyclerView;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.DbOpQueue;
38 import net.ktnx.mobileledger.model.LedgerAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.ui.activity.MainActivity;
41 import net.ktnx.mobileledger.utils.Locker;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import java.util.List;
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 AsyncListDiffer<LedgerAccount> listDiffer;
53     AccountSummaryAdapter() {
54         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
55             @Override
56             public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
57                                            @NotNull LedgerAccount newItem) {
58                 return TextUtils.equals(oldItem.getName(), newItem.getName());
59             }
60             @Override
61             public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
62                                               @NotNull LedgerAccount newItem) {
63                 return oldItem.equals(newItem);
64             }
65         });
66     }
67
68     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
69         holder.bindToAccount(listDiffer.getCurrentList()
70                                        .get(position));
71     }
72
73     @NonNull
74     @Override
75     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
76         View row = LayoutInflater.from(parent.getContext())
77                                  .inflate(R.layout.account_summary_row, parent, false);
78         return new LedgerRowHolder(row);
79     }
80
81     @Override
82     public int getItemCount() {
83         return listDiffer.getCurrentList()
84                          .size();
85     }
86     public void setAccounts(List<LedgerAccount> newList) {
87         listDiffer.submitList(newList);
88     }
89     static class LedgerRowHolder extends RecyclerView.ViewHolder {
90         TextView tvAccountName, tvAccountAmounts;
91         ConstraintLayout row;
92         View expanderContainer;
93         ImageView expander;
94         View accountExpanderContainer;
95         LedgerAccount mAccount;
96         public LedgerRowHolder(@NonNull View itemView) {
97             super(itemView);
98
99             row = itemView.findViewById(R.id.account_summary_row);
100             tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
101             tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
102             expanderContainer = itemView.findViewById(R.id.account_expander_container);
103             expander = itemView.findViewById(R.id.account_expander);
104             accountExpanderContainer =
105                     itemView.findViewById(R.id.account_row_amounts_expander_container);
106
107             itemView.setOnLongClickListener(this::onItemLongClick);
108             tvAccountName.setOnLongClickListener(this::onItemLongClick);
109             tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
110             expanderContainer.setOnLongClickListener(this::onItemLongClick);
111             expander.setOnLongClickListener(this::onItemLongClick);
112             row.setOnLongClickListener(this::onItemLongClick);
113
114             tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
115             expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
116             expander.setOnClickListener(v -> toggleAccountExpanded());
117             tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
118         }
119         private void toggleAccountExpanded() {
120             if (!mAccount.hasSubAccounts())
121                 return;
122             debug("accounts", "Account expander clicked");
123
124             // make sure we use the same object as the one in the allAccounts list
125             MobileLedgerProfile profile = mAccount.getProfile();
126             if (profile == null) {
127                 return;
128             }
129             try (Locker ignored = profile.lockAccountsForWriting()) {
130                 LedgerAccount realAccount = profile.locateAccount(mAccount.getName());
131                 if (realAccount == null)
132                     return;
133
134                 mAccount = realAccount;
135                 mAccount.toggleExpanded();
136             }
137             expanderContainer.animate()
138                              .rotation(mAccount.isExpanded() ? 0 : 180);
139             profile.updateDisplayedAccounts();
140
141             DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
142                     new Object[]{mAccount.isExpanded(), mAccount.getName(), profile.getUuid()
143                     });
144
145         }
146         private void toggleAmountsExpanded() {
147             if (mAccount.getAmountCount() <= AMOUNT_LIMIT)
148                 return;
149
150             mAccount.toggleAmountsExpanded();
151             if (mAccount.amountsExpanded()) {
152                 tvAccountAmounts.setText(mAccount.getAmountsString());
153                 accountExpanderContainer.setVisibility(View.GONE);
154             }
155             else {
156                 tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
157                 accountExpanderContainer.setVisibility(View.VISIBLE);
158             }
159
160             MobileLedgerProfile profile = mAccount.getProfile();
161             if (profile == null)
162                 return;
163
164             DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
165                     new Object[]{mAccount.amountsExpanded(), mAccount.getName(), profile.getUuid()
166                     });
167
168         }
169         private boolean onItemLongClick(View v) {
170             MainActivity activity = (MainActivity) v.getContext();
171             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
172             final String accountName = mAccount.getName();
173             builder.setTitle(accountName);
174             builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
175                 switch (which) {
176                     case 0:
177                         // show transactions
178                         activity.showAccountTransactions(accountName);
179                         break;
180                     default:
181                         throw new RuntimeException(
182                                 String.format("Unknown menu item id (%d)", which));
183                 }
184                 dialog.dismiss();
185             });
186             builder.show();
187             return true;
188         }
189         public void bindToAccount(LedgerAccount acc) {
190             Context ctx = row.getContext();
191             Resources rm = ctx.getResources();
192             mAccount = acc;
193
194             row.setTag(acc);
195
196             tvAccountName.setText(acc.getShortName());
197
198             ConstraintLayout.LayoutParams lp =
199                     (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
200             lp.setMarginStart(
201                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
202
203             if (acc.hasSubAccounts()) {
204                 expanderContainer.setVisibility(View.VISIBLE);
205                 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
206             }
207             else {
208                 expanderContainer.setVisibility(View.GONE);
209             }
210
211             int amounts = acc.getAmountCount();
212             if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
213                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
214                 accountExpanderContainer.setVisibility(View.VISIBLE);
215             }
216             else {
217                 tvAccountAmounts.setText(acc.getAmountsString());
218                 accountExpanderContainer.setVisibility(View.GONE);
219             }
220         }
221     }
222 }