]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
57de4c0c0666765367ad3677cf1e81e58087e58b
[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.database.sqlite.SQLiteDatabase;
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.TableRow;
31 import android.widget.TextView;
32
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
35 import net.ktnx.mobileledger.utils.Globals;
36 import net.ktnx.mobileledger.utils.MLDB;
37
38 import java.util.Iterator;
39 import java.util.List;
40
41 class TransactionListAdapter
42         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
43     private List<LedgerTransaction> transactions;
44
45     TransactionListAdapter(List<LedgerTransaction> transactions) {
46         this.transactions = transactions;
47     }
48
49     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
50         LedgerTransaction tr = transactions.get(position);
51         Context ctx = holder.row.getContext();
52
53         try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
54             tr.loadData(db);
55
56             holder.tvDescription
57                     .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
58             TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
59             tbl.removeAllViews();
60             for (LedgerTransactionAccount acc : tr.getAccounts()) {
61                 TableRow row = new TableRow(holder.row.getContext());
62                 TextView child = new TextView(ctx);
63                 child.setText(acc.getShortAccountName());
64                 row.addView(child);
65                 child = new TextView(ctx);
66                 child.setText(acc.toString());
67                 row.addView(child);
68                 tbl.addView(row);
69             }
70
71             if (position % 2 == 0) {
72                 holder.row.setBackgroundColor(Globals.table_row_even_bg);
73             }
74             else {
75                 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
76             }
77
78             holder.row.setTag(R.id.POS, position);
79         }
80     }
81
82     @NonNull
83     @Override
84     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
85         View row = LayoutInflater.from(parent.getContext())
86                 .inflate(R.layout.transaction_list_row, parent, false);
87         return new TransactionRowHolder(row);
88     }
89
90     @Override
91     public int getItemCount() {
92         return transactions.size();
93     }
94     class TransactionRowHolder extends RecyclerView.ViewHolder {
95         TextView tvDescription;
96         TableLayout tableAccounts;
97         LinearLayout row;
98         public TransactionRowHolder(@NonNull View itemView) {
99             super(itemView);
100             this.row = (LinearLayout) itemView;
101             this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
102             this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);
103         }
104     }
105 }