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.text.Spannable;
28 import android.text.SpannableString;
29 import android.text.style.StyleSpan;
30 import android.view.Gravity;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.model.LedgerTransaction;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.TransactionListItem;
41 import net.ktnx.mobileledger.utils.Globals;
42 import net.ktnx.mobileledger.utils.MLDB;
44 import java.text.DateFormat;
45 import java.util.Date;
47 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
49 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
50 private String boldAccountName;
51 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
52 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
54 // in a race when transaction value is reduced, but the model hasn't been notified yet
55 // the view will disappear when the notifications reaches the model, so by simply omitting
56 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
58 if (item == null) return;
60 if (item.getType() == TransactionListItem.Type.TRANSACTION) {
61 holder.vTransaction.setVisibility(View.VISIBLE);
62 holder.vDelimiter.setVisibility(View.GONE);
63 LedgerTransaction tr = item.getTransaction();
65 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
66 // tr.getAccounts().size()));
68 TransactionLoader loader = new TransactionLoader();
69 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
72 // WORKAROUND what seems to be a bug in CardHolder somewhere
73 // when a view that was previously holding a delimiter is re-purposed
74 // occasionally it stays too short (not high enough)
75 holder.vTransaction.measure(View.MeasureSpec
76 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
77 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
80 Date date = item.getDate();
81 holder.vTransaction.setVisibility(View.GONE);
82 holder.vDelimiter.setVisibility(View.VISIBLE);
83 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
84 if (item.isMonthShown()) {
85 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
86 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
87 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
88 holder.vDelimiterLine.setVisibility(View.GONE);
89 holder.vDelimiterThick.setVisibility(View.VISIBLE);
92 holder.tvDelimiterMonth.setVisibility(View.GONE);
93 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
94 holder.vDelimiterLine.setVisibility(View.VISIBLE);
95 holder.vDelimiterThick.setVisibility(View.GONE);
102 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
103 // Log.d("perf", "onCreateViewHolder called");
104 View row = LayoutInflater.from(parent.getContext())
105 .inflate(R.layout.transaction_list_row, parent, false);
106 return new TransactionRowHolder(row);
110 public int getItemCount() {
111 return TransactionListViewModel.getTransactionCount();
113 public void setBoldAccountName(String boldAccountName) {
114 this.boldAccountName = boldAccountName;
116 public void resetBoldAccountName() {
117 this.boldAccountName = null;
120 enum LoaderStep {HEAD, ACCOUNTS, DONE}
122 private static class TransactionLoader
123 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
125 protected Void doInBackground(TransactionLoaderParams... p) {
126 LedgerTransaction tr = p[0].transaction;
127 boolean odd = p[0].odd;
129 SQLiteDatabase db = MLDB.getReadableDatabase();
132 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
135 for (LedgerTransactionAccount acc : tr.getAccounts()) {
136 // Log.d(c.getAccountName(), acc.getAmount()));
137 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
138 p[0].boldAccountName));
141 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
146 protected void onProgressUpdate(TransactionLoaderStep... values) {
147 super.onProgressUpdate(values);
148 TransactionLoaderStep step = values[0];
149 TransactionRowHolder holder = step.getHolder();
151 switch (step.getStep()) {
153 holder.tvDescription.setText(step.getTransaction().getDescription());
155 if (step.isOdd()) holder.row.setBackgroundColor(Globals.tableRowDarkBG);
156 else holder.row.setBackgroundColor(Globals.tableRowLightBG);
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();
195 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
196 // acc.getAccountName(), acc.getAmount()));
198 String boldAccountName = step.getBoldAccountName();
199 if ((boldAccountName != null) &&
200 acc.getAccountName().startsWith(boldAccountName))
202 accName.setTextColor(Globals.primaryDark);
203 accAmount.setTextColor(Globals.primaryDark);
205 SpannableString ss = new SpannableString(acc.getAccountName());
206 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
207 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
211 accName.setTextColor(Globals.defaultTextColor);
212 accAmount.setTextColor(Globals.defaultTextColor);
213 accName.setText(acc.getAccountName());
215 accAmount.setText(acc.toString());
219 int accCount = step.getAccountCount();
220 if (holder.tableAccounts.getChildCount() > accCount) {
221 holder.tableAccounts.removeViews(accCount,
222 holder.tableAccounts.getChildCount() - accCount);
225 // Log.d("transactions",
226 // String.format("Position %d fill done", step.getPosition()));
231 private class TransactionLoaderParams {
232 LedgerTransaction transaction;
233 TransactionRowHolder holder;
235 String boldAccountName;
237 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
238 int position, String boldAccountName, boolean odd) {
239 this.transaction = transaction;
240 this.holder = holder;
241 this.position = position;
242 this.boldAccountName = boldAccountName;