]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
show accounts and amounts in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / TransactionListAdapter.java
1 /*
2  * Copyright © 2018 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger;
19
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.os.Build;
23 import android.support.annotation.NonNull;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.LinearLayout;
29 import android.widget.TableLayout;
30 import android.widget.TextView;
31
32 import net.ktnx.mobileledger.model.LedgerTransaction;
33
34 import java.util.List;
35
36 class TransactionListAdapter
37         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
38     private List<LedgerTransaction> transactions;
39
40     TransactionListAdapter(List<LedgerTransaction> transactions) {
41         this.transactions = transactions;
42     }
43
44     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
45         LedgerTransaction tr = transactions.get(position);
46         Context ctx = holder.row.getContext();
47         Resources rm = ctx.getResources();
48
49         holder.tvDescription.setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
50         TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
51         tbl.removeAllViews();
52         for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
53             LedgerTransactionItem acc = it.next();
54             TableRow row = new TableRow(holder.row.getContext());
55             TextView child = new TextView(ctx);
56             child.setText(acc.getShortAccountName());
57             row.addView(child);
58             child = new TextView(ctx);
59             child.setText(acc.toString());
60             row.addView(child);
61             tbl.addView(row);
62         }
63
64         if (position % 2 == 0) {
65             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
66                     .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
67             else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
68         }
69         else {
70             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
71                     .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
72             else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
73         }
74
75         holder.row.setTag(R.id.POS, position);
76     }
77
78     @NonNull
79     @Override
80     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
81         View row = LayoutInflater.from(parent.getContext())
82                 .inflate(R.layout.transaction_list_row, parent, false);
83         return new TransactionRowHolder(row);
84     }
85
86     @Override
87     public int getItemCount() {
88         return transactions.size();
89     }
90     class TransactionRowHolder extends RecyclerView.ViewHolder {
91         TextView tvDescription;
92         TableLayout tableAccounts;
93         LinearLayout row;
94         public TransactionRowHolder(@NonNull View itemView) {
95             super(itemView);
96             this.row = (LinearLayout) itemView;
97             this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
98             this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);
99         }
100     }
101 }