]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
drop a couple of global colour values
[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.RecyclerView;
37
38 import net.ktnx.mobileledger.App;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.LedgerTransaction;
42 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
43 import net.ktnx.mobileledger.model.TransactionListItem;
44 import net.ktnx.mobileledger.utils.Colors;
45 import net.ktnx.mobileledger.utils.Globals;
46 import net.ktnx.mobileledger.utils.Misc;
47 import net.ktnx.mobileledger.utils.SimpleDate;
48
49 import java.text.DateFormat;
50 import java.util.GregorianCalendar;
51 import java.util.TimeZone;
52
53 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
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)
62             return;
63
64         switch (item.getType()) {
65             case TRANSACTION:
66                 holder.vTransaction.setVisibility(View.VISIBLE);
67                 holder.vDelimiter.setVisibility(View.GONE);
68                 LedgerTransaction tr = item.getTransaction();
69
70                 //        debug("transactions", String.format("Filling position %d with %d
71                 //        accounts", position,
72                 //                tr.getAccounts().size()));
73
74                 TransactionLoader loader = new TransactionLoader();
75                 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
76                         new TransactionLoaderParams(tr, holder, position,
77                                 Data.accountFilter.getValue(), item.isOdd()));
78
79                 // WORKAROUND what seems to be a bug in CardHolder somewhere
80                 // when a view that was previously holding a delimiter is re-purposed
81                 // occasionally it stays too short (not high enough)
82                 holder.vTransaction.measure(
83                         View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
84                                 View.MeasureSpec.EXACTLY),
85                         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
86                 break;
87             case DELIMITER:
88                 SimpleDate date = item.getDate();
89                 holder.vTransaction.setVisibility(View.GONE);
90                 holder.vDelimiter.setVisibility(View.VISIBLE);
91                 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
92                                                          .format(date.toDate()));
93                 if (item.isMonthShown()) {
94                     GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
95                     cal.setTime(date.toDate());
96                     holder.tvDelimiterMonth.setText(
97                             Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
98                     holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
99                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
100                     //                .dashed_border_8dp);
101                     holder.vDelimiterThick.setVisibility(View.VISIBLE);
102                 }
103                 else {
104                     holder.tvDelimiterMonth.setVisibility(View.GONE);
105                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
106                     //                .dashed_border_1dp);
107                     holder.vDelimiterThick.setVisibility(View.GONE);
108                 }
109                 break;
110         }
111     }
112
113     @NonNull
114     @Override
115     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
116 //        debug("perf", "onCreateViewHolder called");
117         View row = LayoutInflater.from(parent.getContext())
118                                  .inflate(R.layout.transaction_list_row, parent, false);
119         return new TransactionRowHolder(row);
120     }
121
122     @Override
123     public int getItemCount() {
124         return Data.transactions.size();
125     }
126     enum LoaderStep {HEAD, ACCOUNTS, DONE}
127
128     private static class TransactionLoader
129             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
130         @Override
131         protected Void doInBackground(TransactionLoaderParams... p) {
132             LedgerTransaction tr = p[0].transaction;
133             boolean odd = p[0].odd;
134
135             SQLiteDatabase db = App.getDatabase();
136             tr.loadData(db);
137
138             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
139
140             int rowIndex = 0;
141             // FIXME ConcurrentModificationException in ArrayList$ltr.next (ArrayList.java:831)
142             for (LedgerTransactionAccount acc : tr.getAccounts()) {
143 //                debug(c.getAccountName(), acc.getAmount()));
144                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
145                         p[0].boldAccountName));
146             }
147
148             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
149
150             return null;
151         }
152         @Override
153         protected void onProgressUpdate(TransactionLoaderStep... values) {
154             super.onProgressUpdate(values);
155             TransactionLoaderStep step = values[0];
156             TransactionRowHolder holder = step.getHolder();
157
158             switch (step.getStep()) {
159                 case HEAD:
160                     holder.tvDescription.setText(step.getTransaction()
161                                                      .getDescription());
162                     String trComment = Misc.emptyIsNull(step.getTransaction()
163                                                             .getComment());
164                     if (trComment == null)
165                         holder.tvComment.setVisibility(View.GONE);
166                     else {
167                         holder.tvComment.setText(trComment);
168                         holder.tvComment.setVisibility(View.VISIBLE);
169                     }
170
171 //                    if (step.isOdd())
172 //                        holder.row.setBackgroundColor(Colors.tableRowDarkBG);
173 //                    else
174 //                        holder.row.setBackgroundColor(Colors.tableRowLightBG);
175
176                     break;
177                 case ACCOUNTS:
178                     int rowIndex = step.getAccountPosition();
179                     Context ctx = holder.row.getContext();
180                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
181                     if (row == null) {
182                         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
183                         row = (LinearLayout) inflater.inflate(
184                                 R.layout.transaction_list_row_accounts_table_row, null);
185                         // if the rootView above is given (and the line below is spared)
186                         // the accounts remain with their default text (set in the layout resource)
187                         holder.tableAccounts.addView(row);
188                     }
189                     TextView dummyText = row.findViewById(R.id.dummy_text);
190                     TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
191                     TextView accComment =
192                             row.findViewById(R.id.transaction_list_acc_row_acc_comment);
193                     TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
194                     LedgerTransactionAccount acc = step.getAccount();
195
196
197 //                    debug("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) && acc.getAccountName()
202                                                         .startsWith(boldAccountName))
203                     {
204                         accName.setTextColor(Colors.accent);
205                         accAmount.setTextColor(Colors.accent);
206
207                         SpannableString ss = new SpannableString(acc.getAccountName());
208                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
209                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
210                         accName.setText(ss);
211                     }
212                     else {
213                         @ColorInt int textColor = dummyText.getTextColors()
214                                                            .getDefaultColor();
215                         accName.setTextColor(textColor);
216                         accAmount.setTextColor(textColor);
217                         accName.setText(acc.getAccountName());
218                     }
219
220                     String comment = acc.getComment();
221                     if (comment != null && !comment.isEmpty()) {
222                         accComment.setText(comment);
223                         accComment.setVisibility(View.VISIBLE);
224                     }
225                     else {
226                         accComment.setVisibility(View.GONE);
227                     }
228                     accAmount.setText(acc.toString());
229
230                     break;
231                 case DONE:
232                     int accCount = step.getAccountCount();
233                     if (holder.tableAccounts.getChildCount() > accCount) {
234                         holder.tableAccounts.removeViews(accCount,
235                                 holder.tableAccounts.getChildCount() - accCount);
236                     }
237
238 //                    debug("transactions",
239 //                            String.format("Position %d fill done", step.getPosition()));
240             }
241         }
242     }
243
244     private class TransactionLoaderParams {
245         LedgerTransaction transaction;
246         TransactionRowHolder holder;
247         int position;
248         String boldAccountName;
249         boolean odd;
250         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
251                                 int position, String boldAccountName, boolean odd) {
252             this.transaction = transaction;
253             this.holder = holder;
254             this.position = position;
255             this.boldAccountName = boldAccountName;
256             this.odd = odd;
257         }
258     }
259 }