2 * Copyright © 2019 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.os.AsyncTask;
24 import android.support.annotation.NonNull;
25 import android.support.v7.widget.AppCompatTextView;
26 import android.support.v7.widget.RecyclerView;
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.R;
35 import net.ktnx.mobileledger.model.LedgerTransaction;
36 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
37 import net.ktnx.mobileledger.model.TransactionListItem;
38 import net.ktnx.mobileledger.utils.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
41 import java.text.DateFormat;
42 import java.util.Date;
44 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
46 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
47 private String boldAccountName;
48 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
49 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
51 if (item.getType() == TransactionListItem.Type.TRANSACTION) {
52 holder.vTransaction.setVisibility(View.VISIBLE);
53 holder.vDelimiter.setVisibility(View.GONE);
54 LedgerTransaction tr = item.getTransaction();
55 // in a race when transaction value is reduced, but the model hasn't been notified yet
56 // the view will disappear when the notifications reaches the model, so by simply omitting
57 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
59 if (tr == null) return;
61 LedgerTransaction previous = null;
62 TransactionListItem previousItem = null;
64 previousItem = TransactionListViewModel.getTransactionListItem(position - 1);
66 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
67 // tr.getAccounts().size()));
69 TransactionLoader loader = new TransactionLoader();
71 new TransactionLoaderParams(tr, previous, holder, position, boldAccountName));
73 // WORKAROUND what seems to be a bug in CardHolder somewhere
74 // when a view that was previously holding a delimiter is re-purposed
75 // occasionally it stays too short (not high enough)
76 holder.vTransaction.measure(View.MeasureSpec
77 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
78 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
81 Date date = item.getDate();
82 holder.vTransaction.setVisibility(View.GONE);
83 holder.vDelimiter.setVisibility(View.VISIBLE);
84 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
85 holder.tvDelimiterMonth
86 .setText(item.isMonthShown() ? Globals.monthNames[date.getMonth()] : "");
92 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
93 // Log.d("perf", "onCreateViewHolder called");
94 View row = LayoutInflater.from(parent.getContext())
95 .inflate(R.layout.transaction_list_row, parent, false);
96 return new TransactionRowHolder(row);
100 public int getItemCount() {
101 return TransactionListViewModel.getTransactionCount();
103 public void setBoldAccountName(String boldAccountName) {
104 this.boldAccountName = boldAccountName;
106 public void resetBoldAccountName() {
107 this.boldAccountName = null;
110 enum LoaderStep {HEAD, ACCOUNTS, DONE}
112 private static class TransactionLoader
113 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
115 protected Void doInBackground(TransactionLoaderParams... p) {
116 LedgerTransaction tr = p[0].transaction;
117 LedgerTransaction previous = p[0].previousTransaction;
119 SQLiteDatabase db = MLDB.getReadableDatabase();
123 if (previous == null) showDate = true;
125 previous.loadData(db);
126 showDate = !previous.getDate().equals(tr.getDate());
128 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, showDate));
131 for (LedgerTransactionAccount acc : tr.getAccounts()) {
132 // Log.d(c.getAccountName(), acc.getAmount()));
133 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
134 p[0].boldAccountName));
137 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
142 protected void onProgressUpdate(TransactionLoaderStep... values) {
143 super.onProgressUpdate(values);
144 TransactionLoaderStep step = values[0];
145 TransactionRowHolder holder = step.getHolder();
147 switch (step.getStep()) {
149 holder.tvDescription.setText(step.getTransaction().getDescription());
151 if (step.getPosition() % 2 == 0) {
152 holder.row.setBackgroundColor(Globals.tableRowEvenBG);
155 holder.row.setBackgroundColor(Globals.tableRowOddBG);
160 int rowIndex = step.getAccountPosition();
161 Context ctx = holder.row.getContext();
162 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
163 TextView accName, accAmount;
165 row = new LinearLayout(ctx);
166 row.setLayoutParams(new LinearLayout.LayoutParams(
167 LinearLayout.LayoutParams.MATCH_PARENT,
168 LinearLayout.LayoutParams.WRAP_CONTENT));
169 row.setGravity(Gravity.CENTER_VERTICAL);
170 row.setOrientation(LinearLayout.HORIZONTAL);
171 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
172 accName = new AppCompatTextView(ctx);
173 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
174 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
175 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
176 row.addView(accName);
177 accAmount = new AppCompatTextView(ctx);
178 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
179 LinearLayout.LayoutParams.WRAP_CONTENT,
180 LinearLayout.LayoutParams.WRAP_CONTENT);
182 accAmount.setLayoutParams(llp);
183 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
184 accAmount.setMinWidth(dp2px(ctx, 60));
185 row.addView(accAmount);
186 holder.tableAccounts.addView(row);
189 accName = (TextView) row.getChildAt(0);
190 accAmount = (TextView) row.getChildAt(1);
192 LedgerTransactionAccount acc = step.getAccount();
194 accName.setText(acc.getAccountName());
195 accAmount.setText(acc.toString());
197 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
198 // acc.getAccountName(), acc.getAmount()));
200 String boldAccountName = step.getBoldAccountName();
201 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
202 accName.setTypeface(null, Typeface.BOLD);
203 accAmount.setTypeface(null, Typeface.BOLD);
204 accName.setTextColor(Globals.primaryDark);
205 accAmount.setTextColor(Globals.primaryDark);
208 accName.setTypeface(null, Typeface.NORMAL);
209 accAmount.setTypeface(null, Typeface.NORMAL);
210 accName.setTextColor(Globals.defaultTextColor);
211 accAmount.setTextColor(Globals.defaultTextColor);
216 int accCount = step.getAccountCount();
217 if (holder.tableAccounts.getChildCount() > accCount) {
218 holder.tableAccounts.removeViews(accCount,
219 holder.tableAccounts.getChildCount() - accCount);
222 // Log.d("transactions",
223 // String.format("Position %d fill done", step.getPosition()));
228 private class TransactionLoaderParams {
229 LedgerTransaction transaction, previousTransaction;
230 TransactionRowHolder holder;
232 String boldAccountName;
233 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder, int position, String boldAccountName) {
234 this(transaction, null, holder, position, boldAccountName);
236 TransactionLoaderParams(LedgerTransaction transaction,
237 LedgerTransaction previousTransaction, TransactionRowHolder holder,
238 int position, String boldAccountName) {
239 this.transaction = transaction;
240 this.previousTransaction = previousTransaction;
241 this.holder = holder;
242 this.position = position;
243 this.boldAccountName = boldAccountName;