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.ui.transaction_list.TransactionListViewModel;
37 import net.ktnx.mobileledger.utils.Globals;
38 import net.ktnx.mobileledger.utils.MLDB;
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
42 class TransactionListAdapter
43 extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
44 TransactionListViewModel model;
45 public TransactionListAdapter(TransactionListViewModel model) {
48 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
49 LedgerTransaction tr = model.getTransaction(position);
50 // in a race when transaction list is reduced, but the model hasn't been notified yet
51 // the view will disappear when the notifications reaches the model, so by simply omitting
52 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
54 if (tr == null) return;
56 Context ctx = holder.row.getContext();
58 try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
60 holder.tvDescription.setText(tr.getDescription());
61 holder.tvDate.setText(tr.getDate());
64 for (LedgerTransactionAccount acc : tr.getAccounts()) {
65 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
66 TextView accName, accAmount;
68 row = new LinearLayout(ctx);
70 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
71 LinearLayout.LayoutParams.WRAP_CONTENT));
72 row.setGravity(Gravity.CENTER_VERTICAL);
73 row.setOrientation(LinearLayout.HORIZONTAL);
74 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
75 accName = new AppCompatTextView(ctx);
76 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
77 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
78 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
80 accAmount = new AppCompatTextView(ctx);
81 LinearLayout.LayoutParams llp =
82 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
83 LinearLayout.LayoutParams.WRAP_CONTENT);
85 accAmount.setLayoutParams(llp);
86 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
87 accAmount.setMinWidth(dp2px(ctx, 60));
88 row.addView(accAmount);
89 holder.tableAccounts.addView(row);
92 accName = (TextView) row.getChildAt(0);
93 accAmount = (TextView) row.getChildAt(1);
95 accName.setText(acc.getAccountName());
96 accAmount.setText(acc.toString());
98 if (holder.tableAccounts.getChildCount() > rowIndex) {
100 .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
103 if (position % 2 == 0) {
104 holder.row.setBackgroundColor(Globals.table_row_even_bg);
107 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
110 Log.d("transactions", String.format("Filled position %d", position));
116 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
117 Log.d("perf", "onCreateViewHolder called");
118 View row = LayoutInflater.from(parent.getContext())
119 .inflate(R.layout.transaction_list_row, parent, false);
120 return new TransactionRowHolder(row);
124 public int getItemCount()
126 return model.getTransactionCount();
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);