]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
stop closing acuired db handles and leave that for the application termination
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / transaction_list / TransactionListAdapter.java
1 /*
2  * Copyright © 2018 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", position));
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                 publishProgress(new TransactionLoaderStep(p[0].holder, acc, rowIndex++,
95                         p[0].boldAccountName));
96             }
97
98             publishProgress(new TransactionLoaderStep(p[0].holder, p[0].position, rowIndex));
99
100             return null;
101         }
102         @Override
103         protected void onProgressUpdate(TransactionLoaderStep... values) {
104             super.onProgressUpdate(values);
105             TransactionLoaderStep step = values[0];
106             TransactionRowHolder holder = step.getHolder();
107
108             switch (step.getStep()) {
109                 case HEAD:
110                     holder.tvDescription.setText(step.getTransaction().getDescription());
111                     holder.tvDate.setText(step.getTransaction().getDate());
112
113                     if (step.getPosition() % 2 == 0) {
114                         holder.row.setBackgroundColor(Globals.table_row_even_bg);
115                     }
116                     else {
117                         holder.row.setBackgroundColor(Globals.table_row_odd_bg);
118                     }
119
120                     break;
121                 case ACCOUNTS:
122                     int rowIndex = step.getAccountPosition();
123                     Context ctx = holder.row.getContext();
124                     LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex);
125                     TextView accName, accAmount;
126                     if (row == null) {
127                         row = new LinearLayout(ctx);
128                         row.setLayoutParams(new LinearLayout.LayoutParams(
129                                 LinearLayout.LayoutParams.MATCH_PARENT,
130                                 LinearLayout.LayoutParams.WRAP_CONTENT));
131                         row.setGravity(Gravity.CENTER_VERTICAL);
132                         row.setOrientation(LinearLayout.HORIZONTAL);
133                         row.setPaddingRelative(dp2px(ctx, 8), 0, 0, 0);
134                         accName = new AppCompatTextView(ctx);
135                         accName.setLayoutParams(new LinearLayout.LayoutParams(0,
136                                 LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
137                         accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
138                         row.addView(accName);
139                         accAmount = new AppCompatTextView(ctx);
140                         LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
141                                 LinearLayout.LayoutParams.WRAP_CONTENT,
142                                 LinearLayout.LayoutParams.WRAP_CONTENT);
143                         llp.setMarginEnd(0);
144                         accAmount.setLayoutParams(llp);
145                         accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
146                         accAmount.setMinWidth(dp2px(ctx, 60));
147                         row.addView(accAmount);
148                         holder.tableAccounts.addView(row);
149                     }
150                     else {
151                         accName = (TextView) row.getChildAt(0);
152                         accAmount = (TextView) row.getChildAt(1);
153                     }
154                     LedgerTransactionAccount acc = step.getAccount();
155
156                     accName.setText(acc.getAccountName());
157                     accAmount.setText(acc.toString());
158
159                     String boldAccountName = step.getBoldAccountName();
160                     if ((boldAccountName != null) && boldAccountName.equals(acc.getAccountName())) {
161                         accName.setTypeface(null, Typeface.BOLD);
162                         accAmount.setTypeface(null, Typeface.BOLD);
163                         accName.setTextColor(Globals.primaryDark);
164                         accAmount.setTextColor(Globals.primaryDark);
165                     }
166                     else {
167                         accName.setTypeface(null, Typeface.NORMAL);
168                         accAmount.setTypeface(null, Typeface.NORMAL);
169                         accName.setTextColor(Globals.defaultTextColor);
170                         accAmount.setTextColor(Globals.defaultTextColor);
171                     }
172
173                     break;
174                 case DONE:
175                     int accCount = step.getAccountCount();
176                     if (holder.tableAccounts.getChildCount() > accCount) {
177                         holder.tableAccounts.removeViews(accCount,
178                                 holder.tableAccounts.getChildCount() - accCount);
179                     }
180
181                     Log.d("transactions",
182                             String.format("Position %d fill done", step.getPosition()));
183             }
184         }
185     }
186
187     private class TransactionLoaderParams {
188         LedgerTransaction transaction;
189         TransactionRowHolder holder;
190         int position;
191         String boldAccountName;
192         TransactionLoaderParams(LedgerTransaction transaction, TransactionRowHolder holder,
193                                 int position, String boldAccountName) {
194             this.transaction = transaction;
195             this.holder = holder;
196             this.position = position;
197             this.boldAccountName = boldAccountName;
198         }
199     }
200 }