]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
7983aa5b9cd580b13e4f4b367647347a5822bf3d
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / 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.ui.transaction_list;
19
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;
34
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;
40
41 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
42
43 public class TransactionListAdapter
44         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
45     TransactionListViewModel model;
46     private String boldAccountName;
47     public TransactionListAdapter(TransactionListViewModel model) {
48         this.model = model;
49     }
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
55         // a bit longer
56         if (tr == null) return;
57
58         Context ctx = holder.row.getContext();
59
60         try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
61             tr.loadData(db);
62             holder.tvDescription.setText(tr.getDescription());
63             holder.tvDate.setText(tr.getDate());
64
65             int rowIndex = 0;
66             for (LedgerTransactionAccount acc : tr.getAccounts()) {
67                 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
68                 TextView accName, accAmount;
69                 if (row == null) {
70                     row = new LinearLayout(ctx);
71                     row.setLayoutParams(
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,
80                                     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.getAccountName());
99                 accAmount.setText(acc.toString());
100
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);
106                 }
107                 else {
108                     accName.setTypeface(null, Typeface.NORMAL);
109                     accAmount.setTypeface(null, Typeface.NORMAL);
110                     accName.setTextColor(Globals.defaultTextColor);
111                     accAmount.setTextColor(Globals.defaultTextColor);
112                 }
113
114             }
115             if (holder.tableAccounts.getChildCount() > rowIndex) {
116                 holder.tableAccounts
117                         .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
118             }
119
120             if (position % 2 == 0) {
121                 holder.row.setBackgroundColor(Globals.table_row_even_bg);
122             }
123             else {
124                 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
125             }
126
127             Log.d("transactions", String.format("Filled position %d", position));
128         }
129     }
130
131     @NonNull
132     @Override
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);
138     }
139
140     @Override
141     public int getItemCount() {
142         return model.getTransactionCount();
143     }
144     public void setBoldAccountName(String boldAccountName) {
145         this.boldAccountName = boldAccountName;
146     }
147     public void resetBoldAccountName() {
148         this.boldAccountName = null;
149     }
150     class TransactionRowHolder extends RecyclerView.ViewHolder {
151         TextView tvDescription, tvDate;
152         LinearLayout tableAccounts;
153         ConstraintLayout row;
154         public TransactionRowHolder(@NonNull View itemView) {
155             super(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);
160         }
161     }
162 }