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