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