]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionRowHolder.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionRowHolder.java
1 /*
2  * Copyright © 2021 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.transaction_list;
19
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Typeface;
23 import android.text.Spannable;
24 import android.text.SpannableString;
25 import android.text.style.StyleSpan;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.widget.LinearLayout;
29 import android.widget.TextView;
30
31 import androidx.annotation.ColorInt;
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.databinding.TransactionListRowBinding;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
39 import net.ktnx.mobileledger.model.TransactionListItem;
40 import net.ktnx.mobileledger.utils.Colors;
41 import net.ktnx.mobileledger.utils.Misc;
42
43 class TransactionRowHolder extends TransactionRowHolderBase {
44     private final TransactionListRowBinding b;
45     TransactionListItem.Type lastType;
46     public TransactionRowHolder(@NonNull TransactionListRowBinding binding) {
47         super(binding.getRoot());
48         b = binding;
49     }
50     public void bind(@NonNull TransactionListItem item, @Nullable String boldAccountName) {
51         LedgerTransaction tr = item.getTransaction();
52         b.transactionRowDescription.setText(tr.getDescription());
53         String trComment = Misc.emptyIsNull(tr.getComment());
54         if (trComment == null)
55             b.transactionComment.setVisibility(View.GONE);
56         else {
57             b.transactionComment.setText(trComment);
58             b.transactionComment.setVisibility(View.VISIBLE);
59         }
60
61         if (Misc.emptyIsNull(item.getRunningTotal()) != null) {
62             b.transactionRunningTotal.setText(item.getRunningTotal());
63             b.transactionRunningTotal.setVisibility(View.VISIBLE);
64             b.transactionRunningTotalDivider.setVisibility(View.VISIBLE);
65         }
66         else {
67             b.transactionRunningTotal.setVisibility(View.GONE);
68             b.transactionRunningTotalDivider.setVisibility(View.GONE);
69         }
70
71         int rowIndex = 0;
72         Context ctx = b.getRoot()
73                        .getContext();
74         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
75         for (LedgerTransactionAccount acc : tr.getAccounts()) {
76             LinearLayout row = (LinearLayout) b.transactionRowAccAmounts.getChildAt(rowIndex);
77             if (row == null) {
78                 row = new LinearLayout(ctx);
79                 inflater.inflate(R.layout.transaction_list_row_accounts_table_row, row);
80                 b.transactionRowAccAmounts.addView(row);
81             }
82
83             TextView dummyText = row.findViewById(R.id.dummy_text);
84             TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
85             TextView accComment = row.findViewById(R.id.transaction_list_acc_row_acc_comment);
86             TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
87
88             if ((boldAccountName != null) && acc.getAccountName()
89                                                 .startsWith(boldAccountName))
90             {
91                 accName.setTextColor(Colors.primary);
92                 accAmount.setTextColor(Colors.primary);
93
94                 SpannableString ss = new SpannableString(Misc.addWrapHints(acc.getAccountName()));
95                 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, Misc.addWrapHints(boldAccountName)
96                                                                 .length(),
97                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
98                 accName.setText(ss);
99             }
100             else {
101                 @ColorInt int textColor = dummyText.getTextColors()
102                                                    .getDefaultColor();
103                 accName.setTextColor(textColor);
104                 accAmount.setTextColor(textColor);
105                 accName.setText(Misc.addWrapHints(acc.getAccountName()));
106             }
107
108             String comment = acc.getComment();
109             if (comment != null && !comment.isEmpty()) {
110                 accComment.setText(comment);
111                 accComment.setVisibility(View.VISIBLE);
112             }
113             else {
114                 accComment.setVisibility(View.GONE);
115             }
116             accAmount.setText(acc.toString());
117
118             rowIndex++;
119         }
120
121         if (b.transactionRowAccAmounts.getChildCount() > rowIndex) {
122             b.transactionRowAccAmounts.removeViews(rowIndex,
123                     b.transactionRowAccAmounts.getChildCount() - rowIndex);
124         }
125     }
126 }