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