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.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.utils.Globals;
38 import net.ktnx.mobileledger.utils.MLDB;
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
42 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
43 private String boldAccountName;
44 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
45 LedgerTransaction tr = TransactionListViewModel.getTransaction(position);
46 // in a race when transaction value is reduced, but the model hasn't been notified yet
47 // the view will disappear when the notifications reaches the model, so by simply omitting
48 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
50 if (tr == null) return;
52 // Log.d("transactions", String.format("Filling position %d with %d accounts", position,
53 // tr.getAccounts().size()));
55 TransactionLoader loader = new TransactionLoader();
56 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName));
61 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
62 // Log.d("perf", "onCreateViewHolder called");
63 View row = LayoutInflater.from(parent.getContext())
64 .inflate(R.layout.transaction_list_row, parent, false);
65 return new TransactionRowHolder(row);
69 public int getItemCount() {
70 return TransactionListViewModel.getTransactionCount();
72 public void setBoldAccountName(String boldAccountName) {
73 this.boldAccountName = boldAccountName;
75 public void resetBoldAccountName() {
76 this.boldAccountName = null;
79 enum LoaderStep {HEAD, ACCOUNTS, DONE}
81 private static class TransactionLoader
82 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
84 protected Void doInBackground(TransactionLoaderParams... p) {
85 LedgerTransaction tr = p[0].transaction;
87 SQLiteDatabase db = MLDB.getReadableDatabase();
90 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
93 for (LedgerTransactionAccount acc : tr.getAccounts()) {
94 // Log.d(c.getAccountName(), acc.getAmount()));
95 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
96 p[0].boldAccountName));
99 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
104 protected void onProgressUpdate(TransactionLoaderStep... values) {
105 super.onProgressUpdate(values);
106 TransactionLoaderStep step = values[0];
107 TransactionRowHolder holder = step.getHolder();
109 switch (step.getStep()) {
111 holder.tvDescription.setText(step.getTransaction().getDescription());
112 holder.tvDate.setText(step.getTransaction().getDate());
114 if (step.getPosition() % 2 == 0) {
115 holder.row.setBackgroundColor(Globals.table_row_even_bg);
118 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
123 int rowIndex = step.getAccountPosition();
124 Context ctx = holder.row.getContext();
125 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
126 TextView accName, accAmount;
128 row = new LinearLayout(ctx);
129 row.setLayoutParams(new LinearLayout.LayoutParams(
130 LinearLayout.LayoutParams.MATCH_PARENT,
131 LinearLayout.LayoutParams.WRAP_CONTENT));
132 row.setGravity(Gravity.CENTER_VERTICAL);
133 row.setOrientation(LinearLayout.HORIZONTAL);
134 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
135 accName = new AppCompatTextView(ctx);
136 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
137 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
138 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
139 row.addView(accName);
140 accAmount = new AppCompatTextView(ctx);
141 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
142 LinearLayout.LayoutParams.WRAP_CONTENT,
143 LinearLayout.LayoutParams.WRAP_CONTENT);
145 accAmount.setLayoutParams(llp);
146 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
147 accAmount.setMinWidth(dp2px(ctx, 60));
148 row.addView(accAmount);
149 holder.tableAccounts.addView(row);
152 accName = (TextView) row.getChildAt(0);
153 accAmount = (TextView) row.getChildAt(1);
155 LedgerTransactionAccount acc = step.getAccount();
157 accName.setText(acc.getAccountName());
158 accAmount.setText(acc.toString());
160 // Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
161 // acc.getAccountName(), acc.getAmount()));
163 String boldAccountName = step.getBoldAccountName();
164 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
165 accName.setTypeface(null, Typeface.BOLD);
166 accAmount.setTypeface(null, Typeface.BOLD);
167 accName.setTextColor(Globals.primaryDark);
168 accAmount.setTextColor(Globals.primaryDark);
171 accName.setTypeface(null, Typeface.NORMAL);
172 accAmount.setTypeface(null, Typeface.NORMAL);
173 accName.setTextColor(Globals.defaultTextColor);
174 accAmount.setTextColor(Globals.defaultTextColor);
179 int accCount = step.getAccountCount();
180 if (holder.tableAccounts.getChildCount() > accCount) {
181 holder.tableAccounts.removeViews(accCount,
182 holder.tableAccounts.getChildCount() - accCount);
185 // Log.d("transactions",
186 // String.format("Position %d fill done", step.getPosition()));
191 private class TransactionLoaderParams {
192 LedgerTransaction transaction;
193 TransactionRowHolder holder;
195 String boldAccountName;
196 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
197 int position, String boldAccountName) {
198 this.transaction = transaction;
199 this.holder = holder;
200 this.position = position;
201 this.boldAccountName = boldAccountName;