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