]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
replace dates in transaction list items with delimiters between items in different...
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.transaction_list;
19
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;
33
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.Globals;
39 import net.ktnx.mobileledger.utils.MLDB;
40
41 import java.text.DateFormat;
42 import java.util.Date;
43
44 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
45
46 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
47     private String boldAccountName;
48     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
49         TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
50
51         if (item.getType() == TransactionListItem.Type.TRANSACTION) {
52             holder.vTransaction.setVisibility(View.VISIBLE);
53             holder.vDelimiter.setVisibility(View.GONE);
54             LedgerTransaction tr = item.getTransaction();
55             // in a race when transaction value is reduced, but the model hasn't been notified yet
56             // the view will disappear when the notifications reaches the model, so by simply omitting
57             // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
58             // a bit longer
59             if (tr == null) return;
60
61             LedgerTransaction previous = null;
62             TransactionListItem previousItem = null;
63             if (position > 0)
64                 previousItem = TransactionListViewModel.getTransactionListItem(position - 1);
65
66 //        Log.d("transactions", String.format("Filling position %d with %d accounts", position,
67 //                tr.getAccounts().size()));
68
69             TransactionLoader loader = new TransactionLoader();
70             loader.execute(
71                     new TransactionLoaderParams(tr, previous, holder, position, boldAccountName));
72
73             // WORKAROUND what seems to be a bug in CardHolder somewhere
74             // when a view that was previously holding a delimiter is re-purposed
75             // occasionally it stays too short (not high enough)
76             holder.vTransaction.measure(View.MeasureSpec
77                             .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
78                     View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
79         }
80         else {
81             Date date = item.getDate();
82             holder.vTransaction.setVisibility(View.GONE);
83             holder.vDelimiter.setVisibility(View.VISIBLE);
84             holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
85             holder.tvDelimiterMonth
86                     .setText(item.isMonthShown() ? Globals.monthNames[date.getMonth()] : "");
87         }
88     }
89
90     @NonNull
91     @Override
92     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
93 //        Log.d("perf", "onCreateViewHolder called");
94         View row = LayoutInflater.from(parent.getContext())
95                 .inflate(R.layout.transaction_list_row, parent, false);
96         return new TransactionRowHolder(row);
97     }
98
99     @Override
100     public int getItemCount() {
101         return TransactionListViewModel.getTransactionCount();
102     }
103     public void setBoldAccountName(String boldAccountName) {
104         this.boldAccountName = boldAccountName;
105     }
106     public void resetBoldAccountName() {
107         this.boldAccountName = null;
108     }
109
110     enum LoaderStep {HEAD, ACCOUNTS, DONE}
111
112     private static class TransactionLoader
113             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
114         @Override
115         protected Void doInBackground(TransactionLoaderParams... p) {
116             LedgerTransaction tr = p[0].transaction;
117             LedgerTransaction previous = p[0].previousTransaction;
118
119             SQLiteDatabase db = MLDB.getReadableDatabase();
120             tr.loadData(db);
121
122             boolean showDate;
123             if (previous == null) showDate = true;
124             else {
125                 previous.loadData(db);
126                 showDate = !previous.getDate().equals(tr.getDate());
127             }
128             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, showDate));
129
130             int rowIndex = 0;
131             for (LedgerTransactionAccount acc : tr.getAccounts()) {
132 //                Log.d(c.getAccountName(), acc.getAmount()));
133                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
134                         p[0].boldAccountName));
135             }
136
137             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
138
139             return null;
140         }
141         @Override
142         protected void onProgressUpdate(TransactionLoaderStep... values) {
143             super.onProgressUpdate(values);
144             TransactionLoaderStep step = values[0];
145             TransactionRowHolder holder = step.getHolder();
146
147             switch (step.getStep()) {
148                 case HEAD:
149                     holder.tvDescription.setText(step.getTransaction().getDescription());
150
151                     if (step.getPosition() % 2 == 0) {
152                         holder.row.setBackgroundColor(Globals.tableRowEvenBG);
153                     }
154                     else {
155                         holder.row.setBackgroundColor(Globals.tableRowOddBG);
156                     }
157
158                     break;
159                 case ACCOUNTS:
160                     int rowIndex = step.getAccountPosition();
161                     Context ctx = holder.row.getContext();
162                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
163                     TextView accName, accAmount;
164                     if (row == null) {
165                         row = new LinearLayout(ctx);
166                         row.setLayoutParams(new LinearLayout.LayoutParams(
167                                 LinearLayout.LayoutParams.MATCH_PARENT,
168                                 LinearLayout.LayoutParams.WRAP_CONTENT));
169                         row.setGravity(Gravity.CENTER_VERTICAL);
170                         row.setOrientation(LinearLayout.HORIZONTAL);
171                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
172                         accName = new AppCompatTextView(ctx);
173                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
174                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
175                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
176                         row.addView(accName);
177                         accAmount = new AppCompatTextView(ctx);
178                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
179                                 LinearLayout.LayoutParams.WRAP_CONTENT,
180                                 LinearLayout.LayoutParams.WRAP_CONTENT);
181                         llp.setMarginEnd(0);
182                         accAmount.setLayoutParams(llp);
183                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
184                         accAmount.setMinWidth(dp2px(ctx, 60));
185                         row.addView(accAmount);
186                         holder.tableAccounts.addView(row);
187                     }
188                     else {
189                         accName = (TextView) row.getChildAt(0);
190                         accAmount = (TextView) row.getChildAt(1);
191                     }
192                     LedgerTransactionAccount acc = step.getAccount();
193
194                     accName.setText(acc.getAccountName());
195                     accAmount.setText(acc.toString());
196
197 //                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
198 //                            acc.getAccountName(), acc.getAmount()));
199
200                     String boldAccountName = step.getBoldAccountName();
201                     if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
202                         accName.setTypeface(null, Typeface.BOLD);
203                         accAmount.setTypeface(null, Typeface.BOLD);
204                         accName.setTextColor(Globals.primaryDark);
205                         accAmount.setTextColor(Globals.primaryDark);
206                     }
207                     else {
208                         accName.setTypeface(null, Typeface.NORMAL);
209                         accAmount.setTypeface(null, Typeface.NORMAL);
210                         accName.setTextColor(Globals.defaultTextColor);
211                         accAmount.setTextColor(Globals.defaultTextColor);
212                     }
213
214                     break;
215                 case DONE:
216                     int accCount = step.getAccountCount();
217                     if (holder.tableAccounts.getChildCount() > accCount) {
218                         holder.tableAccounts.removeViews(accCount,
219                                 holder.tableAccounts.getChildCount() - accCount);
220                     }
221
222 //                    Log.d("transactions",
223 //                            String.format("Position %d fill done", step.getPosition()));
224             }
225         }
226     }
227
228     private class TransactionLoaderParams {
229         LedgerTransaction transaction, previousTransaction;
230         TransactionRowHolder holder;
231         int position;
232         String boldAccountName;
233         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder, int position, String boldAccountName) {
234             this(transaction, null, holder, position, boldAccountName);
235         }
236         TransactionLoaderParams(LedgerTransaction transaction,
237                                 LedgerTransaction previousTransaction, TransactionRowHolder holder,
238                                 int position, String boldAccountName) {
239             this.transaction = transaction;
240             this.previousTransaction = previousTransaction;
241             this.holder = holder;
242             this.position = position;
243             this.boldAccountName = boldAccountName;
244         }
245     }
246 }