2 * Copyright © 2018 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", position));
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 try (SQLiteDatabase db = MLDB.getReadableDatabase()) {
90 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
93 for (LedgerTransactionAccount acc : tr.getAccounts()) {
94 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
95 p[0].boldAccountName));
98 publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
103 protected void onProgressUpdate(TransactionLoaderStep... values) {
104 super.onProgressUpdate(values);
105 TransactionLoaderStep step = values[0];
106 TransactionRowHolder holder = step.getHolder();
108 switch (step.getStep()) {
110 holder.tvDescription.setText(step.getTransaction().getDescription());
111 holder.tvDate.setText(step.getTransaction().getDate());
113 if (step.getPosition() % 2 == 0) {
114 holder.row.setBackgroundColor(Globals.table_row_even_bg);
117 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
122 int rowIndex = step.getAccountPosition();
123 Context ctx = holder.row.getContext();
124 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
125 TextView accName, accAmount;
127 row = new LinearLayout(ctx);
128 row.setLayoutParams(new LinearLayout.LayoutParams(
129 LinearLayout.LayoutParams.MATCH_PARENT,
130 LinearLayout.LayoutParams.WRAP_CONTENT));
131 row.setGravity(Gravity.CENTER_VERTICAL);
132 row.setOrientation(LinearLayout.HORIZONTAL);
133 row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
134 accName = new AppCompatTextView(ctx);
135 accName.setLayoutParams(new LinearLayout.LayoutParams(0,
136 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
137 accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
138 row.addView(accName);
139 accAmount = new AppCompatTextView(ctx);
140 LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
141 LinearLayout.LayoutParams.WRAP_CONTENT,
142 LinearLayout.LayoutParams.WRAP_CONTENT);
144 accAmount.setLayoutParams(llp);
145 accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
146 accAmount.setMinWidth(dp2px(ctx, 60));
147 row.addView(accAmount);
148 holder.tableAccounts.addView(row);
151 accName = (TextView) row.getChildAt(0);
152 accAmount = (TextView) row.getChildAt(1);
154 LedgerTransactionAccount acc = step.getAccount();
156 accName.setText(acc.getAccountName());
157 accAmount.setText(acc.toString());
159 String boldAccountName = step.getBoldAccountName();
160 if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
161 accName.setTypeface(null, Typeface.BOLD);
162 accAmount.setTypeface(null, Typeface.BOLD);
163 accName.setTextColor(Globals.primaryDark);
164 accAmount.setTextColor(Globals.primaryDark);
167 accName.setTypeface(null, Typeface.NORMAL);
168 accAmount.setTypeface(null, Typeface.NORMAL);
169 accName.setTextColor(Globals.defaultTextColor);
170 accAmount.setTextColor(Globals.defaultTextColor);
175 int accCount = step.getAccountCount();
176 if (holder.tableAccounts.getChildCount() > accCount) {
177 holder.tableAccounts.removeViews(accCount,
178 holder.tableAccounts.getChildCount() - accCount);
181 Log.d("transactions",
182 String.format("Position %d fill done", step.getPosition()));
187 private class TransactionLoaderParams {
188 LedgerTransaction transaction;
189 TransactionRowHolder holder;
191 String boldAccountName;
192 TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
193 int position, String boldAccountName) {
194 this.transaction = transaction;
195 this.holder = holder;
196 this.position = position;
197 this.boldAccountName = boldAccountName;