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.AppCompatTextView;
25 import android.support.v7.widget.RecyclerView;
26 import android.util.Log;
27 import android.view.Gravity;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.LinearLayout;
32 import android.widget.TextView;
34 import net.ktnx.mobileledger.model.LedgerTransaction;
35 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
36 import net.ktnx.mobileledger.utils.Globals;
37 import net.ktnx.mobileledger.utils.MLDB;
39 import java.util.List;
41 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
43 class TransactionListAdapter
44 extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
45 private List<LedgerTransaction> transactions;
47 TransactionListAdapter(List<LedgerTransaction> transactions) {
48 this.transactions = transactions;
51 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
52 // in a race when transaction list is reduced, but the model hasn't been notified yet
53 // the view will disappear when the notifications reaches the model, so by simply omitting
54 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
56 if (position >= transactions.size()) return;
58 LedgerTransaction tr = transactions.get(position);
59 Context ctx = holder.row.getContext();
61 try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
63 holder.tvDescription.setText(tr.getDescription());
64 holder.tvDate.setText(tr.getDate());
67 for (LedgerTransactionAccount acc : tr.getAccounts()) {
68 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
69 TextView accName, accAmount;
71 row = new LinearLayout(ctx);
73 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
74 LinearLayout.LayoutParams.WRAP_CONTENT));
75 row.setGravity(Gravity.CENTER_VERTICAL);
76 row.setOrientation(LinearLayout.HORIZONTAL);
77 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
78 accName = new AppCompatTextView(ctx);
79 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
80 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
81 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
83 accAmount = new AppCompatTextView(ctx);
84 LinearLayout.LayoutParams llp =
85 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
86 LinearLayout.LayoutParams.WRAP_CONTENT);
88 accAmount.setLayoutParams(llp);
89 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
90 accAmount.setMinWidth(dp2px(ctx, 60));
91 row.addView(accAmount);
92 holder.tableAccounts.addView(row);
95 accName = (TextView) row.getChildAt(0);
96 accAmount = (TextView) row.getChildAt(1);
98 accName.setText(acc.getShortAccountName());
99 accAmount.setText(acc.toString());
101 if (holder.tableAccounts.getChildCount() > rowIndex) {
103 .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
106 if (position % 2 == 0) {
107 holder.row.setBackgroundColor(Globals.table_row_even_bg);
110 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
117 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
118 Log.d("perf", "onCreateViewHolder called");
119 View row = LayoutInflater.from(parent.getContext())
120 .inflate(R.layout.transaction_list_row, parent, false);
121 return new TransactionRowHolder(row);
125 public int getItemCount() {
126 return transactions.size();
128 class TransactionRowHolder extends RecyclerView.ViewHolder {
129 TextView tvDescription, tvDate;
130 LinearLayout tableAccounts;
131 ConstraintLayout row;
132 public TransactionRowHolder(@NonNull View itemView) {
134 this.row = itemView.findViewById(R.id.transaction_row);
135 this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
136 this.tvDate = itemView.findViewById(R.id.transaction_row_date);
137 this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);