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.
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.
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/>.
18 package net.ktnx.mobileledger;
20 import android.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.support.annotation.NonNull;
23 import android.support.constraint.ConstraintLayout;
24 import android.support.v7.widget.RecyclerView;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.LinearLayout;
31 import android.widget.TextView;
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;
38 import java.util.List;
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
42 class TransactionListAdapter
43 extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
44 private List<LedgerTransaction> transactions;
46 TransactionListAdapter(List<LedgerTransaction> transactions) {
47 this.transactions = transactions;
50 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
51 LedgerTransaction tr = transactions.get(position);
52 Context ctx = holder.row.getContext();
54 try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
56 holder.tvDescription.setText(tr.getDescription());
57 holder.tvDate.setText(tr.getDate());
60 for (LedgerTransactionAccount acc : tr.getAccounts()) {
61 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
62 TextView accName, accAmount;
64 row = new LinearLayout(ctx);
66 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
67 LinearLayout.LayoutParams.WRAP_CONTENT));
68 row.setGravity(Gravity.CENTER_VERTICAL);
69 row.setOrientation(LinearLayout.HORIZONTAL);
70 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 8);
71 accName = new TextView(ctx);
72 accName.setLayoutParams(
73 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
74 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
75 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
77 accAmount = new TextView(ctx);
78 LinearLayout.LayoutParams llp =
79 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
80 LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
82 accAmount.setLayoutParams(llp);
83 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
84 accAmount.setMinWidth(dp2px(ctx, 60));
85 row.addView(accAmount);
86 holder.tableAccounts.addView(row);
89 accName = (TextView) row.getChildAt(0);
90 accAmount = (TextView) row.getChildAt(1);
92 accName.setText(acc.getShortAccountName());
93 accAmount.setText(acc.toString());
95 if (holder.tableAccounts.getChildCount() > rowIndex) {
97 .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
100 if (position % 2 == 0) {
101 holder.row.setBackgroundColor(Globals.table_row_even_bg);
104 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
111 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
112 Log.d("perf", "onCreateViewHolder called");
113 View row = LayoutInflater.from(parent.getContext())
114 .inflate(R.layout.transaction_list_row, parent, false);
115 return new TransactionRowHolder(row);
119 public int getItemCount() {
120 return transactions.size();
122 class TransactionRowHolder extends RecyclerView.ViewHolder {
123 TextView tvDescription, tvDate;
124 LinearLayout tableAccounts;
125 ConstraintLayout row;
126 public TransactionRowHolder(@NonNull View itemView) {
128 this.row = (ConstraintLayout) itemView;
129 this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
130 this.tvDate = itemView.findViewById(R.id.transaction_row_date);
131 this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);