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