]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
transaction list: make day/month markers more visible
[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         // in a race when transaction value is reduced, but the model hasn't been notified yet
52         // the view will disappear when the notifications reaches the model, so by simply omitting
53         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
54         // a bit longer
55         if (item == null) return;
56
57         if (item.getType() == TransactionListItem.Type.TRANSACTION) {
58             holder.vTransaction.setVisibility(View.VISIBLE);
59             holder.vDelimiter.setVisibility(View.GONE);
60             LedgerTransaction tr = item.getTransaction();
61
62 //        Log.d("transactions", String.format("Filling position %d with %d accounts", position,
63 //                tr.getAccounts().size()));
64
65             TransactionLoader loader = new TransactionLoader();
66             loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName,
67                     item.isOdd()));
68
69             // WORKAROUND what seems to be a bug in CardHolder somewhere
70             // when a view that was previously holding a delimiter is re-purposed
71             // occasionally it stays too short (not high enough)
72             holder.vTransaction.measure(View.MeasureSpec
73                             .makeMeasureSpec(holder.itemView.getWidth(), View.MeasureSpec.EXACTLY),
74                     View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
75         }
76         else {
77             Date date = item.getDate();
78             holder.vTransaction.setVisibility(View.GONE);
79             holder.vDelimiter.setVisibility(View.VISIBLE);
80             holder.tvDelimiterDate.setText(DateFormat.getDateInstance().format(date));
81             if (item.isMonthShown()) {
82                 holder.tvDelimiterMonth.setText(Globals.monthNames[date.getMonth()]);
83                 holder.tvDelimiterMonth.setVisibility(View.VISIBLE);
84 //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_8dp);
85                 holder.vDelimiterLine.setVisibility(View.GONE);
86                 holder.vDelimiterThick.setVisibility(View.VISIBLE);
87             }
88             else {
89                 holder.tvDelimiterMonth.setVisibility(View.GONE);
90 //                holder.vDelimiterLine.setBackgroundResource(R.drawable.dashed_border_1dp);
91                 holder.vDelimiterLine.setVisibility(View.VISIBLE);
92                 holder.vDelimiterThick.setVisibility(View.GONE);
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             boolean odd = p[0].odd;
125
126             SQLiteDatabase db = MLDB.getReadableDatabase();
127             tr.loadData(db);
128
129             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr, odd));
130
131             int rowIndex = 0;
132             for (LedgerTransactionAccount acc : tr.getAccounts()) {
133 //                Log.d(c.getAccountName(), acc.getAmount()));
134                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
135                         p[0].boldAccountName));
136             }
137
138             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
139
140             return null;
141         }
142         @Override
143         protected void onProgressUpdate(TransactionLoaderStep... values) {
144             super.onProgressUpdate(values);
145             TransactionLoaderStep step = values[0];
146             TransactionRowHolder holder = step.getHolder();
147
148             switch (step.getStep()) {
149                 case HEAD:
150                     holder.tvDescription.setText(step.getTransaction().getDescription());
151
152                     if (step.isOdd()) holder.row.setBackgroundColor(Globals.tableRowDarkBG);
153                     else holder.row.setBackgroundColor(Globals.tableRowLightBG);
154
155                     break;
156                 case ACCOUNTS:
157                     int rowIndex = step.getAccountPosition();
158                     Context ctx = holder.row.getContext();
159                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
160                     TextView accName, accAmount;
161                     if (row == null) {
162                         row = new LinearLayout(ctx);
163                         row.setLayoutParams(new LinearLayout.LayoutParams(
164                                 LinearLayout.LayoutParams.MATCH_PARENT,
165                                 LinearLayout.LayoutParams.WRAP_CONTENT));
166                         row.setGravity(Gravity.CENTER_VERTICAL);
167                         row.setOrientation(LinearLayout.HORIZONTAL);
168                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
169                         accName = new AppCompatTextView(ctx);
170                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
171                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
172                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
173                         row.addView(accName);
174                         accAmount = new AppCompatTextView(ctx);
175                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
176                                 LinearLayout.LayoutParams.WRAP_CONTENT,
177                                 LinearLayout.LayoutParams.WRAP_CONTENT);
178                         llp.setMarginEnd(0);
179                         accAmount.setLayoutParams(llp);
180                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
181                         accAmount.setMinWidth(dp2px(ctx, 60));
182                         row.addView(accAmount);
183                         holder.tableAccounts.addView(row);
184                     }
185                     else {
186                         accName = (TextView) row.getChildAt(0);
187                         accAmount = (TextView) row.getChildAt(1);
188                     }
189                     LedgerTransactionAccount acc = step.getAccount();
190
191                     accName.setText(acc.getAccountName());
192                     accAmount.setText(acc.toString());
193
194 //                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
195 //                            acc.getAccountName(), acc.getAmount()));
196
197                     String boldAccountName = step.getBoldAccountName();
198                     if ((boldAccountName != null) &&
199                         acc.getAccountName().startsWith(boldAccountName))
200                     {
201                         accName.setTypeface(null, Typeface.BOLD);
202                         accAmount.setTypeface(null, Typeface.BOLD);
203                         accName.setTextColor(Globals.primaryDark);
204                         accAmount.setTextColor(Globals.primaryDark);
205                     }
206                     else {
207                         accName.setTypeface(null, Typeface.NORMAL);
208                         accAmount.setTypeface(null, Typeface.NORMAL);
209                         accName.setTextColor(Globals.defaultTextColor);
210                         accAmount.setTextColor(Globals.defaultTextColor);
211                     }
212
213                     break;
214                 case DONE:
215                     int accCount = step.getAccountCount();
216                     if (holder.tableAccounts.getChildCount() > accCount) {
217                         holder.tableAccounts.removeViews(accCount,
218                                 holder.tableAccounts.getChildCount() - accCount);
219                     }
220
221 //                    Log.d("transactions",
222 //                            String.format("Position %d fill done", step.getPosition()));
223             }
224         }
225     }
226
227     private class TransactionLoaderParams {
228         LedgerTransaction transaction;
229         TransactionRowHolder holder;
230         int position;
231         String boldAccountName;
232         boolean odd;
233         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
234                                 int position, String boldAccountName, boolean odd) {
235             this.transaction = transaction;
236             this.holder = holder;
237             this.position = position;
238             this.boldAccountName = boldAccountName;
239             this.odd = odd;
240         }
241     }
242 }