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