2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe 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 * MoLe 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 MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.transaction_list;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.graphics.Typeface;
24 import android.os.AsyncTask;
25 import android.text.Spannable;
26 import android.text.SpannableString;
27 import android.text.style.StyleSpan;
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 androidx.annotation.ColorInt;
35 import androidx.annotation.NonNull;
36 import androidx.recyclerview.widget.AsyncListDiffer;
37 import androidx.recyclerview.widget.DiffUtil;
38 import androidx.recyclerview.widget.RecyclerView;
40 import net.ktnx.mobileledger.App;
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.LedgerTransaction;
44 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
45 import net.ktnx.mobileledger.model.TransactionListItem;
46 import net.ktnx.mobileledger.ui.MainModel;
47 import net.ktnx.mobileledger.utils.Colors;
48 import net.ktnx.mobileledger.utils.Globals;
49 import net.ktnx.mobileledger.utils.Logger;
50 import net.ktnx.mobileledger.utils.Misc;
51 import net.ktnx.mobileledger.utils.SimpleDate;
53 import java.text.DateFormat;
54 import java.util.GregorianCalendar;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.TimeZone;
59 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
60 private final MainModel model;
61 private final AsyncListDiffer<TransactionListItem> listDiffer;
62 public TransactionListAdapter(MainModel model) {
66 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
68 public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
69 @NonNull TransactionListItem newItem) {
70 if (oldItem.getType() != newItem.getType())
72 switch (oldItem.getType()) {
74 return (oldItem.getDate()
75 .equals(newItem.getDate()));
77 return oldItem.getTransaction()
78 .getId() == newItem.getTransaction()
81 return true; // there can be only one header
83 throw new IllegalStateException(
84 String.format(Locale.US, "Unexpected transaction item type %s",
89 public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
90 @NonNull TransactionListItem newItem) {
91 switch (oldItem.getType()) {
93 // Delimiters items are "same" for same dates and the contents are the date
96 return oldItem.getTransaction()
97 .equals(newItem.getTransaction());
99 // headers don't differ in their contents. they observe the last update
100 // date and react to its changes
103 throw new IllegalStateException(
104 String.format(Locale.US, "Unexpected transaction item type %s",
111 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
112 TransactionListItem item = listDiffer.getCurrentList()
115 // in a race when transaction value is reduced, but the model hasn't been notified yet
116 // the view will disappear when the notifications reaches the model, so by simply omitting
117 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
122 final TransactionListItem.Type newType = item.getType();
123 holder.setType(newType);
127 LedgerTransaction tr = item.getTransaction();
129 // debug("transactions", String.format("Filling position %d with %d
130 // accounts", position,
131 // tr.getAccounts().size()));
133 TransactionLoader loader = new TransactionLoader();
134 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
135 new TransactionLoaderParams(tr, holder, position, model.getAccountFilter()
138 // WORKAROUND what seems to be a bug in CardHolder somewhere
139 // when a view that was previously holding a delimiter is re-purposed
140 // occasionally it stays too short (not high enough)
141 holder.vTransaction.measure(
142 View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
143 View.MeasureSpec.EXACTLY),
144 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
147 SimpleDate date = item.getDate();
148 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
149 .format(date.toDate()));
150 if (item.isMonthShown()) {
151 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
152 cal.setTime(date.toDate());
153 App.prepareMonthNames();
154 holder.tvDelimiterMonth.setText(
155 Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
156 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
157 // holder.vDelimiterLine.setBackgroundResource(R.drawable
158 // .dashed_border_8dp);
159 holder.vDelimiterThick.setVisibility(View.VISIBLE);
162 holder.tvDelimiterMonth.setVisibility(View.GONE);
163 // holder.vDelimiterLine.setBackgroundResource(R.drawable
164 // .dashed_border_1dp);
165 holder.vDelimiterThick.setVisibility(View.GONE);
169 holder.setLastUpdateText(Data.lastTransactionsUpdateText.get());
173 throw new IllegalStateException("Unexpected value: " + newType);
178 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
179 // debug("perf", "onCreateViewHolder called");
180 View row = LayoutInflater.from(parent.getContext())
181 .inflate(R.layout.transaction_list_row, parent, false);
182 return new TransactionRowHolder(row);
186 public int getItemCount() {
187 return listDiffer.getCurrentList()
190 public void setTransactions(List<TransactionListItem> newList) {
191 Logger.debug("transactions",
192 String.format(Locale.US, "Got new transaction list (%d items)", newList.size()));
193 listDiffer.submitList(newList);
195 enum LoaderStep {HEAD, ACCOUNTS, DONE}
197 private static class TransactionLoader
198 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
200 protected Void doInBackground(TransactionLoaderParams... p) {
201 LedgerTransaction tr = p[0].transaction;
203 SQLiteDatabase db = App.getDatabase();
206 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
209 // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
210 for (LedgerTransactionAccount acc : tr.getAccounts()) {
211 // debug(c.getAccountName(), acc.getAmount()));
212 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
213 p[0].boldAccountName));
216 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
221 protected void onProgressUpdate(TransactionLoaderStep... values) {
222 super.onProgressUpdate(values);
223 TransactionLoaderStep step = values[0];
224 TransactionRowHolder holder = step.getHolder();
226 switch (step.getStep()) {
228 holder.tvDescription.setText(step.getTransaction()
230 String trComment = Misc.emptyIsNull(step.getTransaction()
232 if (trComment == null)
233 holder.tvComment.setVisibility(View.GONE);
235 holder.tvComment.setText(trComment);
236 holder.tvComment.setVisibility(View.VISIBLE);
240 // holder.row.setBackgroundColor(Colors.tableRowDarkBG);
242 // holder.row.setBackgroundColor(Colors.tableRowLightBG);
246 int rowIndex = step.getAccountPosition();
247 Context ctx = holder.row.getContext();
248 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
250 row = new LinearLayout(ctx);
251 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
252 inflater.inflate(R.layout.transaction_list_row_accounts_table_row, row);
253 holder.tableAccounts.addView(row);
255 TextView dummyText = row.findViewById(R.id.dummy_text);
256 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
257 TextView accComment =
258 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
259 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
260 LedgerTransactionAccount acc = step.getAccount();
263 // debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
264 // acc.getAccountName(), acc.getAmount()));
266 String boldAccountName = step.getBoldAccountName();
267 if ((boldAccountName != null) && acc.getAccountName()
268 .startsWith(boldAccountName))
270 accName.setTextColor(Colors.secondary);
271 accAmount.setTextColor(Colors.secondary);
273 SpannableString ss = new SpannableString(acc.getAccountName());
274 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
275 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
279 @ColorInt int textColor = dummyText.getTextColors()
281 accName.setTextColor(textColor);
282 accAmount.setTextColor(textColor);
283 accName.setText(acc.getAccountName());
286 String comment = acc.getComment();
287 if (comment != null && !comment.isEmpty()) {
288 accComment.setText(comment);
289 accComment.setVisibility(View.VISIBLE);
292 accComment.setVisibility(View.GONE);
294 accAmount.setText(acc.toString());
298 int accCount = step.getAccountCount();
299 if (holder.tableAccounts.getChildCount() > accCount) {
300 holder.tableAccounts.removeViews(accCount,
301 holder.tableAccounts.getChildCount() - accCount);
304 // debug("transactions",
305 // String.format("Position %d fill done", step.getPosition()));
310 private static class TransactionLoaderParams {
311 final LedgerTransaction transaction;
312 final TransactionRowHolder holder;
314 final String boldAccountName;
315 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
316 int position, String boldAccountName) {
317 this.transaction = transaction;
318 this.holder = holder;
319 this.position = position;
320 this.boldAccountName = boldAccountName;