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