2 * Copyright © 2019 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.NonNull;
35 import androidx.recyclerview.widget.RecyclerView;
37 import net.ktnx.mobileledger.App;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerTransaction;
41 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
42 import net.ktnx.mobileledger.model.TransactionListItem;
43 import net.ktnx.mobileledger.utils.Colors;
44 import net.ktnx.mobileledger.utils.Globals;
45 import net.ktnx.mobileledger.utils.Misc;
47 import java.text.DateFormat;
48 import java.util.Date;
49 import java.util.GregorianCalendar;
50 import java.util.TimeZone;
52 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
53 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
54 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
56 // in a race when transaction value is reduced, but the model hasn't been notified yet
57 // the view will disappear when the notifications reaches the model, so by simply omitting
58 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
63 switch (item.getType()) {
65 holder.vTransaction.setVisibility(View.VISIBLE);
66 holder.vDelimiter.setVisibility(View.GONE);
67 LedgerTransaction tr = item.getTransaction();
69 // debug("transactions", String.format("Filling position %d with %d
70 // accounts", position,
71 // tr.getAccounts().size()));
73 TransactionLoader loader = new TransactionLoader();
74 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
75 new TransactionLoaderParams(tr, holder, position,
76 Data.accountFilter.getValue(), item.isOdd()));
78 // WORKAROUND what seems to be a bug in CardHolder somewhere
79 // when a view that was previously holding a delimiter is re-purposed
80 // occasionally it stays too short (not high enough)
81 holder.vTransaction.measure(
82 View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
83 View.MeasureSpec.EXACTLY),
84 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
87 Date date = item.getDate();
88 holder.vTransaction.setVisibility(View.GONE);
89 holder.vDelimiter.setVisibility(View.VISIBLE);
90 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
92 if (item.isMonthShown()) {
93 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
95 holder.tvDelimiterMonth.setText(
96 Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
97 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
98 // holder.vDelimiterLine.setBackgroundResource(R.drawable
99 // .dashed_border_8dp);
100 holder.vDelimiterThick.setVisibility(View.VISIBLE);
103 holder.tvDelimiterMonth.setVisibility(View.GONE);
104 // holder.vDelimiterLine.setBackgroundResource(R.drawable
105 // .dashed_border_1dp);
106 holder.vDelimiterThick.setVisibility(View.GONE);
114 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
115 // debug("perf", "onCreateViewHolder called");
116 View row = LayoutInflater.from(parent.getContext())
117 .inflate(R.layout.transaction_list_row, parent, false);
118 return new TransactionRowHolder(row);
122 public int getItemCount() {
123 return Data.transactions.size();
125 enum LoaderStep {HEAD, ACCOUNTS, DONE}
127 private static class TransactionLoader
128 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
130 protected Void doInBackground(TransactionLoaderParams... p) {
131 LedgerTransaction tr = p[0].transaction;
132 boolean odd = p[0].odd;
134 SQLiteDatabase db = App.getDatabase();
137 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
140 // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
141 for (LedgerTransactionAccount acc : tr.getAccounts()) {
142 // debug(c.getAccountName(), acc.getAmount()));
143 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
144 p[0].boldAccountName));
147 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
152 protected void onProgressUpdate(TransactionLoaderStep... values) {
153 super.onProgressUpdate(values);
154 TransactionLoaderStep step = values[0];
155 TransactionRowHolder holder = step.getHolder();
157 switch (step.getStep()) {
159 holder.tvDescription.setText(step.getTransaction()
161 String trComment = Misc.emptyIsNull(step.getTransaction()
163 if (trComment == null)
164 holder.tvComment.setVisibility(View.GONE);
166 holder.tvComment.setText(trComment);
167 holder.tvComment.setVisibility(View.VISIBLE);
171 // holder.row.setBackgroundColor(Colors.tableRowDarkBG);
173 // holder.row.setBackgroundColor(Colors.tableRowLightBG);
177 int rowIndex = step.getAccountPosition();
178 Context ctx = holder.row.getContext();
179 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
181 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
182 row = (LinearLayout) inflater.inflate(
183 R.layout.transaction_list_row_accounts_table_row, null);
184 // if the rootView above is given (and the line below is spared)
185 // the accounts remain with their default text (set in the layout resource)
186 holder.tableAccounts.addView(row);
188 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
189 TextView accComment =
190 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
191 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
192 LedgerTransactionAccount acc = step.getAccount();
195 // debug("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) && acc.getAccountName()
200 .startsWith(boldAccountName))
202 accName.setTextColor(Colors.accent);
203 accAmount.setTextColor(Colors.accent);
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(Colors.defaultTextColor);
212 accAmount.setTextColor(Colors.defaultTextColor);
213 accName.setText(acc.getAccountName());
216 String comment = acc.getComment();
217 if (comment != null && !comment.isEmpty()) {
218 accComment.setText(comment);
219 accComment.setVisibility(View.VISIBLE);
222 accComment.setVisibility(View.GONE);
224 accAmount.setText(acc.toString());
228 int accCount = step.getAccountCount();
229 if (holder.tableAccounts.getChildCount() > accCount) {
230 holder.tableAccounts.removeViews(accCount,
231 holder.tableAccounts.getChildCount() - accCount);
234 // debug("transactions",
235 // String.format("Position %d fill done", step.getPosition()));
240 private class TransactionLoaderParams {
241 LedgerTransaction transaction;
242 TransactionRowHolder holder;
244 String boldAccountName;
246 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
247 int position, String boldAccountName, boolean odd) {
248 this.transaction = transaction;
249 this.holder = holder;
250 this.position = position;
251 this.boldAccountName = boldAccountName;