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.util.Log;
28 import android.view.Gravity;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.utils.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
41 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
43 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
44 private String boldAccountName;
45 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
46 LedgerTransaction tr = TransactionListViewModel.getTransaction(position);
47 // in a race when transaction value is reduced, but the model hasn't been notified yet
48 // the view will disappear when the notifications reaches the model, so by simply omitting
49 // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
51 if (tr == null) return;
53 Log.d("transactions", String.format("Filling position %d with %d accounts", position,
54 tr.getAccounts().size()));
56 TransactionLoader loader = new TransactionLoader();
57 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName));
62 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
63 Log.d("perf", "onCreateViewHolder called");
64 View row = LayoutInflater.from(parent.getContext())
65 .inflate(R.layout.transaction_list_row, parent, false);
66 return new TransactionRowHolder(row);
70 public int getItemCount() {
71 return TransactionListViewModel.getTransactionCount();
73 public void setBoldAccountName(String boldAccountName) {
74 this.boldAccountName = boldAccountName;
76 public void resetBoldAccountName() {
77 this.boldAccountName = null;
80 enum LoaderStep {HEAD, ACCOUNTS, DONE}
82 private static class TransactionLoader
83 extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
85 protected Void doInBackground(TransactionLoaderParams... p) {
86 LedgerTransaction tr = p[0].transaction;
88 SQLiteDatabase db = MLDB.getReadableDatabase();
91 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
94 for (LedgerTransactionAccount acc : tr.getAccounts()) {
95 Log.d("tmp", String.format("publishing tr %d acc %s %1.2f", tr.getId(),
96 acc.getAccountName(), acc.getAmount()));
97 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
98 p[0].boldAccountName));
101 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
106 protected void onProgressUpdate(TransactionLoaderStep... values) {
107 super.onProgressUpdate(values);
108 TransactionLoaderStep step = values[0];
109 TransactionRowHolder holder = step.getHolder();
111 switch (step.getStep()) {
113 holder.tvDescription.setText(step.getTransaction().getDescription());
114 holder.tvDate.setText(step.getTransaction().getDate());
116 if (step.getPosition() % 2 == 0) {
117 holder.row.setBackgroundColor(Globals.table_row_even_bg);
120 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
125 int rowIndex = step.getAccountPosition();
126 Context ctx = holder.row.getContext();
127 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
128 TextView accName, accAmount;
130 row = new LinearLayout(ctx);
131 row.setLayoutParams(new LinearLayout.LayoutParams(
132 LinearLayout.LayoutParams.MATCH_PARENT,
133 LinearLayout.LayoutParams.WRAP_CONTENT));
134 row.setGravity(Gravity.CENTER_VERTICAL);
135 row.setOrientation(LinearLayout.HORIZONTAL);
136 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
137 accName = new AppCompatTextView(ctx);
138 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
139 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
140 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
141 row.addView(accName);
142 accAmount = new AppCompatTextView(ctx);
143 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
144 LinearLayout.LayoutParams.WRAP_CONTENT,
145 LinearLayout.LayoutParams.WRAP_CONTENT);
147 accAmount.setLayoutParams(llp);
148 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
149 accAmount.setMinWidth(dp2px(ctx, 60));
150 row.addView(accAmount);
151 holder.tableAccounts.addView(row);
154 accName = (TextView) row.getChildAt(0);
155 accAmount = (TextView) row.getChildAt(1);
157 LedgerTransactionAccount acc = step.getAccount();
159 accName.setText(acc.getAccountName());
160 accAmount.setText(acc.toString());
162 Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
163 acc.getAccountName(), acc.getAmount()));
165 String boldAccountName = step.getBoldAccountName();
166 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
167 accName.setTypeface(null, Typeface.BOLD);
168 accAmount.setTypeface(null, Typeface.BOLD);
169 accName.setTextColor(Globals.primaryDark);
170 accAmount.setTextColor(Globals.primaryDark);
173 accName.setTypeface(null, Typeface.NORMAL);
174 accAmount.setTypeface(null, Typeface.NORMAL);
175 accName.setTextColor(Globals.defaultTextColor);
176 accAmount.setTextColor(Globals.defaultTextColor);
181 int accCount = step.getAccountCount();
182 if (holder.tableAccounts.getChildCount() > accCount) {
183 holder.tableAccounts.removeViews(accCount,
184 holder.tableAccounts.getChildCount() - accCount);
187 Log.d("transactions",
188 String.format("Position %d fill done", step.getPosition()));
193 private class TransactionLoaderParams {
194 LedgerTransaction transaction;
195 TransactionRowHolder holder;
197 String boldAccountName;
198 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
199 int position, String boldAccountName) {
200 this.transaction = transaction;
201 this.holder = holder;
202 this.position = position;
203 this.boldAccountName = boldAccountName;