]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
migrate account name filter to LiveData
[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     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, Data.accountFilter.getValue(),
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 Data.transactions.size() + 1;
130     }
131     enum LoaderStep {HEAD, ACCOUNTS, DONE}
132     private static class TransactionLoader
133             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
134         @Override
135         protected Void doInBackground(TransactionLoaderParams... p) {
136             LedgerTransaction tr = p[0].transaction;
137             boolean odd = p[0].odd;
138
139             SQLiteDatabase db = MLDB.getDatabase();
140             tr.loadData(db);
141
142             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
143
144             int rowIndex = 0;
145             for (LedgerTransactionAccount acc : tr.getAccounts()) {
146 //                Log.d(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().getDescription());
164
165                     if (step.isOdd()) holder.row.setBackgroundColor(Colors.tableRowDarkBG);
166                     else 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                     TextView accName, accAmount;
174                     if (row == null) {
175                         row = new LinearLayout(ctx);
176                         row.setLayoutParams(new LinearLayout.LayoutParams(
177                                 LinearLayout.LayoutParams.MATCH_PARENT,
178                                 LinearLayout.LayoutParams.WRAP_CONTENT));
179                         row.setGravity(Gravity.CENTER_VERTICAL);
180                         row.setOrientation(LinearLayout.HORIZONTAL);
181                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
182                         accName = new AppCompatTextView(ctx);
183                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
184                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
185                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
186                         row.addView(accName);
187                         accAmount = new AppCompatTextView(ctx);
188                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
189                                 LinearLayout.LayoutParams.WRAP_CONTENT,
190                                 LinearLayout.LayoutParams.WRAP_CONTENT);
191                         llp.setMarginEnd(0);
192                         accAmount.setLayoutParams(llp);
193                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
194                         accAmount.setMinWidth(dp2px(ctx, 60));
195                         row.addView(accAmount);
196                         holder.tableAccounts.addView(row);
197                     }
198                     else {
199                         accName = (TextView) row.getChildAt(0);
200                         accAmount = (TextView) row.getChildAt(1);
201                     }
202                     LedgerTransactionAccount acc = step.getAccount();
203
204
205 //                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
206 //                            acc.getAccountName(), acc.getAmount()));
207
208                     String boldAccountName = step.getBoldAccountName();
209                     if ((boldAccountName != null) &&
210                         acc.getAccountName().startsWith(boldAccountName))
211                     {
212                         accName.setTextColor(Colors.accent);
213                         accAmount.setTextColor(Colors.accent);
214
215                         SpannableString ss = new SpannableString(acc.getAccountName());
216                         ss.setSpan(new StyleSpan(Typeface.BOLD), 0, boldAccountName.length(),
217                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
218                         accName.setText(ss);
219                     }
220                     else {
221                         accName.setTextColor(Colors.defaultTextColor);
222                         accAmount.setTextColor(Colors.defaultTextColor);
223                         accName.setText(acc.getAccountName());
224                     }
225                     accAmount.setText(acc.toString());
226
227                     break;
228                 case DONE:
229                     int accCount = step.getAccountCount();
230                     if (holder.tableAccounts.getChildCount() > accCount) {
231                         holder.tableAccounts.removeViews(accCount,
232                                 holder.tableAccounts.getChildCount() - accCount);
233                     }
234
235 //                    Log.d("transactions",
236 //                            String.format("Position %d fill done", step.getPosition()));
237             }
238         }
239     }
240
241     private class TransactionLoaderParams {
242         LedgerTransaction transaction;
243         TransactionRowHolder holder;
244         int position;
245         String boldAccountName;
246         boolean odd;
247         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
248                                 int position, String boldAccountName, boolean odd) {
249             this.transaction = transaction;
250             this.holder = holder;
251             this.position = position;
252             this.boldAccountName = boldAccountName;
253             this.odd = odd;
254         }
255     }
256 }