]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionRowHolder.java
85d36911a6e772421b0fff015039cc281493cd02
[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 LedgerTransaction tr, @Nullable String boldAccountName) {
54         b.transactionRowDescription.setText(tr.getDescription());
55         String trComment = Misc.emptyIsNull(tr.getComment());
56         if (trComment == null)
57             b.transactionComment.setVisibility(View.GONE);
58         else {
59             b.transactionComment.setText(trComment);
60             b.transactionComment.setVisibility(View.VISIBLE);
61         }
62
63         int rowIndex = 0;
64         Context ctx = b.getRoot()
65                        .getContext();
66         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
67         for (LedgerTransactionAccount acc : tr.getAccounts()) {
68             LinearLayout row = (LinearLayout) b.transactionRowAccAmounts.getChildAt(rowIndex);
69             if (row == null) {
70                 row = new LinearLayout(ctx);
71                 inflater.inflate(R.layout.transaction_list_row_accounts_table_row, row);
72                 b.transactionRowAccAmounts.addView(row);
73             }
74
75             TextView dummyText = row.findViewById(R.id.dummy_text);
76             TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
77             TextView accComment = row.findViewById(R.id.transaction_list_acc_row_acc_comment);
78             TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
79
80             if ((boldAccountName != null) && acc.getAccountName()
81                                                 .startsWith(boldAccountName))
82             {
83                 accName.setTextColor(Colors.secondary);
84                 accAmount.setTextColor(Colors.secondary);
85
86                 SpannableString ss = new SpannableString(acc.getAccountName());
87                 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
88                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
89                 accName.setText(ss);
90             }
91             else {
92                 @ColorInt int textColor = dummyText.getTextColors()
93                                                    .getDefaultColor();
94                 accName.setTextColor(textColor);
95                 accAmount.setTextColor(textColor);
96                 accName.setText(acc.getAccountName());
97             }
98
99             String comment = acc.getComment();
100             if (comment != null && !comment.isEmpty()) {
101                 accComment.setText(comment);
102                 accComment.setVisibility(View.VISIBLE);
103             }
104             else {
105                 accComment.setVisibility(View.GONE);
106             }
107             accAmount.setText(acc.toString());
108
109             rowIndex++;
110         }
111
112         if (b.transactionRowAccAmounts.getChildCount() > rowIndex) {
113             b.transactionRowAccAmounts.removeViews(rowIndex,
114                     b.transactionRowAccAmounts.getChildCount() - rowIndex);
115         }
116     }
117 }