]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
drop long unused methods
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryAdapter.java
1 /*
2  * Copyright © 2019 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.graphics.Typeface;
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.RecyclerView;
34
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerAccount;
38 import net.ktnx.mobileledger.ui.activity.MainActivity;
39 import net.ktnx.mobileledger.utils.LockHolder;
40
41 public class AccountSummaryAdapter
42         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
43     public static final int AMOUNT_LIMIT = 3;
44
45     AccountSummaryAdapter() { }
46
47     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
48         try (LockHolder lh = Data.accounts.lockForReading()) {
49             if (position < Data.accounts.size()) {
50                 LedgerAccount acc = Data.accounts.get(position);
51                 Context ctx = holder.row.getContext();
52                 Resources rm = ctx.getResources();
53
54                 holder.row.setTag(acc);
55                 holder.row.setVisibility(View.VISIBLE);
56                 holder.vTrailer.setVisibility(View.GONE);
57                 holder.tvAccountName.setText(acc.getShortName());
58                 ConstraintLayout.LayoutParams lp =
59                         (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
60                 lp.setMarginStart(
61                         acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
62                 holder.expanderContainer.setVisibility(
63                         acc.hasSubAccounts() ? View.VISIBLE : View.GONE);
64                 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
65                 int amounts = acc.getAmountCount();
66                 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
67                     holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
68                     holder.accountExpanderContainer.setVisibility(View.VISIBLE);
69                 }
70                 else {
71                     holder.tvAccountAmounts.setText(acc.getAmountsString());
72                     holder.accountExpanderContainer.setVisibility(View.GONE);
73                 }
74
75                 if (acc.isHiddenByStar()) {
76                     holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
77                     holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
78                 }
79                 else {
80                     holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
81                     holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
82                 }
83
84                 holder.row.setTag(R.id.POS, position);
85             }
86             else {
87                 holder.vTrailer.setVisibility(View.VISIBLE);
88                 holder.row.setVisibility(View.GONE);
89             }
90         }
91     }
92
93     @NonNull
94     @Override
95     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
96         View row = LayoutInflater.from(parent.getContext())
97                                  .inflate(R.layout.account_summary_row, parent, false);
98         return new LedgerRowHolder(row);
99     }
100
101     @Override
102     public int getItemCount() {
103         return Data.accounts.size() + (Data.profile.getValue()
104                                                    .isPostingPermitted() ? 1 : 0);
105     }
106     static class LedgerRowHolder extends RecyclerView.ViewHolder {
107         TextView tvAccountName, tvAccountAmounts;
108         ConstraintLayout row;
109         View vTrailer;
110         View expanderContainer;
111         ImageView expander;
112         View accountExpanderContainer;
113         public LedgerRowHolder(@NonNull View itemView) {
114             super(itemView);
115             this.row = itemView.findViewById(R.id.account_summary_row);
116             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
117             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
118             this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
119             this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
120             this.expander = itemView.findViewById(R.id.account_expander);
121             this.accountExpanderContainer =
122                     itemView.findViewById(R.id.account_row_amounts_expander_container);
123
124             expanderContainer.addOnLayoutChangeListener(
125                     (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
126                         int w = right - left;
127                         int h = bottom - top;
128                         if (h > w) {
129                             int p = (h - w) / 2;
130                             v.setPadding(0, p, 0, p);
131                         }
132                         else v.setPadding(0, 0, 0, 0);
133                     });
134
135             itemView.setOnLongClickListener(this::onItemLongClick);
136             tvAccountName.setOnLongClickListener(this::onItemLongClick);
137             tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
138             expanderContainer.setOnLongClickListener(this::onItemLongClick);
139             expander.setOnLongClickListener(this::onItemLongClick);
140             row.setOnLongClickListener(this::onItemLongClick);
141         }
142         private boolean onItemLongClick(View v) {
143             MainActivity activity = (MainActivity) v.getContext();
144             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
145             View row;
146             int id = v.getId();
147             switch (id) {
148                 case R.id.account_summary_row:
149                     row = v;
150                     break;
151                 case R.id.account_row_acc_amounts:
152                 case R.id.account_row_amounts_expander_container:
153                     row = (View) v.getParent();
154                     break;
155                 case R.id.account_row_acc_name:
156                 case R.id.account_expander_container:
157                     row = (View) v.getParent()
158                                   .getParent();
159                     break;
160                 case R.id.account_expander:
161                     row = (View) v.getParent()
162                                   .getParent()
163                                   .getParent();
164                     break;
165                 default:
166                     Log.e("error",
167                             String.format("Don't know how to handle long click on id %d", id));
168                     return false;
169             }
170             LedgerAccount acc = (LedgerAccount) row.getTag();
171             builder.setTitle(acc.getName());
172             builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
173                 switch (which) {
174                     case 0:
175                         // show transactions
176                         activity.showAccountTransactions(acc);
177                         break;
178                 }
179                 dialog.dismiss();
180             });
181             builder.show();
182             return true;
183         }
184     }
185 }