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