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;
48 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
50 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
51 private String boldAccountName;
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
59 if (item == null) return;
61 switch (item.getType()) {
63 holder.vTransaction.setVisibility(View.VISIBLE);
64 holder.vDelimiter.setVisibility(View.GONE);
65 holder.vTrailer.setVisibility(View.GONE);
66 LedgerTransaction tr = item.getTransaction();
68 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
69 // tr.getAccounts().size()));
71 TransactionLoader loader = new TransactionLoader();
72 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
75 // WORKAROUND what seems to be a bug in CardHolder somewhere
76 // when a view that was previously holding a delimiter is re-purposed
77 // occasionally it stays too short (not high enough)
78 holder.vTransaction.measure(View.MeasureSpec
79 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
80 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
83 Date date = item.getDate();
84 holder.vTransaction.setVisibility(View.GONE);
85 holder.vTrailer.setVisibility(View.GONE);
86 holder.vDelimiter.setVisibility(View.VISIBLE);
87 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
88 if (item.isMonthShown()) {
89 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
90 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
91 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
92 holder.vDelimiterLine.setVisibility(View.GONE);
93 holder.vDelimiterThick.setVisibility(View.VISIBLE);
96 holder.tvDelimiterMonth.setVisibility(View.GONE);
97 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
98 holder.vDelimiterLine.setVisibility(View.VISIBLE);
99 holder.vDelimiterThick.setVisibility(View.GONE);
103 holder.vTransaction.setVisibility(View.GONE);
104 holder.vTrailer.setVisibility(View.VISIBLE);
105 holder.vDelimiter.setVisibility(View.GONE);
113 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
114 // Log.d("perf", "onCreateViewHolder called");
115 View row = LayoutInflater.from(parent.getContext())
116 .inflate(R.layout.transaction_list_row, parent, false);
117 return new TransactionRowHolder(row);
121 public int getItemCount() {
122 return TransactionListViewModel.getTransactionCount() + 1;
124 public void setBoldAccountName(String boldAccountName) {
125 this.boldAccountName = boldAccountName;
127 public void resetBoldAccountName() {
128 this.boldAccountName = null;
131 enum LoaderStep {HEAD, ACCOUNTS, DONE}
133 private static class TransactionLoader
134 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
136 protected Void doInBackground(TransactionLoaderParams... p) {
137 LedgerTransaction tr = p[0].transaction;
138 boolean odd = p[0].odd;
140 SQLiteDatabase db = MLDB.getReadableDatabase();
143 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
146 for (LedgerTransactionAccount acc : tr.getAccounts()) {
147 // Log.d(c.getAccountName(), acc.getAmount()));
148 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
149 p[0].boldAccountName));
152 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
157 protected void onProgressUpdate(TransactionLoaderStep... values) {
158 super.onProgressUpdate(values);
159 TransactionLoaderStep step = values[0];
160 TransactionRowHolder holder = step.getHolder();
162 switch (step.getStep()) {
164 holder.tvDescription.setText(step.getTransaction().getDescription());
166 if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
167 else holder.row.setBackgroundColor(Colors.tableRowLightBG);
171 int rowIndex = step.getAccountPosition();
172 Context ctx = holder.row.getContext();
173 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
174 TextView accName, accAmount;
176 row = new LinearLayout(ctx);
177 row.setLayoutParams(new LinearLayout.LayoutParams(
178 LinearLayout.LayoutParams.MATCH_PARENT,
179 LinearLayout.LayoutParams.WRAP_CONTENT));
180 row.setGravity(Gravity.CENTER_VERTICAL);
181 row.setOrientation(LinearLayout.HORIZONTAL);
182 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
183 accName = new AppCompatTextView(ctx);
184 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
185 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
186 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
187 row.addView(accName);
188 accAmount = new AppCompatTextView(ctx);
189 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
190 LinearLayout.LayoutParams.WRAP_CONTENT,
191 LinearLayout.LayoutParams.WRAP_CONTENT);
193 accAmount.setLayoutParams(llp);
194 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
195 accAmount.setMinWidth(dp2px(ctx, 60));
196 row.addView(accAmount);
197 holder.tableAccounts.addView(row);
200 accName = (TextView) row.getChildAt(0);
201 accAmount = (TextView) row.getChildAt(1);
203 LedgerTransactionAccount acc = step.getAccount();
206 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
207 // acc.getAccountName(), acc.getAmount()));
209 String boldAccountName = step.getBoldAccountName();
210 if ((boldAccountName != null) &&
211 acc.getAccountName().startsWith(boldAccountName))
213 accName.setTextColor(Colors.accent);
214 accAmount.setTextColor(Colors.accent);
216 SpannableString ss = new SpannableString(acc.getAccountName());
217 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
218 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
222 accName.setTextColor(Colors.defaultTextColor);
223 accAmount.setTextColor(Colors.defaultTextColor);
224 accName.setText(acc.getAccountName());
226 accAmount.setText(acc.toString());
230 int accCount = step.getAccountCount();
231 if (holder.tableAccounts.getChildCount() > accCount) {
232 holder.tableAccounts.removeViews(accCount,
233 holder.tableAccounts.getChildCount() - accCount);
236 // Log.d("transactions",
237 // String.format("Position %d fill done", step.getPosition()));
242 private class TransactionLoaderParams {
243 LedgerTransaction transaction;
244 TransactionRowHolder holder;
246 String boldAccountName;
248 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
249 int position, String boldAccountName, boolean odd) {
250 this.transaction = transaction;
251 this.holder = holder;
252 this.position = position;
253 this.boldAccountName = boldAccountName;