]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
Data.transactions -> observable 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 net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.model.TransactionListItem;
39 import net.ktnx.mobileledger.utils.Colors;
40 import net.ktnx.mobileledger.utils.Globals;
41 import net.ktnx.mobileledger.utils.MLDB;
42
43 import java.text.DateFormat;
44 import java.util.Date;
45 import java.util.GregorianCalendar;
46 import java.util.TimeZone;
47
48 import androidx.annotation.NonNull;
49 import androidx.appcompat.widget.AppCompatTextView;
50 import androidx.recyclerview.widget.RecyclerView;
51
52 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
53
54 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
55     private String boldAccountName;
56     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
57         TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
58
59         // in a race when transaction value is reduced, but the model hasn't been notified yet
60         // the view will disappear when the notifications reaches the model, so by simply omitting
61         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
62         // a bit longer
63         if (item == null) return;
64
65         switch (item.getType()) {
66             case TRANSACTION:
67                 holder.vTransaction.setVisibility(View.VISIBLE);
68                 holder.vDelimiter.setVisibility(View.GONE);
69                 holder.vTrailer.setVisibility(View.GONE);
70                 LedgerTransaction tr = item.getTransaction();
71
72                 //        Log.d("transactions", String.format("Filling position %d with %d 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, boldAccountName,
78                                 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(View.MeasureSpec
84                                 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
85                         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
86                 break;
87             case DELIMITER:
88                 Date date = item.getDate();
89                 holder.vTransaction.setVisibility(View.GONE);
90                 holder.vTrailer.setVisibility(View.GONE);
91                 holder.vDelimiter.setVisibility(View.VISIBLE);
92                 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
93                 if (item.isMonthShown()) {
94                     GregorianCalendar cal = new GregorianCalendar(TimeZone.getDefault());
95                     cal.setTime(date);
96                     holder.tvDelimiterMonth
97                             .setText(Globals.monthNames[cal.get(GregorianCalendar.MONTH)]);
98                     holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
99                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
100                     holder.vDelimiterLine.setVisibility(View.GONE);
101                     holder.vDelimiterThick.setVisibility(View.VISIBLE);
102                 }
103                 else {
104                     holder.tvDelimiterMonth.setVisibility(View.GONE);
105                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
106                     holder.vDelimiterLine.setVisibility(View.VISIBLE);
107                     holder.vDelimiterThick.setVisibility(View.GONE);
108                 }
109                 break;
110             case TRAILER:
111                 holder.vTransaction.setVisibility(View.GONE);
112                 holder.vTrailer.setVisibility(View.VISIBLE);
113                 holder.vDelimiter.setVisibility(View.GONE);
114
115                 break;
116         }
117     }
118
119     @NonNull
120     @Override
121     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
122 //        Log.d("perf", "onCreateViewHolder called");
123         View row = LayoutInflater.from(parent.getContext())
124                 .inflate(R.layout.transaction_list_row, parent, false);
125         return new TransactionRowHolder(row);
126     }
127
128     @Override
129     public int getItemCount() {
130         return Data.transactions.size() + 1;
131     }
132     public void setBoldAccountName(String boldAccountName) {
133         this.boldAccountName = boldAccountName;
134     }
135     public void resetBoldAccountName() {
136         this.boldAccountName = null;
137     }
138
139     enum LoaderStep {HEAD, ACCOUNTS, DONE}
140
141     private static class TransactionLoader
142             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
143         @Override
144         protected Void doInBackground(TransactionLoaderParams... p) {
145             LedgerTransaction tr = p[0].transaction;
146             boolean odd = p[0].odd;
147
148             SQLiteDatabase db = MLDB.getDatabase();
149             tr.loadData(db);
150
151             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
152
153             int rowIndex = 0;
154             for (LedgerTransactionAccount acc : tr.getAccounts()) {
155 //                Log.d(c.getAccountName(), acc.getAmount()));
156                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
157                         p[0].boldAccountName));
158             }
159
160             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
161
162             return null;
163         }
164         @Override
165         protected void onProgressUpdate(TransactionLoaderStep... values) {
166             super.onProgressUpdate(values);
167             TransactionLoaderStep step = values[0];
168             TransactionRowHolder holder = step.getHolder();
169
170             switch (step.getStep()) {
171                 case HEAD:
172                     holder.tvDescription.setText(step.getTransaction().getDescription());
173
174                     if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
175                     else holder.row.setBackgroundColor(Colors.tableRowLightBG);
176
177                     break;
178                 case ACCOUNTS:
179                     int rowIndex = step.getAccountPosition();
180                     Context ctx = holder.row.getContext();
181                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
182                     TextView accName, accAmount;
183                     if (row == null) {
184                         row = new LinearLayout(ctx);
185                         row.setLayoutParams(new LinearLayout.LayoutParams(
186                                 LinearLayout.LayoutParams.MATCH_PARENT,
187                                 LinearLayout.LayoutParams.WRAP_CONTENT));
188                         row.setGravity(Gravity.CENTER_VERTICAL);
189                         row.setOrientation(LinearLayout.HORIZONTAL);
190                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
191                         accName = new AppCompatTextView(ctx);
192                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
193                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
194                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
195                         row.addView(accName);
196                         accAmount = new AppCompatTextView(ctx);
197                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
198                                 LinearLayout.LayoutParams.WRAP_CONTENT,
199                                 LinearLayout.LayoutParams.WRAP_CONTENT);
200                         llp.setMarginEnd(0);
201                         accAmount.setLayoutParams(llp);
202                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
203                         accAmount.setMinWidth(dp2px(ctx, 60));
204                         row.addView(accAmount);
205                         holder.tableAccounts.addView(row);
206                     }
207                     else {
208                         accName = (TextView) row.getChildAt(0);
209                         accAmount = (TextView) row.getChildAt(1);
210                     }
211                     LedgerTransactionAccount acc = step.getAccount();
212
213
214 //                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
215 //                            acc.getAccountName(), acc.getAmount()));
216
217                     String boldAccountName = step.getBoldAccountName();
218                     if ((boldAccountName != null) &&
219                         acc.getAccountName().startsWith(boldAccountName))
220                     {
221                         accName.setTextColor(Colors.accent);
222                         accAmount.setTextColor(Colors.accent);
223
224                         SpannableString ss = new SpannableString(acc.getAccountName());
225                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
226                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
227                         accName.setText(ss);
228                     }
229                     else {
230                         accName.setTextColor(Colors.defaultTextColor);
231                         accAmount.setTextColor(Colors.defaultTextColor);
232                         accName.setText(acc.getAccountName());
233                     }
234                     accAmount.setText(acc.toString());
235
236                     break;
237                 case DONE:
238                     int accCount = step.getAccountCount();
239                     if (holder.tableAccounts.getChildCount() > accCount) {
240                         holder.tableAccounts.removeViews(accCount,
241                                 holder.tableAccounts.getChildCount() - accCount);
242                     }
243
244 //                    Log.d("transactions",
245 //                            String.format("Position %d fill done", step.getPosition()));
246             }
247         }
248     }
249
250     private class TransactionLoaderParams {
251         LedgerTransaction transaction;
252         TransactionRowHolder holder;
253         int position;
254         String boldAccountName;
255         boolean odd;
256         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
257                                 int position, String boldAccountName, boolean odd) {
258             this.transaction = transaction;
259             this.holder = holder;
260             this.position = position;
261             this.boldAccountName = boldAccountName;
262             this.odd = odd;
263         }
264     }
265 }