]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
Rename to MoLe
[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.support.annotation.NonNull;
25 import android.support.v7.widget.AppCompatTextView;
26 import android.support.v7.widget.RecyclerView;
27 import android.text.Spannable;
28 import android.text.SpannableString;
29 import android.text.style.StyleSpan;
30 import android.view.Gravity;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.model.LedgerTransaction;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.TransactionListItem;
41 import net.ktnx.mobileledger.utils.Globals;
42 import net.ktnx.mobileledger.utils.MLDB;
43
44 import java.text.DateFormat;
45 import java.util.Date;
46
47 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
48
49 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
50     private String boldAccountName;
51     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
52         TransactionListItem item = TransactionListViewModel.getTransactionListItem(position);
53
54         // in a race when transaction value is reduced, but the model hasn't been notified yet
55         // the view will disappear when the notifications reaches the model, so by simply omitting
56         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
57         // a bit longer
58         if (item == null) return;
59
60         switch (item.getType()) {
61             case TRANSACTION:
62                 holder.vTransaction.setVisibility(View.VISIBLE);
63                 holder.vDelimiter.setVisibility(View.GONE);
64                 holder.vTrailer.setVisibility(View.GONE);
65                 LedgerTransaction tr = item.getTransaction();
66
67                 //        Log.d("transactions", String.format("Filling position %d with %d accounts", position,
68                 //                tr.getAccounts().size()));
69
70                 TransactionLoader loader = new TransactionLoader();
71                 loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
72                         item.isOdd()));
73
74                 // WORKAROUND what seems to be a bug in CardHolder somewhere
75                 // when a view that was previously holding a delimiter is re-purposed
76                 // occasionally it stays too short (not high enough)
77                 holder.vTransaction.measure(View.MeasureSpec
78                                 .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
79                         View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
80                 break;
81             case DELIMITER:
82                 Date date = item.getDate();
83                 holder.vTransaction.setVisibility(View.GONE);
84                 holder.vTrailer.setVisibility(View.GONE);
85                 holder.vDelimiter.setVisibility(View.VISIBLE);
86                 holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
87                 if (item.isMonthShown()) {
88                     holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
89                     holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
90                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
91                     holder.vDelimiterLine.setVisibility(View.GONE);
92                     holder.vDelimiterThick.setVisibility(View.VISIBLE);
93                 }
94                 else {
95                     holder.tvDelimiterMonth.setVisibility(View.GONE);
96                     //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
97                     holder.vDelimiterLine.setVisibility(View.VISIBLE);
98                     holder.vDelimiterThick.setVisibility(View.GONE);
99                 }
100                 break;
101             case TRAILER:
102                 holder.vTransaction.setVisibility(View.GONE);
103                 holder.vTrailer.setVisibility(View.VISIBLE);
104                 holder.vDelimiter.setVisibility(View.GONE);
105
106                 break;
107         }
108     }
109
110     @NonNull
111     @Override
112     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
113 //        Log.d("perf", "onCreateViewHolder called");
114         View row = LayoutInflater.from(parent.getContext())
115                 .inflate(R.layout.transaction_list_row, parent, false);
116         return new TransactionRowHolder(row);
117     }
118
119     @Override
120     public int getItemCount() {
121         return TransactionListViewModel.getTransactionCount() + 1;
122     }
123     public void setBoldAccountName(String boldAccountName) {
124         this.boldAccountName = boldAccountName;
125     }
126     public void resetBoldAccountName() {
127         this.boldAccountName = null;
128     }
129
130     enum LoaderStep {HEAD, ACCOUNTS, DONE}
131
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.getReadableDatabase();
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(Globals.tableRowDarkBG);
166                     else holder.row.setBackgroundColor(Globals.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(Globals.primaryDark);
213                         accAmount.setTextColor(Globals.primaryDark);
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(Globals.defaultTextColor);
222                         accAmount.setTextColor(Globals.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 }