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 // in a race when transaction value is reduced, but the model hasn't been notified yet
52 // the view will disappear when the notifications reaches the model, so by simply omitting
53 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
55 if (item == null) return;
57 if (item.getType() == TransactionListItem.Type.TRANSACTION) {
58 holder.vTransaction.setVisibility(View.VISIBLE);
59 holder.vDelimiter.setVisibility(View.GONE);
60 LedgerTransaction tr = item.getTransaction();
62 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
63 // tr.getAccounts().size()));
65 TransactionLoader loader = new TransactionLoader();
66 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName));
68 // WORKAROUND what seems to be a bug in CardHolder somewhere
69 // when a view that was previously holding a delimiter is re-purposed
70 // occasionally it stays too short (not high enough)
71 holder.vTransaction.measure(View.MeasureSpec
72 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
73 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
76 Date date = item.getDate();
77 holder.vTransaction.setVisibility(View.GONE);
78 holder.vDelimiter.setVisibility(View.VISIBLE);
79 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
80 if (item.isMonthShown()) {
81 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
82 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
83 holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
86 holder.tvDelimiterMonth.setVisibility(View.GONE);
87 holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
94 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
95 // Log.d("perf", "onCreateViewHolder called");
96 View row = LayoutInflater.from(parent.getContext())
97 .inflate(R.layout.transaction_list_row, parent, false);
98 return new TransactionRowHolder(row);
102 public int getItemCount() {
103 return TransactionListViewModel.getTransactionCount();
105 public void setBoldAccountName(String boldAccountName) {
106 this.boldAccountName = boldAccountName;
108 public void resetBoldAccountName() {
109 this.boldAccountName = null;
112 enum LoaderStep {HEAD, ACCOUNTS, DONE}
114 private static class TransactionLoader
115 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
117 protected Void doInBackground(TransactionLoaderParams... p) {
118 LedgerTransaction tr = p[0].transaction;
120 SQLiteDatabase db = MLDB.getReadableDatabase();
123 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
126 for (LedgerTransactionAccount acc : tr.getAccounts()) {
127 // Log.d(c.getAccountName(), acc.getAmount()));
128 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
129 p[0].boldAccountName));
132 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
137 protected void onProgressUpdate(TransactionLoaderStep... values) {
138 super.onProgressUpdate(values);
139 TransactionLoaderStep step = values[0];
140 TransactionRowHolder holder = step.getHolder();
142 switch (step.getStep()) {
144 holder.tvDescription.setText(step.getTransaction().getDescription());
146 if (step.getPosition() % 2 == 0) {
147 holder.row.setBackgroundColor(Globals.tableRowEvenBG);
150 holder.row.setBackgroundColor(Globals.tableRowOddBG);
155 int rowIndex = step.getAccountPosition();
156 Context ctx = holder.row.getContext();
157 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
158 TextView accName, accAmount;
160 row = new LinearLayout(ctx);
161 row.setLayoutParams(new LinearLayout.LayoutParams(
162 LinearLayout.LayoutParams.MATCH_PARENT,
163 LinearLayout.LayoutParams.WRAP_CONTENT));
164 row.setGravity(Gravity.CENTER_VERTICAL);
165 row.setOrientation(LinearLayout.HORIZONTAL);
166 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
167 accName = new AppCompatTextView(ctx);
168 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
169 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
170 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
171 row.addView(accName);
172 accAmount = new AppCompatTextView(ctx);
173 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
174 LinearLayout.LayoutParams.WRAP_CONTENT,
175 LinearLayout.LayoutParams.WRAP_CONTENT);
177 accAmount.setLayoutParams(llp);
178 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
179 accAmount.setMinWidth(dp2px(ctx, 60));
180 row.addView(accAmount);
181 holder.tableAccounts.addView(row);
184 accName = (TextView) row.getChildAt(0);
185 accAmount = (TextView) row.getChildAt(1);
187 LedgerTransactionAccount acc = step.getAccount();
189 accName.setText(acc.getAccountName());
190 accAmount.setText(acc.toString());
192 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
193 // acc.getAccountName(), acc.getAmount()));
195 String boldAccountName = step.getBoldAccountName();
196 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
197 accName.setTypeface(null, Typeface.BOLD);
198 accAmount.setTypeface(null, Typeface.BOLD);
199 accName.setTextColor(Globals.primaryDark);
200 accAmount.setTextColor(Globals.primaryDark);
203 accName.setTypeface(null, Typeface.NORMAL);
204 accAmount.setTypeface(null, Typeface.NORMAL);
205 accName.setTextColor(Globals.defaultTextColor);
206 accAmount.setTextColor(Globals.defaultTextColor);
211 int accCount = step.getAccountCount();
212 if (holder.tableAccounts.getChildCount() > accCount) {
213 holder.tableAccounts.removeViews(accCount,
214 holder.tableAccounts.getChildCount() - accCount);
217 // Log.d("transactions",
218 // String.format("Position %d fill done", step.getPosition()));
223 private class TransactionLoaderParams {
224 LedgerTransaction transaction;
225 TransactionRowHolder holder;
227 String boldAccountName;
228 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
229 int position, String boldAccountName) {
230 this.transaction = transaction;
231 this.holder = holder;
232 this.position = position;
233 this.boldAccountName = boldAccountName;