make "manually" added account rows look the same as those coming from the layout
[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.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;
33
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;
38
39 import java.util.List;
40
41 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
42
43 class TransactionListAdapter
44         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
45     private List<LedgerTransaction> transactions;
46
47     TransactionListAdapter(List<LedgerTransaction> transactions) {
48         this.transactions = transactions;
49     }
50
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
55         // a bit longer
56         if (position >= transactions.size()) return;
57
58         LedgerTransaction tr = transactions.get(position);
59         Context ctx = holder.row.getContext();
60
61         try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
62             tr.loadData(db);
63             holder.tvDescription.setText(tr.getDescription());
64             holder.tvDate.setText(tr.getDate());
65
66             int rowIndex = 0;
67             for (LedgerTransactionAccount acc : tr.getAccounts()) {
68                 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
69                 TextView accName, accAmount;
70                 if (row == null) {
71                     row = new LinearLayout(ctx);
72                     row.setLayoutParams(
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);
82                     row.addView(accName);
83                     accAmount = new AppCompatTextView(ctx);
84                     LinearLayout.LayoutParams llp =
85                             new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
86                                     LinearLayout.LayoutParams.WRAP_CONTENT);
87                     llp.setMarginEnd(0);
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);
93                 }
94                 else {
95                     accName = (TextView) row.getChildAt(0);
96                     accAmount = (TextView) row.getChildAt(1);
97                 }
98                 accName.setText(acc.getShortAccountName());
99                 accAmount.setText(acc.toString());
100             }
101             if (holder.tableAccounts.getChildCount() > rowIndex) {
102                 holder.tableAccounts
103                         .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
104             }
105
106             if (position % 2 == 0) {
107                 holder.row.setBackgroundColor(Globals.table_row_even_bg);
108             }
109             else {
110                 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
111             }
112         }
113     }
114
115     @NonNull
116     @Override
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);
122     }
123
124     @Override
125     public int getItemCount() {
126         return transactions.size();
127     }
128     class TransactionRowHolder extends RecyclerView.ViewHolder {
129         TextView tvDescription, tvDate;
130         LinearLayout tableAccounts;
131         ConstraintLayout row;
132         public TransactionRowHolder(@NonNull View itemView) {
133             super(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);
138         }
139     }
140 }