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 net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.model.TransactionListItem;
39 import net.ktnx.mobileledger.utils.Colors;
40 import net.ktnx.mobileledger.utils.Globals;
41 import net.ktnx.mobileledger.utils.MLDB;
43 import java.text.DateFormat;
44 import java.util.Date;
45 import java.util.GregorianCalendar;
46 import java.util.TimeZone;
48 import androidx.annotation.NonNull;
49 import androidx.appcompat.widget.AppCompatTextView;
50 import androidx.recyclerview.widget.RecyclerView;
52 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
54 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
55 private String boldAccountName;
56 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
57 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
59 // in a race when transaction value is reduced, but the model hasn't been notified yet
60 // the view will disappear when the notifications reaches the model, so by simply omitting
61 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
63 if (item == null) return;
65 switch (item.getType()) {
67 holder.vTransaction.setVisibility(View.VISIBLE);
68 holder.vDelimiter.setVisibility(View.GONE);
69 holder.vTrailer.setVisibility(View.GONE);
70 LedgerTransaction tr = item.getTransaction();
72 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
73 // tr.getAccounts().size()));
75 TransactionLoader loader = new TransactionLoader();
76 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
77 new TransactionLoaderParams(tr, holder, position, boldAccountName,
80 // WORKAROUND what seems to be a bug in CardHolder somewhere
81 // when a view that was previously holding a delimiter is re-purposed
82 // occasionally it stays too short (not high enough)
83 holder.vTransaction.measure(View.MeasureSpec
84 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
85 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
88 Date date = item.getDate();
89 holder.vTransaction.setVisibility(View.GONE);
90 holder.vTrailer.setVisibility(View.GONE);
91 holder.vDelimiter.setVisibility(View.VISIBLE);
92 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
93 if (item.isMonthShown()) {
94 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
96 holder.tvDelimiterMonth
97 .setText(Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
98 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
99 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
100 holder.vDelimiterLine.setVisibility(View.GONE);
101 holder.vDelimiterThick.setVisibility(View.VISIBLE);
104 holder.tvDelimiterMonth.setVisibility(View.GONE);
105 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
106 holder.vDelimiterLine.setVisibility(View.VISIBLE);
107 holder.vDelimiterThick.setVisibility(View.GONE);
111 holder.vTransaction.setVisibility(View.GONE);
112 holder.vTrailer.setVisibility(View.VISIBLE);
113 holder.vDelimiter.setVisibility(View.GONE);
121 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
122 // Log.d("perf", "onCreateViewHolder called");
123 View row = LayoutInflater.from(parent.getContext())
124 .inflate(R.layout.transaction_list_row, parent, false);
125 return new TransactionRowHolder(row);
129 public int getItemCount() {
130 return Data.transactions.size() + 1;
132 public void setBoldAccountName(String boldAccountName) {
133 this.boldAccountName = boldAccountName;
135 public void resetBoldAccountName() {
136 this.boldAccountName = null;
139 enum LoaderStep {HEAD, ACCOUNTS, DONE}
141 private static class TransactionLoader
142 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
144 protected Void doInBackground(TransactionLoaderParams... p) {
145 LedgerTransaction tr = p[0].transaction;
146 boolean odd = p[0].odd;
148 SQLiteDatabase db = MLDB.getDatabase();
151 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
154 for (LedgerTransactionAccount acc : tr.getAccounts()) {
155 // Log.d(c.getAccountName(), acc.getAmount()));
156 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
157 p[0].boldAccountName));
160 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
165 protected void onProgressUpdate(TransactionLoaderStep... values) {
166 super.onProgressUpdate(values);
167 TransactionLoaderStep step = values[0];
168 TransactionRowHolder holder = step.getHolder();
170 switch (step.getStep()) {
172 holder.tvDescription.setText(step.getTransaction().getDescription());
174 if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
175 else holder.row.setBackgroundColor(Colors.tableRowLightBG);
179 int rowIndex = step.getAccountPosition();
180 Context ctx = holder.row.getContext();
181 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
182 TextView accName, accAmount;
184 row = new LinearLayout(ctx);
185 row.setLayoutParams(new LinearLayout.LayoutParams(
186 LinearLayout.LayoutParams.MATCH_PARENT,
187 LinearLayout.LayoutParams.WRAP_CONTENT));
188 row.setGravity(Gravity.CENTER_VERTICAL);
189 row.setOrientation(LinearLayout.HORIZONTAL);
190 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
191 accName = new AppCompatTextView(ctx);
192 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
193 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
194 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
195 row.addView(accName);
196 accAmount = new AppCompatTextView(ctx);
197 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
198 LinearLayout.LayoutParams.WRAP_CONTENT,
199 LinearLayout.LayoutParams.WRAP_CONTENT);
201 accAmount.setLayoutParams(llp);
202 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
203 accAmount.setMinWidth(dp2px(ctx, 60));
204 row.addView(accAmount);
205 holder.tableAccounts.addView(row);
208 accName = (TextView) row.getChildAt(0);
209 accAmount = (TextView) row.getChildAt(1);
211 LedgerTransactionAccount acc = step.getAccount();
214 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
215 // acc.getAccountName(), acc.getAmount()));
217 String boldAccountName = step.getBoldAccountName();
218 if ((boldAccountName != null) &&
219 acc.getAccountName().startsWith(boldAccountName))
221 accName.setTextColor(Colors.accent);
222 accAmount.setTextColor(Colors.accent);
224 SpannableString ss = new SpannableString(acc.getAccountName());
225 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
226 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
230 accName.setTextColor(Colors.defaultTextColor);
231 accAmount.setTextColor(Colors.defaultTextColor);
232 accName.setText(acc.getAccountName());
234 accAmount.setText(acc.toString());
238 int accCount = step.getAccountCount();
239 if (holder.tableAccounts.getChildCount() > accCount) {
240 holder.tableAccounts.removeViews(accCount,
241 holder.tableAccounts.getChildCount() - accCount);
244 // Log.d("transactions",
245 // String.format("Position %d fill done", step.getPosition()));
250 private class TransactionLoaderParams {
251 LedgerTransaction transaction;
252 TransactionRowHolder holder;
254 String boldAccountName;
256 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
257 int position, String boldAccountName, boolean odd) {
258 this.transaction = transaction;
259 this.holder = holder;
260 this.position = position;
261 this.boldAccountName = boldAccountName;