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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
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;
31 import androidx.annotation.ColorInt;
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
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;
43 import java.util.Observer;
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());
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);
60 b.transactionComment.setText(trComment);
61 b.transactionComment.setVisibility(View.VISIBLE);
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);
70 b.transactionRunningTotal.setVisibility(View.GONE);
71 b.transactionRunningTotalDivider.setVisibility(View.GONE);
75 Context ctx = b.getRoot()
77 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
78 for (LedgerTransactionAccount acc : tr.getAccounts()) {
79 LinearLayout row = (LinearLayout) b.transactionRowAccAmounts.getChildAt(rowIndex);
81 row = new LinearLayout(ctx);
82 inflater.inflate(R.layout.transaction_list_row_accounts_table_row, row);
83 b.transactionRowAccAmounts.addView(row);
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);
91 if ((boldAccountName != null) && acc.getAccountName()
92 .startsWith(boldAccountName))
94 accName.setTextColor(Colors.primary);
95 accAmount.setTextColor(Colors.primary);
97 SpannableString ss = new SpannableString(acc.getAccountName());
98 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
99 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
103 @ColorInt int textColor = dummyText.getTextColors()
105 accName.setTextColor(textColor);
106 accAmount.setTextColor(textColor);
107 accName.setText(acc.getAccountName());
110 String comment = acc.getComment();
111 if (comment != null && !comment.isEmpty()) {
112 accComment.setText(comment);
113 accComment.setVisibility(View.VISIBLE);
116 accComment.setVisibility(View.GONE);
118 accAmount.setText(acc.toString());
123 if (b.transactionRowAccAmounts.getChildCount() > rowIndex) {
124 b.transactionRowAccAmounts.removeViews(rowIndex,
125 b.transactionRowAccAmounts.getChildCount() - rowIndex);