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.MobileLedgerProfile;
46 import net.ktnx.mobileledger.model.TransactionListItem;
47 import net.ktnx.mobileledger.utils.Colors;
48 import net.ktnx.mobileledger.utils.Globals;
49 import net.ktnx.mobileledger.utils.Misc;
50 import net.ktnx.mobileledger.utils.SimpleDate;
52 import java.text.DateFormat;
53 import java.util.GregorianCalendar;
54 import java.util.Locale;
55 import java.util.TimeZone;
57 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
58 private MobileLedgerProfile profile;
59 private AsyncListDiffer<TransactionListItem> listDiffer;
60 public TransactionListAdapter() {
62 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
64 public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
65 @NonNull TransactionListItem newItem) {
66 if (oldItem.getType() != newItem.getType())
68 switch (oldItem.getType()) {
70 return (oldItem.getDate()
71 .equals(newItem.getDate()));
73 return oldItem.getTransaction()
74 .getId() == newItem.getTransaction()
77 throw new IllegalStateException(
78 String.format(Locale.US, "Unexpected transaction item type %s",
83 public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
84 @NonNull TransactionListItem newItem) {
85 switch (oldItem.getType()) {
87 // Delimiters items are "same" for same dates and the contents are the date
90 return oldItem.getTransaction()
91 .equals(newItem.getTransaction());
93 throw new IllegalStateException(
94 String.format(Locale.US, "Unexpected transaction item type %s",
101 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
102 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
104 // in a race when transaction value is reduced, but the model hasn't been notified yet
105 // the view will disappear when the notifications reaches the model, so by simply omitting
106 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
111 switch (item.getType()) {
113 holder.vTransaction.setVisibility(View.VISIBLE);
114 holder.vDelimiter.setVisibility(View.GONE);
115 LedgerTransaction tr = item.getTransaction();
117 // debug("transactions", String.format("Filling position %d with %d
118 // accounts", position,
119 // tr.getAccounts().size()));
121 TransactionLoader loader = new TransactionLoader();
122 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
123 new TransactionLoaderParams(tr, holder, position,
124 Data.accountFilter.getValue(), item.isOdd()));
126 // WORKAROUND what seems to be a bug in CardHolder somewhere
127 // when a view that was previously holding a delimiter is re-purposed
128 // occasionally it stays too short (not high enough)
129 holder.vTransaction.measure(
130 View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
131 View.MeasureSpec.EXACTLY),
132 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
135 SimpleDate date = item.getDate();
136 holder.vTransaction.setVisibility(View.GONE);
137 holder.vDelimiter.setVisibility(View.VISIBLE);
138 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
139 .format(date.toDate()));
140 if (item.isMonthShown()) {
141 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
142 cal.setTime(date.toDate());
143 App.prepareMonthNames();
144 holder.tvDelimiterMonth.setText(
145 Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
146 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
147 // holder.vDelimiterLine.setBackgroundResource(R.drawable
148 // .dashed_border_8dp);
149 holder.vDelimiterThick.setVisibility(View.VISIBLE);
152 holder.tvDelimiterMonth.setVisibility(View.GONE);
153 // holder.vDelimiterLine.setBackgroundResource(R.drawable
154 // .dashed_border_1dp);
155 holder.vDelimiterThick.setVisibility(View.GONE);
163 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
164 // debug("perf", "onCreateViewHolder called");
165 View row = LayoutInflater.from(parent.getContext())
166 .inflate(R.layout.transaction_list_row, parent, false);
167 return new TransactionRowHolder(row);
171 public int getItemCount() {
172 return Data.transactions.size();
174 enum LoaderStep {HEAD, ACCOUNTS, DONE}
176 private static class TransactionLoader
177 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
179 protected Void doInBackground(TransactionLoaderParams... p) {
180 LedgerTransaction tr = p[0].transaction;
181 boolean odd = p[0].odd;
183 SQLiteDatabase db = App.getDatabase();
186 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
189 // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
190 for (LedgerTransactionAccount acc : tr.getAccounts()) {
191 // debug(c.getAccountName(), acc.getAmount()));
192 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
193 p[0].boldAccountName));
196 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
201 protected void onProgressUpdate(TransactionLoaderStep... values) {
202 super.onProgressUpdate(values);
203 TransactionLoaderStep step = values[0];
204 TransactionRowHolder holder = step.getHolder();
206 switch (step.getStep()) {
208 holder.tvDescription.setText(step.getTransaction()
210 String trComment = Misc.emptyIsNull(step.getTransaction()
212 if (trComment == null)
213 holder.tvComment.setVisibility(View.GONE);
215 holder.tvComment.setText(trComment);
216 holder.tvComment.setVisibility(View.VISIBLE);
220 // holder.row.setBackgroundColor(Colors.tableRowDarkBG);
222 // holder.row.setBackgroundColor(Colors.tableRowLightBG);
226 int rowIndex = step.getAccountPosition();
227 Context ctx = holder.row.getContext();
228 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
230 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
231 row = (LinearLayout) inflater.inflate(
232 R.layout.transaction_list_row_accounts_table_row, null);
233 // if the rootView above is given (and the line below is spared)
234 // the accounts remain with their default text (set in the layout resource)
235 holder.tableAccounts.addView(row);
237 TextView dummyText = row.findViewById(R.id.dummy_text);
238 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
239 TextView accComment =
240 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
241 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
242 LedgerTransactionAccount acc = step.getAccount();
245 // debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
246 // acc.getAccountName(), acc.getAmount()));
248 String boldAccountName = step.getBoldAccountName();
249 if ((boldAccountName != null) && acc.getAccountName()
250 .startsWith(boldAccountName))
252 accName.setTextColor(Colors.secondary);
253 accAmount.setTextColor(Colors.secondary);
255 SpannableString ss = new SpannableString(acc.getAccountName());
256 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
257 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
261 @ColorInt int textColor = dummyText.getTextColors()
263 accName.setTextColor(textColor);
264 accAmount.setTextColor(textColor);
265 accName.setText(acc.getAccountName());
268 String comment = acc.getComment();
269 if (comment != null && !comment.isEmpty()) {
270 accComment.setText(comment);
271 accComment.setVisibility(View.VISIBLE);
274 accComment.setVisibility(View.GONE);
276 accAmount.setText(acc.toString());
280 int accCount = step.getAccountCount();
281 if (holder.tableAccounts.getChildCount() > accCount) {
282 holder.tableAccounts.removeViews(accCount,
283 holder.tableAccounts.getChildCount() - accCount);
286 // debug("transactions",
287 // String.format("Position %d fill done", step.getPosition()));
292 private class TransactionLoaderParams {
293 LedgerTransaction transaction;
294 TransactionRowHolder holder;
296 String boldAccountName;
298 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
299 int position, String boldAccountName, boolean odd) {
300 this.transaction = transaction;
301 this.holder = holder;
302 this.position = position;
303 this.boldAccountName = boldAccountName;