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;
46 import java.text.DateFormat;
47 import java.util.Date;
48 import java.util.GregorianCalendar;
49 import java.util.TimeZone;
51 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
52 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
53 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
55 // in a race when transaction value is reduced, but the model hasn't been notified yet
56 // the view will disappear when the notifications reaches the model, so by simply omitting
57 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
62 switch (item.getType()) {
64 holder.vTransaction.setVisibility(View.VISIBLE);
65 holder.vDelimiter.setVisibility(View.GONE);
66 LedgerTransaction tr = item.getTransaction();
68 // debug("transactions", String.format("Filling position %d with %d
69 // accounts", position,
70 // tr.getAccounts().size()));
72 TransactionLoader loader = new TransactionLoader();
73 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
74 new TransactionLoaderParams(tr, holder, position,
75 Data.accountFilter.getValue(), item.isOdd()));
77 // WORKAROUND what seems to be a bug in CardHolder somewhere
78 // when a view that was previously holding a delimiter is re-purposed
79 // occasionally it stays too short (not high enough)
80 holder.vTransaction.measure(
81 View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
82 View.MeasureSpec.EXACTLY),
83 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
86 Date date = item.getDate();
87 holder.vTransaction.setVisibility(View.GONE);
88 holder.vDelimiter.setVisibility(View.VISIBLE);
89 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
91 if (item.isMonthShown()) {
92 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
94 holder.tvDelimiterMonth.setText(
95 Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
96 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
97 // holder.vDelimiterLine.setBackgroundResource(R.drawable
98 // .dashed_border_8dp);
99 holder.vDelimiterLine.setVisibility(View.GONE);
100 holder.vDelimiterThick.setVisibility(View.VISIBLE);
103 holder.tvDelimiterMonth.setVisibility(View.GONE);
104 // holder.vDelimiterLine.setBackgroundResource(R.drawable
105 // .dashed_border_1dp);
106 holder.vDelimiterLine.setVisibility(View.VISIBLE);
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()
164 holder.row.setBackgroundColor(Colors.tableRowDarkBG);
166 holder.row.setBackgroundColor(Colors.tableRowLightBG);
170 int rowIndex = step.getAccountPosition();
171 Context ctx = holder.row.getContext();
172 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
174 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
175 row = (LinearLayout) inflater.inflate(
176 R.layout.transaction_list_row_accounts_table_row, null);
177 // if the rootView above is given (and the line below is spared)
178 // the accounts remain with their default text (set in the layout resource)
179 holder.tableAccounts.addView(row);
181 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
182 TextView accComment =
183 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
184 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
185 LedgerTransactionAccount acc = step.getAccount();
188 // debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
189 // acc.getAccountName(), acc.getAmount()));
191 String boldAccountName = step.getBoldAccountName();
192 if ((boldAccountName != null) && acc.getAccountName()
193 .startsWith(boldAccountName))
195 accName.setTextColor(Colors.accent);
196 accAmount.setTextColor(Colors.accent);
198 SpannableString ss = new SpannableString(acc.getAccountName());
199 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
200 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
204 accName.setTextColor(Colors.defaultTextColor);
205 accAmount.setTextColor(Colors.defaultTextColor);
206 accName.setText(acc.getAccountName());
209 String comment = acc.getComment();
210 if (comment != null && !comment.isEmpty()) {
211 accComment.setText(comment);
212 accComment.setVisibility(View.VISIBLE);
215 accComment.setVisibility(View.GONE);
217 accAmount.setText(acc.toString());
221 int accCount = step.getAccountCount();
222 if (holder.tableAccounts.getChildCount() > accCount) {
223 holder.tableAccounts.removeViews(accCount,
224 holder.tableAccounts.getChildCount() - accCount);
227 // debug("transactions",
228 // String.format("Position %d fill done", step.getPosition()));
233 private class TransactionLoaderParams {
234 LedgerTransaction transaction;
235 TransactionRowHolder holder;
237 String boldAccountName;
239 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
240 int position, String boldAccountName, boolean odd) {
241 this.transaction = transaction;
242 this.holder = holder;
243 this.position = position;
244 this.boldAccountName = boldAccountName;