]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
let the model hold the (single) transaction list
[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.ui.transaction_list.TransactionListViewModel;
37 import net.ktnx.mobileledger.utils.Globals;
38 import net.ktnx.mobileledger.utils.MLDB;
39
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
41
42 class TransactionListAdapter
43         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
44     TransactionListViewModel model;
45     public TransactionListAdapter(TransactionListViewModel model) {
46         this.model = model;
47     }
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
53         // a bit longer
54         if (tr == null) return;
55
56         Context ctx = holder.row.getContext();
57
58         try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
59             tr.loadData(db);
60             holder.tvDescription.setText(tr.getDescription());
61             holder.tvDate.setText(tr.getDate());
62
63             int rowIndex = 0;
64             for (LedgerTransactionAccount acc : tr.getAccounts()) {
65                 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
66                 TextView accName, accAmount;
67                 if (row == null) {
68                     row = new LinearLayout(ctx);
69                     row.setLayoutParams(
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);
79                     row.addView(accName);
80                     accAmount = new AppCompatTextView(ctx);
81                     LinearLayout.LayoutParams llp =
82                             new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
83                                     LinearLayout.LayoutParams.WRAP_CONTENT);
84                     llp.setMarginEnd(0);
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);
90                 }
91                 else {
92                     accName = (TextView) row.getChildAt(0);
93                     accAmount = (TextView) row.getChildAt(1);
94                 }
95                 accName.setText(acc.getAccountName());
96                 accAmount.setText(acc.toString());
97             }
98             if (holder.tableAccounts.getChildCount() > rowIndex) {
99                 holder.tableAccounts
100                         .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
101             }
102
103             if (position % 2 == 0) {
104                 holder.row.setBackgroundColor(Globals.table_row_even_bg);
105             }
106             else {
107                 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
108             }
109
110             Log.d("transactions", String.format("Filled position %d", position));
111         }
112     }
113
114     @NonNull
115     @Override
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);
121     }
122
123     @Override
124     public int getItemCount()
125     {
126         return model.getTransactionCount();
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 }