]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
toCamelCase
[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.utils.Globals;
38 import net.ktnx.mobileledger.utils.MLDB;
39
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
41
42 public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowHolder> {
43     private String boldAccountName;
44     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
45         LedgerTransaction tr = TransactionListViewModel.getTransaction(position);
46         // in a race when transaction value is reduced, but the model hasn't been notified yet
47         // the view will disappear when the notifications reaches the model, so by simply omitting
48         // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains
49         // a bit longer
50         if (tr == null) return;
51
52 //        Log.d("transactions", String.format("Filling position %d with %d accounts", position,
53 //                tr.getAccounts().size()));
54
55         TransactionLoader loader = new TransactionLoader();
56         loader.execute(new TransactionLoaderParams(tr, holder, position, boldAccountName));
57     }
58
59     @NonNull
60     @Override
61     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
62 //        Log.d("perf", "onCreateViewHolder called");
63         View row = LayoutInflater.from(parent.getContext())
64                 .inflate(R.layout.transaction_list_row, parent, false);
65         return new TransactionRowHolder(row);
66     }
67
68     @Override
69     public int getItemCount() {
70         return TransactionListViewModel.getTransactionCount();
71     }
72     public void setBoldAccountName(String boldAccountName) {
73         this.boldAccountName = boldAccountName;
74     }
75     public void resetBoldAccountName() {
76         this.boldAccountName = null;
77     }
78
79     enum LoaderStep {HEAD, ACCOUNTS, DONE}
80
81     private static class TransactionLoader
82             extends AsyncTask<TransactionLoaderParams, TransactionLoaderStep, Void> {
83         @Override
84         protected Void doInBackground(TransactionLoaderParams... p) {
85             LedgerTransaction tr = p[0].transaction;
86
87             SQLiteDatabase db = MLDB.getReadableDatabase();
88             tr.loadData(db);
89
90             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, tr));
91
92             int rowIndex = 0;
93             for (LedgerTransactionAccount acc : tr.getAccounts()) {
94 //                Log.d(c.getAccountName(), acc.getAmount()));
95                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
96                         p[0].boldAccountName));
97             }
98
99             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
100
101             return null;
102         }
103         @Override
104         protected void onProgressUpdate(TransactionLoaderStep... values) {
105             super.onProgressUpdate(values);
106             TransactionLoaderStep step = values[0];
107             TransactionRowHolder holder = step.getHolder();
108
109             switch (step.getStep()) {
110                 case HEAD:
111                     holder.tvDescription.setText(step.getTransaction().getDescription());
112                     holder.tvDate.setText(step.getTransaction().getDate());
113
114                     if (step.getPosition() % 2 == 0) {
115                         holder.row.setBackgroundColor(Globals.tableRowEvenBG);
116                     }
117                     else {
118                         holder.row.setBackgroundColor(Globals.tableRowOddBG);
119                     }
120
121                     break;
122                 case ACCOUNTS:
123                     int rowIndex = step.getAccountPosition();
124                     Context ctx = holder.row.getContext();
125                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
126                     TextView accName, accAmount;
127                     if (row == null) {
128                         row = new LinearLayout(ctx);
129                         row.setLayoutParams(new LinearLayout.LayoutParams(
130                                 LinearLayout.LayoutParams.MATCH_PARENT,
131                                 LinearLayout.LayoutParams.WRAP_CONTENT));
132                         row.setGravity(Gravity.CENTER_VERTICAL);
133                         row.setOrientation(LinearLayout.HORIZONTAL);
134                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
135                         accName = new AppCompatTextView(ctx);
136                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
137                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
138                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
139                         row.addView(accName);
140                         accAmount = new AppCompatTextView(ctx);
141                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
142                                 LinearLayout.LayoutParams.WRAP_CONTENT,
143                                 LinearLayout.LayoutParams.WRAP_CONTENT);
144                         llp.setMarginEnd(0);
145                         accAmount.setLayoutParams(llp);
146                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
147                         accAmount.setMinWidth(dp2px(ctx, 60));
148                         row.addView(accAmount);
149                         holder.tableAccounts.addView(row);
150                     }
151                     else {
152                         accName = (TextView) row.getChildAt(0);
153                         accAmount = (TextView) row.getChildAt(1);
154                     }
155                     LedgerTransactionAccount acc = step.getAccount();
156
157                     accName.setText(acc.getAccountName());
158                     accAmount.setText(acc.toString());
159
160 //                    Log.d("tmp", String.format("showing acc row %d: %s %1.2f", rowIndex,
161 //                            acc.getAccountName(), acc.getAmount()));
162
163                     String boldAccountName = step.getBoldAccountName();
164                     if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
165                         accName.setTypeface(null, Typeface.BOLD);
166                         accAmount.setTypeface(null, Typeface.BOLD);
167                         accName.setTextColor(Globals.primaryDark);
168                         accAmount.setTextColor(Globals.primaryDark);
169                     }
170                     else {
171                         accName.setTypeface(null, Typeface.NORMAL);
172                         accAmount.setTypeface(null, Typeface.NORMAL);
173                         accName.setTextColor(Globals.defaultTextColor);
174                         accAmount.setTextColor(Globals.defaultTextColor);
175                     }
176
177                     break;
178                 case DONE:
179                     int accCount = step.getAccountCount();
180                     if (holder.tableAccounts.getChildCount() > accCount) {
181                         holder.tableAccounts.removeViews(accCount,
182                                 holder.tableAccounts.getChildCount() - accCount);
183                     }
184
185 //                    Log.d("transactions",
186 //                            String.format("Position %d fill done", step.getPosition()));
187             }
188         }
189     }
190
191     private class TransactionLoaderParams {
192         LedgerTransaction transaction;
193         TransactionRowHolder holder;
194         int position;
195         String boldAccountName;
196         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
197                                 int position, String boldAccountName) {
198             this.transaction = transaction;
199             this.holder = holder;
200             this.position = position;
201             this.boldAccountName = boldAccountName;
202         }
203     }
204 }