]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionRowHolder.java
more pronounced day/month delimiters in the transaction list
[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(Misc.addWrapHints(acc.getAccountName()));
98                 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, Misc.addWrapHints(boldAccountName)
99                                                                 .length(),
100                         Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
101                 accName.setText(ss);
102             }
103             else {
104                 @ColorInt int textColor = dummyText.getTextColors()
105                                                    .getDefaultColor();
106                 accName.setTextColor(textColor);
107                 accAmount.setTextColor(textColor);
108                 accName.setText(Misc.addWrapHints(acc.getAccountName()));
109             }
110
111             String comment = acc.getComment();
112             if (comment != null && !comment.isEmpty()) {
113                 accComment.setText(comment);
114                 accComment.setVisibility(View.VISIBLE);
115             }
116             else {
117                 accComment.setVisibility(View.GONE);
118             }
119             accAmount.setText(acc.toString());
120
121             rowIndex++;
122         }
123
124         if (b.transactionRowAccAmounts.getChildCount() > rowIndex) {
125             b.transactionRowAccAmounts.removeViews(rowIndex,
126                     b.transactionRowAccAmounts.getChildCount() - rowIndex);
127         }
128     }
129 }