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