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 if (item.isMonthShown()) {
86 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
87 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
88 holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
91 holder.tvDelimiterMonth.setVisibility(View.GONE);
92 holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
99 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
100 // Log.d("perf", "onCreateViewHolder called");
101 View row = LayoutInflater.from(parent.getContext())
102 .inflate(R.layout.transaction_list_row, parent, false);
103 return new TransactionRowHolder(row);
107 public int getItemCount() {
108 return TransactionListViewModel.getTransactionCount();
110 public void setBoldAccountName(String boldAccountName) {
111 this.boldAccountName = boldAccountName;
113 public void resetBoldAccountName() {
114 this.boldAccountName = null;
117 enum LoaderStep {HEAD, ACCOUNTS, DONE}
119 private static class TransactionLoader
120 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
122 protected Void doInBackground(TransactionLoaderParams... p) {
123 LedgerTransaction tr = p[0].transaction;
124 LedgerTransaction previous = p[0].previousTransaction;
126 SQLiteDatabase db = MLDB.getReadableDatabase();
130 if (previous == null) showDate = true;
132 previous.loadData(db);
133 showDate = !previous.getDate().equals(tr.getDate());
135 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, showDate));
138 for (LedgerTransactionAccount acc : tr.getAccounts()) {
139 // Log.d(c.getAccountName(), acc.getAmount()));
140 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
141 p[0].boldAccountName));
144 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
149 protected void onProgressUpdate(TransactionLoaderStep... values) {
150 super.onProgressUpdate(values);
151 TransactionLoaderStep step = values[0];
152 TransactionRowHolder holder = step.getHolder();
154 switch (step.getStep()) {
156 holder.tvDescription.setText(step.getTransaction().getDescription());
158 if (step.getPosition() % 2 == 0) {
159 holder.row.setBackgroundColor(Globals.tableRowEvenBG);
162 holder.row.setBackgroundColor(Globals.tableRowOddBG);
167 int rowIndex = step.getAccountPosition();
168 Context ctx = holder.row.getContext();
169 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
170 TextView accName, accAmount;
172 row = new LinearLayout(ctx);
173 row.setLayoutParams(new LinearLayout.LayoutParams(
174 LinearLayout.LayoutParams.MATCH_PARENT,
175 LinearLayout.LayoutParams.WRAP_CONTENT));
176 row.setGravity(Gravity.CENTER_VERTICAL);
177 row.setOrientation(LinearLayout.HORIZONTAL);
178 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
179 accName = new AppCompatTextView(ctx);
180 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
181 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
182 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
183 row.addView(accName);
184 accAmount = new AppCompatTextView(ctx);
185 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
186 LinearLayout.LayoutParams.WRAP_CONTENT,
187 LinearLayout.LayoutParams.WRAP_CONTENT);
189 accAmount.setLayoutParams(llp);
190 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
191 accAmount.setMinWidth(dp2px(ctx, 60));
192 row.addView(accAmount);
193 holder.tableAccounts.addView(row);
196 accName = (TextView) row.getChildAt(0);
197 accAmount = (TextView) row.getChildAt(1);
199 LedgerTransactionAccount acc = step.getAccount();
201 accName.setText(acc.getAccountName());
202 accAmount.setText(acc.toString());
204 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
205 // acc.getAccountName(), acc.getAmount()));
207 String boldAccountName = step.getBoldAccountName();
208 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
209 accName.setTypeface(null, Typeface.BOLD);
210 accAmount.setTypeface(null, Typeface.BOLD);
211 accName.setTextColor(Globals.primaryDark);
212 accAmount.setTextColor(Globals.primaryDark);
215 accName.setTypeface(null, Typeface.NORMAL);
216 accAmount.setTypeface(null, Typeface.NORMAL);
217 accName.setTextColor(Globals.defaultTextColor);
218 accAmount.setTextColor(Globals.defaultTextColor);
223 int accCount = step.getAccountCount();
224 if (holder.tableAccounts.getChildCount() > accCount) {
225 holder.tableAccounts.removeViews(accCount,
226 holder.tableAccounts.getChildCount() - accCount);
229 // Log.d("transactions",
230 // String.format("Position %d fill done", step.getPosition()));
235 private class TransactionLoaderParams {
236 LedgerTransaction transaction, previousTransaction;
237 TransactionRowHolder holder;
239 String boldAccountName;
240 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder, int position, String boldAccountName) {
241 this(transaction, null, holder, position, boldAccountName);
243 TransactionLoaderParams(LedgerTransaction transaction,
244 LedgerTransaction previousTransaction, TransactionRowHolder holder,
245 int position, String boldAccountName) {
246 this.transaction = transaction;
247 this.previousTransaction = previousTransaction;
248 this.holder = holder;
249 this.position = position;
250 this.boldAccountName = boldAccountName;