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.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.graphics.Typeface;
23 import android.os.AsyncTask;
24 import android.text.Spannable;
25 import android.text.SpannableString;
26 import android.text.style.StyleSpan;
27 import android.view.Gravity;
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.appcompat.widget.AppCompatTextView;
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;
47 import java.text.DateFormat;
48 import java.util.Date;
49 import java.util.GregorianCalendar;
50 import java.util.TimeZone;
52 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
54 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
55 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
56 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
58 // in a race when transaction value is reduced, but the model hasn't been notified yet
59 // the view will disappear when the notifications reaches the model, so by simply omitting
60 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
62 if (item == null) return;
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 accounts", position,
71 // tr.getAccounts().size()));
73 TransactionLoader loader = new TransactionLoader();
74 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
75 new TransactionLoaderParams(tr, holder, position, Data.accountFilter.getValue(),
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(View.MeasureSpec
82 .makeMeasureSpec(holder.itemView.getWidth(), 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().format(date));
90 if (item.isMonthShown()) {
91 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
93 holder.tvDelimiterMonth
94 .setText(Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
95 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
96 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
97 holder.vDelimiterLine.setVisibility(View.GONE);
98 holder.vDelimiterThick.setVisibility(View.VISIBLE);
101 holder.tvDelimiterMonth.setVisibility(View.GONE);
102 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
103 holder.vDelimiterLine.setVisibility(View.VISIBLE);
104 holder.vDelimiterThick.setVisibility(View.GONE);
112 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
113 // debug("perf", "onCreateViewHolder called");
114 View row = LayoutInflater.from(parent.getContext())
115 .inflate(R.layout.transaction_list_row, parent, false);
116 return new TransactionRowHolder(row);
120 public int getItemCount() {
121 return Data.transactions.size();
123 enum LoaderStep {HEAD, ACCOUNTS, DONE}
124 private static class TransactionLoader
125 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
127 protected Void doInBackground(TransactionLoaderParams... p) {
128 LedgerTransaction tr = p[0].transaction;
129 boolean odd = p[0].odd;
131 SQLiteDatabase db = App.getDatabase();
134 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
137 // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
138 for (LedgerTransactionAccount acc : tr.getAccounts()) {
139 // debug(c.getAccountName(), acc.getAmount()));
140 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
141 p[0].boldAccountName));
144 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
149 protected void onProgressUpdate(TransactionLoaderStep... values) {
150 super.onProgressUpdate(values);
151 TransactionLoaderStep step = values[0];
152 TransactionRowHolder holder = step.getHolder();
154 switch (step.getStep()) {
156 holder.tvDescription.setText(step.getTransaction().getDescription());
158 if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
159 else holder.row.setBackgroundColor(Colors.tableRowLightBG);
163 int rowIndex = step.getAccountPosition();
164 Context ctx = holder.row.getContext();
165 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
167 LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
168 row = (LinearLayout) inflater.inflate(
169 R.layout.transaction_list_row_accounts_table_row,
170 holder.tableAccounts);
172 TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
173 TextView accComment =
174 row.findViewById(R.id.transaction_list_acc_row_acc_comment);
175 TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
176 LedgerTransactionAccount acc = step.getAccount();
179 // debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
180 // acc.getAccountName(), acc.getAmount()));
182 String boldAccountName = step.getBoldAccountName();
183 if ((boldAccountName != null) &&
184 acc.getAccountName().startsWith(boldAccountName))
186 accName.setTextColor(Colors.accent);
187 accAmount.setTextColor(Colors.accent);
189 SpannableString ss = new SpannableString(acc.getAccountName());
190 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
191 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
195 accName.setTextColor(Colors.defaultTextColor);
196 accAmount.setTextColor(Colors.defaultTextColor);
197 accName.setText(acc.getAccountName());
200 String comment = acc.getComment();
201 if (comment != null && !comment.isEmpty()) {
202 accComment.setText(comment);
203 accComment.setVisibility(View.VISIBLE);
206 accComment.setVisibility(View.GONE);
208 accAmount.setText(acc.toString());
212 int accCount = step.getAccountCount();
213 if (holder.tableAccounts.getChildCount() > accCount) {
214 holder.tableAccounts.removeViews(accCount,
215 holder.tableAccounts.getChildCount() - accCount);
218 // debug("transactions",
219 // String.format("Position %d fill done", step.getPosition()));
224 private class TransactionLoaderParams {
225 LedgerTransaction transaction;
226 TransactionRowHolder holder;
228 String boldAccountName;
230 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
231 int position, String boldAccountName, boolean odd) {
232 this.transaction = transaction;
233 this.holder = holder;
234 this.position = position;
235 this.boldAccountName = boldAccountName;