]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
whitespace, imports
[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.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.NonNull;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import net.ktnx.mobileledger.App;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Data;
40 import net.ktnx.mobileledger.model.LedgerTransaction;
41 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
42 import net.ktnx.mobileledger.model.TransactionListItem;
43 import net.ktnx.mobileledger.utils.Colors;
44 import net.ktnx.mobileledger.utils.Globals;
45
46 import java.text.DateFormat;
47 import java.util.Date;
48 import java.util.GregorianCalendar;
49 import java.util.TimeZone;
50
51 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
52     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
53         TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
54
55         // in a race when transaction value is reduced, but the model hasn't been notified yet
56         // the view will disappear when the notifications reaches the model, so by simply omitting
57         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
58         // a bit longer
59         if (item == null)
60             return;
61
62         switch (item.getType()) {
63             case TRANSACTION:
64                 holder.vTransaction.setVisibility(View.VISIBLE);
65                 holder.vDelimiter.setVisibility(View.GONE);
66                 LedgerTransaction tr = item.getTransaction();
67
68                 //        debug("transactions", String.format("Filling position %d with %d
69                 //        accounts", position,
70                 //                tr.getAccounts().size()));
71
72                 TransactionLoader loader = new TransactionLoader();
73                 loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
74                         new TransactionLoaderParams(tr, holder, position,
75                                 Data.accountFilter.getValue(), 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(
81                         View.MeasureSpec.makeMeasureSpec(holder.itemView.getWidth(),
82                                 View.MeasureSpec.EXACTLY),
83                         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
84                 break;
85             case DELIMITER:
86                 Date date = item.getDate();
87                 holder.vTransaction.setVisibility(View.GONE);
88                 holder.vDelimiter.setVisibility(View.VISIBLE);
89                 holder.tvDelimiterDate.setText(DateFormat.getDateInstance()
90                                                          .format(date));
91                 if (item.isMonthShown()) {
92                     GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
93                     cal.setTime(date);
94                     holder.tvDelimiterMonth.setText(
95                             Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
96                     holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
97                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
98                     //                .dashed_border_8dp);
99                     holder.vDelimiterLine.setVisibility(View.GONE);
100                     holder.vDelimiterThick.setVisibility(View.VISIBLE);
101                 }
102                 else {
103                     holder.tvDelimiterMonth.setVisibility(View.GONE);
104                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable
105                     //                .dashed_border_1dp);
106                     holder.vDelimiterLine.setVisibility(View.VISIBLE);
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
163                     if (step.isOdd())
164                         holder.row.setBackgroundColor(Colors.tableRowDarkBG);
165                     else
166                         holder.row.setBackgroundColor(Colors.tableRowLightBG);
167
168                     break;
169                 case ACCOUNTS:
170                     int rowIndex = step.getAccountPosition();
171                     Context ctx = holder.row.getContext();
172                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
173                     if (row == null) {
174                         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
175                         row = (LinearLayout) inflater.inflate(
176                                 R.layout.transaction_list_row_accounts_table_row, null);
177                         // if the rootView above is given (and the line below is spared)
178                         // the accounts remain with their default text (set in the layout resource)
179                         holder.tableAccounts.addView(row);
180                     }
181                     TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
182                     TextView accComment =
183                             row.findViewById(R.id.transaction_list_acc_row_acc_comment);
184                     TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
185                     LedgerTransactionAccount acc = step.getAccount();
186
187
188 //                    debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
189 //                            acc.getAccountName(), acc.getAmount()));
190
191                     String boldAccountName = step.getBoldAccountName();
192                     if ((boldAccountName != null) && acc.getAccountName()
193                                                         .startsWith(boldAccountName))
194                     {
195                         accName.setTextColor(Colors.accent);
196                         accAmount.setTextColor(Colors.accent);
197
198                         SpannableString ss = new SpannableString(acc.getAccountName());
199                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
200                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
201                         accName.setText(ss);
202                     }
203                     else {
204                         accName.setTextColor(Colors.defaultTextColor);
205                         accAmount.setTextColor(Colors.defaultTextColor);
206                         accName.setText(acc.getAccountName());
207                     }
208
209                     String comment = acc.getComment();
210                     if (comment != null && !comment.isEmpty()) {
211                         accComment.setText(comment);
212                         accComment.setVisibility(View.VISIBLE);
213                     }
214                     else {
215                         accComment.setVisibility(View.GONE);
216                     }
217                     accAmount.setText(acc.toString());
218
219                     break;
220                 case DONE:
221                     int accCount = step.getAccountCount();
222                     if (holder.tableAccounts.getChildCount() > accCount) {
223                         holder.tableAccounts.removeViews(accCount,
224                                 holder.tableAccounts.getChildCount() - accCount);
225                     }
226
227 //                    debug("transactions",
228 //                            String.format("Position %d fill done", step.getPosition()));
229             }
230         }
231     }
232
233     private class TransactionLoaderParams {
234         LedgerTransaction transaction;
235         TransactionRowHolder holder;
236         int position;
237         String boldAccountName;
238         boolean odd;
239         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
240                                 int position, String boldAccountName, boolean odd) {
241             this.transaction = transaction;
242             this.holder = holder;
243             this.position = position;
244             this.boldAccountName = boldAccountName;
245             this.odd = odd;
246         }
247     }
248 }