]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
b82e84ca59188cb16989e0c80c1979dc36ac3d85
[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.os.Build;
24 import android.support.annotation.NonNull;
25 import android.support.v7.widget.RecyclerView;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.LinearLayout;
30 import android.widget.TableLayout;
31 import android.widget.TableRow;
32 import android.widget.TextView;
33
34 import net.ktnx.mobileledger.model.LedgerTransaction;
35 import net.ktnx.mobileledger.model.LedgerTransactionItem;
36 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
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         Resources rm = ctx.getResources();
53         try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
54             try (SQLiteDatabase db = dbh.getReadableDatabase()) {
55                 tr.loadData(db);
56
57                 holder.tvDescription
58                         .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
59                 TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
60                 tbl.removeAllViews();
61                 for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
62                     LedgerTransactionItem acc = it.next();
63                     TableRow row = new TableRow(holder.row.getContext());
64                     TextView child = new TextView(ctx);
65                     child.setText(acc.getShortAccountName());
66                     row.addView(child);
67                     child = new TextView(ctx);
68                     child.setText(acc.toString());
69                     row.addView(child);
70                     tbl.addView(row);
71                 }
72
73                 if (position % 2 == 0) {
74                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
75                             .setBackgroundColor(
76                                     rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
77                     else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
78                 }
79                 else {
80                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
81                             .setBackgroundColor(
82                                     rm.getColor(R.color.drawer_background, ctx.getTheme()));
83                     else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
84                 }
85
86                 holder.row.setTag(R.id.POS, position);
87             }
88         }
89     }
90
91     @NonNull
92     @Override
93     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
94         View row = LayoutInflater.from(parent.getContext())
95                 .inflate(R.layout.transaction_list_row, parent, false);
96         return new TransactionRowHolder(row);
97     }
98
99     @Override
100     public int getItemCount() {
101         return transactions.size();
102     }
103     class TransactionRowHolder extends RecyclerView.ViewHolder {
104         TextView tvDescription;
105         TableLayout tableAccounts;
106         LinearLayout row;
107         public TransactionRowHolder(@NonNull View itemView) {
108             super(itemView);
109             this.row = (LinearLayout) itemView;
110             this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
111             this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);
112         }
113     }
114 }