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.support.annotation.NonNull;
25 import android.support.v7.widget.AppCompatTextView;
26 import android.support.v7.widget.RecyclerView;
27 import android.text.Spannable;
28 import android.text.SpannableString;
29 import android.text.style.StyleSpan;
30 import android.view.Gravity;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.model.LedgerTransaction;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.TransactionListItem;
41 import net.ktnx.mobileledger.utils.Colors;
42 import net.ktnx.mobileledger.utils.Globals;
43 import net.ktnx.mobileledger.utils.MLDB;
45 import java.text.DateFormat;
46 import java.util.Date;
47 import java.util.GregorianCalendar;
48 import java.util.TimeZone;
50 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
52 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
53 private String boldAccountName;
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
61 if (item == null) return;
63 switch (item.getType()) {
65 holder.vTransaction.setVisibility(View.VISIBLE);
66 holder.vDelimiter.setVisibility(View.GONE);
67 holder.vTrailer.setVisibility(View.GONE);
68 LedgerTransaction tr = item.getTransaction();
70 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
71 // tr.getAccounts().size()));
73 TransactionLoader loader = new TransactionLoader();
74 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
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(View.MeasureSpec
81 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
82 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
85 Date date = item.getDate();
86 holder.vTransaction.setVisibility(View.GONE);
87 holder.vTrailer.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);
108 holder.vTransaction.setVisibility(View.GONE);
109 holder.vTrailer.setVisibility(View.VISIBLE);
110 holder.vDelimiter.setVisibility(View.GONE);
118 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
119 // Log.d("perf", "onCreateViewHolder called");
120 View row = LayoutInflater.from(parent.getContext())
121 .inflate(R.layout.transaction_list_row, parent, false);
122 return new TransactionRowHolder(row);
126 public int getItemCount() {
127 return TransactionListViewModel.getTransactionCount() + 1;
129 public void setBoldAccountName(String boldAccountName) {
130 this.boldAccountName = boldAccountName;
132 public void resetBoldAccountName() {
133 this.boldAccountName = null;
136 enum LoaderStep {HEAD, ACCOUNTS, DONE}
138 private static class TransactionLoader
139 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
141 protected Void doInBackground(TransactionLoaderParams... p) {
142 LedgerTransaction tr = p[0].transaction;
143 boolean odd = p[0].odd;
145 SQLiteDatabase db = MLDB.getReadableDatabase();
148 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
151 for (LedgerTransactionAccount acc : tr.getAccounts()) {
152 // Log.d(c.getAccountName(), acc.getAmount()));
153 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
154 p[0].boldAccountName));
157 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
162 protected void onProgressUpdate(TransactionLoaderStep... values) {
163 super.onProgressUpdate(values);
164 TransactionLoaderStep step = values[0];
165 TransactionRowHolder holder = step.getHolder();
167 switch (step.getStep()) {
169 holder.tvDescription.setText(step.getTransaction().getDescription());
171 if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
172 else holder.row.setBackgroundColor(Colors.tableRowLightBG);
176 int rowIndex = step.getAccountPosition();
177 Context ctx = holder.row.getContext();
178 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
179 TextView accName, accAmount;
181 row = new LinearLayout(ctx);
182 row.setLayoutParams(new LinearLayout.LayoutParams(
183 LinearLayout.LayoutParams.MATCH_PARENT,
184 LinearLayout.LayoutParams.WRAP_CONTENT));
185 row.setGravity(Gravity.CENTER_VERTICAL);
186 row.setOrientation(LinearLayout.HORIZONTAL);
187 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
188 accName = new AppCompatTextView(ctx);
189 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
190 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
191 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
192 row.addView(accName);
193 accAmount = new AppCompatTextView(ctx);
194 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
195 LinearLayout.LayoutParams.WRAP_CONTENT,
196 LinearLayout.LayoutParams.WRAP_CONTENT);
198 accAmount.setLayoutParams(llp);
199 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
200 accAmount.setMinWidth(dp2px(ctx, 60));
201 row.addView(accAmount);
202 holder.tableAccounts.addView(row);
205 accName = (TextView) row.getChildAt(0);
206 accAmount = (TextView) row.getChildAt(1);
208 LedgerTransactionAccount acc = step.getAccount();
211 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
212 // acc.getAccountName(), acc.getAmount()));
214 String boldAccountName = step.getBoldAccountName();
215 if ((boldAccountName != null) &&
216 acc.getAccountName().startsWith(boldAccountName))
218 accName.setTextColor(Colors.accent);
219 accAmount.setTextColor(Colors.accent);
221 SpannableString ss = new SpannableString(acc.getAccountName());
222 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
223 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
227 accName.setTextColor(Colors.defaultTextColor);
228 accAmount.setTextColor(Colors.defaultTextColor);
229 accName.setText(acc.getAccountName());
231 accAmount.setText(acc.toString());
235 int accCount = step.getAccountCount();
236 if (holder.tableAccounts.getChildCount() > accCount) {
237 holder.tableAccounts.removeViews(accCount,
238 holder.tableAccounts.getChildCount() - accCount);
241 // Log.d("transactions",
242 // String.format("Position %d fill done", step.getPosition()));
247 private class TransactionLoaderParams {
248 LedgerTransaction transaction;
249 TransactionRowHolder holder;
251 String boldAccountName;
253 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
254 int position, String boldAccountName, boolean odd) {
255 this.transaction = transaction;
256 this.holder = holder;
257 this.position = position;
258 this.boldAccountName = boldAccountName;