]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
fix appending account entry in transaction list
[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, null);
180                         // if the rootView above is given (and the line below is spared)
181                         // the accounts remain with their default text (set in the layout resource)
182                         holder.tableAccounts.addView(row);
183                     }
184                     TextView accName = row.findViewById(R.id.transaction_list_acc_row_acc_name);
185                     TextView accComment =
186                             row.findViewById(R.id.transaction_list_acc_row_acc_comment);
187                     TextView accAmount = row.findViewById(R.id.transaction_list_acc_row_acc_amount);
188                     LedgerTransactionAccount acc = step.getAccount();
189
190
191 //                    debug("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
192 //                            acc.getAccountName(), acc.getAmount()));
193
194                     String boldAccountName = step.getBoldAccountName();
195                     if ((boldAccountName != null) && acc.getAccountName()
196                                                         .startsWith(boldAccountName))
197                     {
198                         accName.setTextColor(Colors.accent);
199                         accAmount.setTextColor(Colors.accent);
200
201                         SpannableString ss = new SpannableString(acc.getAccountName());
202                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
203                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
204                         accName.setText(ss);
205                     }
206                     else {
207                         accName.setTextColor(Colors.defaultTextColor);
208                         accAmount.setTextColor(Colors.defaultTextColor);
209                         accName.setText(acc.getAccountName());
210                     }
211
212                     String comment = acc.getComment();
213                     if (comment != null && !comment.isEmpty()) {
214                         accComment.setText(comment);
215                         accComment.setVisibility(View.VISIBLE);
216                     }
217                     else {
218                         accComment.setVisibility(View.GONE);
219                     }
220                     accAmount.setText(acc.toString());
221
222                     break;
223                 case DONE:
224                     int accCount = step.getAccountCount();
225                     if (holder.tableAccounts.getChildCount() > accCount) {
226                         holder.tableAccounts.removeViews(accCount,
227                                 holder.tableAccounts.getChildCount() - accCount);
228                     }
229
230 //                    debug("transactions",
231 //                            String.format("Position %d fill done", step.getPosition()));
232             }
233         }
234     }
235
236     private class TransactionLoaderParams {
237         LedgerTransaction transaction;
238         TransactionRowHolder holder;
239         int position;
240         String boldAccountName;
241         boolean odd;
242         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
243                                 int position, String boldAccountName, boolean odd) {
244             this.transaction = transaction;
245             this.holder = holder;
246             this.position = position;
247             this.boldAccountName = boldAccountName;
248             this.odd = odd;
249         }
250     }
251 }