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.ui.transaction_list;
20 import android.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.graphics.Typeface;
23 import android.support.annotation.NonNull;
24 import android.support.constraint.ConstraintLayout;
25 import android.support.v7.widget.AppCompatTextView;
26 import android.support.v7.widget.RecyclerView;
27 import android.util.Log;
28 import android.view.Gravity;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.utils.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
41 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
43 public class TransactionListAdapter
44 extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
45 TransactionListViewModel model;
46 private String boldAccountName;
47 public TransactionListAdapter(TransactionListViewModel model) {
50 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
51 LedgerTransaction tr = model.getTransaction(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 (tr == null) return;
58 Context ctx = holder.row.getContext();
60 try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
62 holder.tvDescription.setText(tr.getDescription());
63 holder.tvDate.setText(tr.getDate());
66 for (LedgerTransactionAccount acc : tr.getAccounts()) {
67 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
68 TextView accName, accAmount;
70 row = new LinearLayout(ctx);
72 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
73 LinearLayout.LayoutParams.WRAP_CONTENT));
74 row.setGravity(Gravity.CENTER_VERTICAL);
75 row.setOrientation(LinearLayout.HORIZONTAL);
76 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
77 accName = new AppCompatTextView(ctx);
78 accName.setLayoutParams(
79 new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,
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.getAccountName());
99 accAmount.setText(acc.toString());
101 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
102 accName.setTypeface(null, Typeface.BOLD);
103 accAmount.setTypeface(null, Typeface.BOLD);
104 accName.setTextColor(Globals.primaryDark);
105 accAmount.setTextColor(Globals.primaryDark);
108 accName.setTypeface(null, Typeface.NORMAL);
109 accAmount.setTypeface(null, Typeface.NORMAL);
110 accName.setTextColor(Globals.defaultTextColor);
111 accAmount.setTextColor(Globals.defaultTextColor);
115 if (holder.tableAccounts.getChildCount() > rowIndex) {
117 .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
120 if (position % 2 == 0) {
121 holder.row.setBackgroundColor(Globals.table_row_even_bg);
124 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
127 Log.d("transactions", String.format("Filled position %d", position));
133 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
134 Log.d("perf", "onCreateViewHolder called");
135 View row = LayoutInflater.from(parent.getContext())
136 .inflate(R.layout.transaction_list_row, parent, false);
137 return new TransactionRowHolder(row);
141 public int getItemCount() {
142 return model.getTransactionCount();
144 public void setBoldAccountName(String boldAccountName) {
145 this.boldAccountName = boldAccountName;
147 public void resetBoldAccountName() {
148 this.boldAccountName = null;
150 class TransactionRowHolder extends RecyclerView.ViewHolder {
151 TextView tvDescription, tvDate;
152 LinearLayout tableAccounts;
153 ConstraintLayout row;
154 public TransactionRowHolder(@NonNull View itemView) {
156 this.row = itemView.findViewById(R.id.transaction_row);
157 this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
158 this.tvDate = itemView.findViewById(R.id.transaction_row_date);
159 this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);