]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
rework account list management to be fully asynchronous
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
1 /*
2  * Copyright © 2020 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.app.Activity;
21 import android.content.Context;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.graphics.Typeface;
24 import android.os.AsyncTask;
25 import android.text.Spannable;
26 import android.text.SpannableString;
27 import android.text.style.StyleSpan;
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 androidx.annotation.ColorInt;
35 import androidx.annotation.NonNull;
36 import androidx.recyclerview.widget.AsyncListDiffer;
37 import androidx.recyclerview.widget.DiffUtil;
38 import androidx.recyclerview.widget.RecyclerView;
39
40 import net.ktnx.mobileledger.App;
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.LedgerTransaction;
44 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
45 import net.ktnx.mobileledger.model.MobileLedgerProfile;
46 import net.ktnx.mobileledger.model.TransactionListItem;
47 import net.ktnx.mobileledger.utils.Colors;
48 import net.ktnx.mobileledger.utils.Globals;
49 import net.ktnx.mobileledger.utils.Misc;
50 import net.ktnx.mobileledger.utils.SimpleDate;
51
52 import java.text.DateFormat;
53 import java.util.GregorianCalendar;
54 import java.util.Locale;
55 import java.util.TimeZone;
56
57 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
58     private MobileLedgerProfile profile;
59     private AsyncListDiffer<TransactionListItem> listDiffer;
60     public TransactionListAdapter() {
61         super();
62         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TransactionListItem>() {
63             @Override
64             public boolean areItemsTheSame(@NonNull TransactionListItem oldItem,
65                                            @NonNull TransactionListItem newItem) {
66                 if (oldItem.getType() != newItem.getType())
67                     return false;
68                 switch (oldItem.getType()) {
69                     case DELIMITER:
70                         return (oldItem.getDate()
71                                        .equals(newItem.getDate()));
72                     case TRANSACTION:
73                         return oldItem.getTransaction()
74                                       .getId() == newItem.getTransaction()
75                                                          .getId();
76                     default:
77                         throw new IllegalStateException(
78                                 String.format(Locale.US, "Unexpected transaction item type %s",
79                                         oldItem.getType()));
80                 }
81             }
82             @Override
83             public boolean areContentsTheSame(@NonNull TransactionListItem oldItem,
84                                               @NonNull TransactionListItem newItem) {
85                 switch (oldItem.getType()) {
86                     case DELIMITER:
87                         // Delimiters items are "same" for same dates and the contents are the date
88                         return true;
89                     case TRANSACTION:
90                         return oldItem.getTransaction()
91                                       .equals(newItem.getTransaction());
92                     default:
93                         throw new IllegalStateException(
94                                 String.format(Locale.US, "Unexpected transaction item type %s",
95                                         oldItem.getType()));
96
97                 }
98             }
99         });
100     }
101     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
102         TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
103
104         // in a race when transaction value is reduced, but the model hasn't been notified yet
105         // the view will disappear when the notifications reaches the model, so by simply omitting
106         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
107         // a bit longer
108         if (item == null)
109             return;
110
111         switch (item.getType()) {
112             case TRANSACTION:
113                 holder.vTransaction.setVisibility(View.VISIBLE);
114                 holder.vDelimiter.setVisibility(View.GONE);
115                 LedgerTransaction tr = item.getTransaction();
116
117                 //        debug("transactions", String.format("Filling position %d with %d
118                 //        accounts", position,
119                 //                tr.getAccounts().size()));
120
121                 TransactionLoader loader = new TransactionLoader();
122                 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
123                         new TransactionLoaderParams(tr, holder, position,
124                                 Data.accountFilter.getValue(), item.isOdd()));
125
126                 // WORKAROUND what seems to be a bug in CardHolder somewhere
127                 // when a view that was previously holding a delimiter is re-purposed
128                 // occasionally it stays too short (not high enough)
129                 holder.vTransaction.measure(
130                         View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
131                                 View.MeasureSpec.EXACTLY),
132                         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
133                 break;
134             case DELIMITER:
135                 SimpleDate date = item.getDate();
136                 holder.vTransaction.setVisibility(View.GONE);
137                 holder.vDelimiter.setVisibility(View.VISIBLE);
138                 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
139                                                          .format(date.toDate()));
140                 if (item.isMonthShown()) {
141                     GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
142                     cal.setTime(date.toDate());
143                     App.prepareMonthNames();
144                     holder.tvDelimiterMonth.setText(
145                             Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
146                     holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
147                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
148                     //                .dashed_border_8dp);
149                     holder.vDelimiterThick.setVisibility(View.VISIBLE);
150                 }
151                 else {
152                     holder.tvDelimiterMonth.setVisibility(View.GONE);
153                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
154                     //                .dashed_border_1dp);
155                     holder.vDelimiterThick.setVisibility(View.GONE);
156                 }
157                 break;
158         }
159     }
160
161     @NonNull
162     @Override
163     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
164 //        debug("perf", "onCreateViewHolder called");
165         View row = LayoutInflater.from(parent.getContext())
166                                  .inflate(R.layout.transaction_list_row, parent, false);
167         return new TransactionRowHolder(row);
168     }
169
170     @Override
171     public int getItemCount() {
172         return Data.transactions.size();
173     }
174     enum LoaderStep {HEAD, ACCOUNTS, DONE}
175
176     private static class TransactionLoader
177             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
178         @Override
179         protected Void doInBackground(TransactionLoaderParams... p) {
180             LedgerTransaction tr = p[0].transaction;
181             boolean odd = p[0].odd;
182
183             SQLiteDatabase db = App.getDatabase();
184             tr.loadData(db);
185
186             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
187
188             int rowIndex = 0;
189             // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
190             for (LedgerTransactionAccount acc : tr.getAccounts()) {
191 //                debug(c.getAccountName(), acc.getAmount()));
192                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
193                         p[0].boldAccountName));
194             }
195
196             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
197
198             return null;
199         }
200         @Override
201         protected void onProgressUpdate(TransactionLoaderStep... values) {
202             super.onProgressUpdate(values);
203             TransactionLoaderStep step = values[0];
204             TransactionRowHolder holder = step.getHolder();
205
206             switch (step.getStep()) {
207                 case HEAD:
208                     holder.tvDescription.setText(step.getTransaction()
209                                                      .getDescription());
210                     String trComment = Misc.emptyIsNull(step.getTransaction()
211                                                             .getComment());
212                     if (trComment == null)
213                         holder.tvComment.setVisibility(View.GONE);
214                     else {
215                         holder.tvComment.setText(trComment);
216                         holder.tvComment.setVisibility(View.VISIBLE);
217                     }
218
219 //                    if (step.isOdd())
220 //                        holder.row.setBackgroundColor(Colors.tableRowDarkBG);
221 //                    else
222 //                        holder.row.setBackgroundColor(Colors.tableRowLightBG);
223
224                     break;
225                 case ACCOUNTS:
226                     int rowIndex = step.getAccountPosition();
227                     Context ctx = holder.row.getContext();
228                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
229                     if (row == null) {
230                         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
231                         row = (LinearLayout) inflater.inflate(
232                                 R.layout.transaction_list_row_accounts_table_row, null);
233                         // if the rootView above is given (and the line below is spared)
234                         // the accounts remain with their default text (set in the layout resource)
235                         holder.tableAccounts.addView(row);
236                     }
237                     TextView dummyText = row.findViewById(R.id.dummy_text);
238                     TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
239                     TextView accComment =
240                             row.findViewById(R.id.transaction_list_acc_row_acc_comment);
241                     TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
242                     LedgerTransactionAccount acc = step.getAccount();
243
244
245 //                    debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
246 //                            acc.getAccountName(), acc.getAmount()));
247
248                     String boldAccountName = step.getBoldAccountName();
249                     if ((boldAccountName != null) && acc.getAccountName()
250                                                         .startsWith(boldAccountName))
251                     {
252                         accName.setTextColor(Colors.secondary);
253                         accAmount.setTextColor(Colors.secondary);
254
255                         SpannableString ss = new SpannableString(acc.getAccountName());
256                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
257                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
258                         accName.setText(ss);
259                     }
260                     else {
261                         @ColorInt int textColor = dummyText.getTextColors()
262                                                            .getDefaultColor();
263                         accName.setTextColor(textColor);
264                         accAmount.setTextColor(textColor);
265                         accName.setText(acc.getAccountName());
266                     }
267
268                     String comment = acc.getComment();
269                     if (comment != null && !comment.isEmpty()) {
270                         accComment.setText(comment);
271                         accComment.setVisibility(View.VISIBLE);
272                     }
273                     else {
274                         accComment.setVisibility(View.GONE);
275                     }
276                     accAmount.setText(acc.toString());
277
278                     break;
279                 case DONE:
280                     int accCount = step.getAccountCount();
281                     if (holder.tableAccounts.getChildCount() > accCount) {
282                         holder.tableAccounts.removeViews(accCount,
283                                 holder.tableAccounts.getChildCount() - accCount);
284                     }
285
286 //                    debug("transactions",
287 //                            String.format("Position %d fill done", step.getPosition()));
288             }
289         }
290     }
291
292     private class TransactionLoaderParams {
293         LedgerTransaction transaction;
294         TransactionRowHolder holder;
295         int position;
296         String boldAccountName;
297         boolean odd;
298         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
299                                 int position, String boldAccountName, boolean odd) {
300             this.transaction = transaction;
301             this.holder = holder;
302             this.position = position;
303             this.boldAccountName = boldAccountName;
304             this.odd = odd;
305         }
306     }
307 }