2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. 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.Globals;
42 import net.ktnx.mobileledger.utils.MLDB;
44 import java.text.DateFormat;
45 import java.util.Date;
47 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
49 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
50 private String boldAccountName;
51 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
52 TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
54 // in a race when transaction value is reduced, but the model hasn't been notified yet
55 // the view will disappear when the notifications reaches the model, so by simply omitting
56 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
58 if (item == null) return;
60 switch (item.getType()) {
62 holder.vTransaction.setVisibility(View.VISIBLE);
63 holder.vDelimiter.setVisibility(View.GONE);
64 holder.vTrailer.setVisibility(View.GONE);
65 LedgerTransaction tr = item.getTransaction();
67 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
68 // tr.getAccounts().size()));
70 TransactionLoader loader = new TransactionLoader();
71 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
74 // WORKAROUND what seems to be a bug in CardHolder somewhere
75 // when a view that was previously holding a delimiter is re-purposed
76 // occasionally it stays too short (not high enough)
77 holder.vTransaction.measure(View.MeasureSpec
78 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
79 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
82 Date date = item.getDate();
83 holder.vTransaction.setVisibility(View.GONE);
84 holder.vTrailer.setVisibility(View.GONE);
85 holder.vDelimiter.setVisibility(View.VISIBLE);
86 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
87 if (item.isMonthShown()) {
88 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
89 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
90 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
91 holder.vDelimiterLine.setVisibility(View.GONE);
92 holder.vDelimiterThick.setVisibility(View.VISIBLE);
95 holder.tvDelimiterMonth.setVisibility(View.GONE);
96 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
97 holder.vDelimiterLine.setVisibility(View.VISIBLE);
98 holder.vDelimiterThick.setVisibility(View.GONE);
102 holder.vTransaction.setVisibility(View.GONE);
103 holder.vTrailer.setVisibility(View.VISIBLE);
104 holder.vDelimiter.setVisibility(View.GONE);
112 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
113 // Log.d("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 TransactionListViewModel.getTransactionCount() + 1;
123 public void setBoldAccountName(String boldAccountName) {
124 this.boldAccountName = boldAccountName;
126 public void resetBoldAccountName() {
127 this.boldAccountName = null;
130 enum LoaderStep {HEAD, ACCOUNTS, DONE}
132 private static class TransactionLoader
133 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
135 protected Void doInBackground(TransactionLoaderParams... p) {
136 LedgerTransaction tr = p[0].transaction;
137 boolean odd = p[0].odd;
139 SQLiteDatabase db = MLDB.getReadableDatabase();
142 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
145 for (LedgerTransactionAccount acc : tr.getAccounts()) {
146 // Log.d(c.getAccountName(), acc.getAmount()));
147 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
148 p[0].boldAccountName));
151 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
156 protected void onProgressUpdate(TransactionLoaderStep... values) {
157 super.onProgressUpdate(values);
158 TransactionLoaderStep step = values[0];
159 TransactionRowHolder holder = step.getHolder();
161 switch (step.getStep()) {
163 holder.tvDescription.setText(step.getTransaction().getDescription());
165 if (step.isOdd()) holder.row.setBackgroundColor(Globals.tableRowDarkBG);
166 else holder.row.setBackgroundColor(Globals.tableRowLightBG);
170 int rowIndex = step.getAccountPosition();
171 Context ctx = holder.row.getContext();
172 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
173 TextView accName, accAmount;
175 row = new LinearLayout(ctx);
176 row.setLayoutParams(new LinearLayout.LayoutParams(
177 LinearLayout.LayoutParams.MATCH_PARENT,
178 LinearLayout.LayoutParams.WRAP_CONTENT));
179 row.setGravity(Gravity.CENTER_VERTICAL);
180 row.setOrientation(LinearLayout.HORIZONTAL);
181 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
182 accName = new AppCompatTextView(ctx);
183 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
184 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
185 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
186 row.addView(accName);
187 accAmount = new AppCompatTextView(ctx);
188 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
189 LinearLayout.LayoutParams.WRAP_CONTENT,
190 LinearLayout.LayoutParams.WRAP_CONTENT);
192 accAmount.setLayoutParams(llp);
193 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
194 accAmount.setMinWidth(dp2px(ctx, 60));
195 row.addView(accAmount);
196 holder.tableAccounts.addView(row);
199 accName = (TextView) row.getChildAt(0);
200 accAmount = (TextView) row.getChildAt(1);
202 LedgerTransactionAccount acc = step.getAccount();
205 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
206 // acc.getAccountName(), acc.getAmount()));
208 String boldAccountName = step.getBoldAccountName();
209 if ((boldAccountName != null) &&
210 acc.getAccountName().startsWith(boldAccountName))
212 accName.setTextColor(Globals.primaryDark);
213 accAmount.setTextColor(Globals.primaryDark);
215 SpannableString ss = new SpannableString(acc.getAccountName());
216 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
217 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
221 accName.setTextColor(Globals.defaultTextColor);
222 accAmount.setTextColor(Globals.defaultTextColor);
223 accName.setText(acc.getAccountName());
225 accAmount.setText(acc.toString());
229 int accCount = step.getAccountCount();
230 if (holder.tableAccounts.getChildCount() > accCount) {
231 holder.tableAccounts.removeViews(accCount,
232 holder.tableAccounts.getChildCount() - accCount);
235 // Log.d("transactions",
236 // String.format("Position %d fill done", step.getPosition()));
241 private class TransactionLoaderParams {
242 LedgerTransaction transaction;
243 TransactionRowHolder holder;
245 String boldAccountName;
247 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
248 int position, String boldAccountName, boolean odd) {
249 this.transaction = transaction;
250 this.holder = holder;
251 this.position = position;
252 this.boldAccountName = boldAccountName;