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.LedgerTransaction;
36 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
37 import net.ktnx.mobileledger.model.TransactionListItem;
38 import net.ktnx.mobileledger.utils.Colors;
39 import net.ktnx.mobileledger.utils.Globals;
40 import net.ktnx.mobileledger.utils.MLDB;
42 import java.text.DateFormat;
43 import java.util.Date;
44 import java.util.GregorianCalendar;
45 import java.util.TimeZone;
47 import androidx.annotation.NonNull;
48 import androidx.appcompat.widget.AppCompatTextView;
49 import androidx.recyclerview.widget.RecyclerView;
51 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
53 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
54 private String boldAccountName;
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 holder.vTrailer.setVisibility(View.GONE);
69 LedgerTransaction tr = item.getTransaction();
71 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
72 // tr.getAccounts().size()));
74 TransactionLoader loader = new TransactionLoader();
75 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
76 new TransactionLoaderParams(tr, holder, position, boldAccountName,
79 // WORKAROUND what seems to be a bug in CardHolder somewhere
80 // when a view that was previously holding a delimiter is re-purposed
81 // occasionally it stays too short (not high enough)
82 holder.vTransaction.measure(View.MeasureSpec
83 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
84 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
87 Date date = item.getDate();
88 holder.vTransaction.setVisibility(View.GONE);
89 holder.vTrailer.setVisibility(View.GONE);
90 holder.vDelimiter.setVisibility(View.VISIBLE);
91 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
92 if (item.isMonthShown()) {
93 GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
95 holder.tvDelimiterMonth
96 .setText(Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
97 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
98 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
99 holder.vDelimiterLine.setVisibility(View.GONE);
100 holder.vDelimiterThick.setVisibility(View.VISIBLE);
103 holder.tvDelimiterMonth.setVisibility(View.GONE);
104 // holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
105 holder.vDelimiterLine.setVisibility(View.VISIBLE);
106 holder.vDelimiterThick.setVisibility(View.GONE);
110 holder.vTransaction.setVisibility(View.GONE);
111 holder.vTrailer.setVisibility(View.VISIBLE);
112 holder.vDelimiter.setVisibility(View.GONE);
120 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
121 // Log.d("perf", "onCreateViewHolder called");
122 View row = LayoutInflater.from(parent.getContext())
123 .inflate(R.layout.transaction_list_row, parent, false);
124 return new TransactionRowHolder(row);
128 public int getItemCount() {
129 return TransactionListViewModel.getTransactionCount() + 1;
131 public void setBoldAccountName(String boldAccountName) {
132 this.boldAccountName = boldAccountName;
134 public void resetBoldAccountName() {
135 this.boldAccountName = null;
138 enum LoaderStep {HEAD, ACCOUNTS, DONE}
140 private static class TransactionLoader
141 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
143 protected Void doInBackground(TransactionLoaderParams... p) {
144 LedgerTransaction tr = p[0].transaction;
145 boolean odd = p[0].odd;
147 SQLiteDatabase db = MLDB.getDatabase();
150 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
153 for (LedgerTransactionAccount acc : tr.getAccounts()) {
154 // Log.d(c.getAccountName(), acc.getAmount()));
155 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
156 p[0].boldAccountName));
159 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
164 protected void onProgressUpdate(TransactionLoaderStep... values) {
165 super.onProgressUpdate(values);
166 TransactionLoaderStep step = values[0];
167 TransactionRowHolder holder = step.getHolder();
169 switch (step.getStep()) {
171 holder.tvDescription.setText(step.getTransaction().getDescription());
173 if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
174 else holder.row.setBackgroundColor(Colors.tableRowLightBG);
178 int rowIndex = step.getAccountPosition();
179 Context ctx = holder.row.getContext();
180 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
181 TextView accName, accAmount;
183 row = new LinearLayout(ctx);
184 row.setLayoutParams(new LinearLayout.LayoutParams(
185 LinearLayout.LayoutParams.MATCH_PARENT,
186 LinearLayout.LayoutParams.WRAP_CONTENT));
187 row.setGravity(Gravity.CENTER_VERTICAL);
188 row.setOrientation(LinearLayout.HORIZONTAL);
189 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
190 accName = new AppCompatTextView(ctx);
191 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
192 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
193 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
194 row.addView(accName);
195 accAmount = new AppCompatTextView(ctx);
196 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
197 LinearLayout.LayoutParams.WRAP_CONTENT,
198 LinearLayout.LayoutParams.WRAP_CONTENT);
200 accAmount.setLayoutParams(llp);
201 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
202 accAmount.setMinWidth(dp2px(ctx, 60));
203 row.addView(accAmount);
204 holder.tableAccounts.addView(row);
207 accName = (TextView) row.getChildAt(0);
208 accAmount = (TextView) row.getChildAt(1);
210 LedgerTransactionAccount acc = step.getAccount();
213 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
214 // acc.getAccountName(), acc.getAmount()));
216 String boldAccountName = step.getBoldAccountName();
217 if ((boldAccountName != null) &&
218 acc.getAccountName().startsWith(boldAccountName))
220 accName.setTextColor(Colors.accent);
221 accAmount.setTextColor(Colors.accent);
223 SpannableString ss = new SpannableString(acc.getAccountName());
224 ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
225 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
229 accName.setTextColor(Colors.defaultTextColor);
230 accAmount.setTextColor(Colors.defaultTextColor);
231 accName.setText(acc.getAccountName());
233 accAmount.setText(acc.toString());
237 int accCount = step.getAccountCount();
238 if (holder.tableAccounts.getChildCount() > accCount) {
239 holder.tableAccounts.removeViews(accCount,
240 holder.tableAccounts.getChildCount() - accCount);
243 // Log.d("transactions",
244 // String.format("Position %d fill done", step.getPosition()));
249 private class TransactionLoaderParams {
250 LedgerTransaction transaction;
251 TransactionRowHolder holder;
253 String boldAccountName;
255 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
256 int position, String boldAccountName, boolean odd) {
257 this.transaction = transaction;
258 this.holder = holder;
259 this.position = position;
260 this.boldAccountName = boldAccountName;