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.RecyclerView;
38 import net.ktnx.mobileledger.App;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.LedgerTransaction;
42 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
43 import net.ktnx.mobileledger.model.TransactionListItem;
44 import net.ktnx.mobileledger.utils.Colors;
45 import net.ktnx.mobileledger.utils.Globals;
46 import net.ktnx.mobileledger.utils.Misc;
47 import net.ktnx.mobileledger.utils.SimpleDate;
49 import java.text.DateFormat;
50 import java.util.GregorianCalendar;
51 import java.util.TimeZone;
53 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
54 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
55 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
57 // in a race when transaction value is reduced, but the model hasn't been notified yet
58 // the view will disappear when the notifications reaches the model, so by simply omitting
59 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
64 switch (item.getType()) {
66 holder.vTransaction.setVisibility(View.VISIBLE);
67 holder.vDelimiter.setVisibility(View.GONE);
68 LedgerTransaction tr = item.getTransaction();
70 // debug("transactions", String.format("Filling position %d with %d
71 // accounts", position,
72 // tr.getAccounts().size()));
74 TransactionLoader loader = new TransactionLoader();
75 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
76 new TransactionLoaderParams(tr, holder, position,
77 Data.accountFilter.getValue(), item.isOdd()));
79 // WORKAROUND what seems to be a bug in CardHolder somewhere
80 // when a view that was previously holding a delimiter is re-purposed
81 // occasionally it stays too short (not high enough)
82 holder.vTransaction.measure(
83 View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
84 View.MeasureSpec.EXACTLY),
85 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
88 SimpleDate date = item.getDate();
89 holder.vTransaction.setVisibility(View.GONE);
90 holder.vDelimiter.setVisibility(View.VISIBLE);
91 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
92 .format(date.toDate()));
93 if (item.isMonthShown()) {
94 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
95 cal.setTime(date.toDate());
96 holder.tvDelimiterMonth.setText(
97 Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
98 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
99 // holder.vDelimiterLine.setBackgroundResource(R.drawable
100 // .dashed_border_8dp);
101 holder.vDelimiterThick.setVisibility(View.VISIBLE);
104 holder.tvDelimiterMonth.setVisibility(View.GONE);
105 // holder.vDelimiterLine.setBackgroundResource(R.drawable
106 // .dashed_border_1dp);
107 holder.vDelimiterThick.setVisibility(View.GONE);
115 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
116 // debug("perf", "onCreateViewHolder called");
117 View row = LayoutInflater.from(parent.getContext())
118 .inflate(R.layout.transaction_list_row, parent, false);
119 return new TransactionRowHolder(row);
123 public int getItemCount() {
124 return Data.transactions.size();
126 enum LoaderStep {HEAD, ACCOUNTS, DONE}
128 private static class TransactionLoader
129 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
131 protected Void doInBackground(TransactionLoaderParams... p) {
132 LedgerTransaction tr = p[0].transaction;
133 boolean odd = p[0].odd;
135 SQLiteDatabase db = App.getDatabase();
138 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
141 // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
142 for (LedgerTransactionAccount acc : tr.getAccounts()) {
143 // debug(c.getAccountName(), acc.getAmount()));
144 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
145 p[0].boldAccountName));
148 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
153 protected void onProgressUpdate(TransactionLoaderStep... values) {
154 super.onProgressUpdate(values);
155 TransactionLoaderStep step = values[0];
156 TransactionRowHolder holder = step.getHolder();
158 switch (step.getStep()) {
160 holder.tvDescription.setText(step.getTransaction()
162 String trComment = Misc.emptyIsNull(step.getTransaction()
164 if (trComment == null)
165 holder.tvComment.setVisibility(View.GONE);
167 holder.tvComment.setText(trComment);
168 holder.tvComment.setVisibility(View.VISIBLE);
172 // holder.row.setBackgroundColor(Colors.tableRowDarkBG);
174 // holder.row.setBackgroundColor(Colors.tableRowLightBG);
178 int rowIndex = step.getAccountPosition();
179 Context ctx = holder.row.getContext();
180 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
182 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
183 row = (LinearLayout) inflater.inflate(
184 R.layout.transaction_list_row_accounts_table_row, null);
185 // if the rootView above is given (and the line below is spared)
186 // the accounts remain with their default text (set in the layout resource)
187 holder.tableAccounts.addView(row);
189 TextView dummyText = row.findViewById(R.id.dummy_text);
190 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
191 TextView accComment =
192 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
193 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
194 LedgerTransactionAccount acc = step.getAccount();
197 // debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
198 // acc.getAccountName(), acc.getAmount()));
200 String boldAccountName = step.getBoldAccountName();
201 if ((boldAccountName != null) && acc.getAccountName()
202 .startsWith(boldAccountName))
204 accName.setTextColor(Colors.accent);
205 accAmount.setTextColor(Colors.accent);
207 SpannableString ss = new SpannableString(acc.getAccountName());
208 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
209 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
213 @ColorInt int textColor = dummyText.getTextColors()
215 accName.setTextColor(textColor);
216 accAmount.setTextColor(textColor);
217 accName.setText(acc.getAccountName());
220 String comment = acc.getComment();
221 if (comment != null && !comment.isEmpty()) {
222 accComment.setText(comment);
223 accComment.setVisibility(View.VISIBLE);
226 accComment.setVisibility(View.GONE);
228 accAmount.setText(acc.toString());
232 int accCount = step.getAccountCount();
233 if (holder.tableAccounts.getChildCount() > accCount) {
234 holder.tableAccounts.removeViews(accCount,
235 holder.tableAccounts.getChildCount() - accCount);
238 // debug("transactions",
239 // String.format("Position %d fill done", step.getPosition()));
244 private class TransactionLoaderParams {
245 LedgerTransaction transaction;
246 TransactionRowHolder holder;
248 String boldAccountName;
250 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
251 int position, String boldAccountName, boolean odd) {
252 this.transaction = transaction;
253 this.holder = holder;
254 this.position = position;
255 this.boldAccountName = boldAccountName;