]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
transaction list: replace table layout with linear one
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / 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;
19
20 import android.content.Context;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.support.annotation.NonNull;
23 import android.support.constraint.ConstraintLayout;
24 import android.support.v7.widget.RecyclerView;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.LinearLayout;
31 import android.widget.TextView;
32
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
35 import net.ktnx.mobileledger.utils.Globals;
36 import net.ktnx.mobileledger.utils.MLDB;
37
38 import java.util.List;
39
40 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
41
42 class TransactionListAdapter
43         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
44     private List<LedgerTransaction> transactions;
45
46     TransactionListAdapter(List<LedgerTransaction> transactions) {
47         this.transactions = transactions;
48     }
49
50     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
51         LedgerTransaction tr = transactions.get(position);
52         Context ctx = holder.row.getContext();
53
54         try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
55             tr.loadData(db);
56             holder.tvDescription
57                     .setText(String.format("%s\t%s", tr.getDescription(), tr.getDate()));
58
59             int rowIndex = 0;
60             for (LedgerTransactionAccount acc : tr.getAccounts()) {
61                 LinearLayout row = (LinearLayout) holder.tableAccounts.getChildAt(rowIndex++);
62                 TextView accName, accAmount;
63                 if (row == null) {
64                     row = new LinearLayout(ctx);
65                     row.setLayoutParams(
66                             new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
67                                     LinearLayout.LayoutParams.WRAP_CONTENT));
68                     row.setGravity(Gravity.CENTER_VERTICAL);
69                     row.setOrientation(LinearLayout.HORIZONTAL);
70                     row.setPaddingRelative(dp2px(ctx, 8), 0, dp2px(ctx, 8), 0);
71                     accName = new TextView(ctx);
72                     accName.setLayoutParams(
73                             new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
74                                     LinearLayout.LayoutParams.WRAP_CONTENT, 5f));
75                     accName.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
76                     row.addView(accName);
77                     accAmount = new TextView(ctx);
78                     LinearLayout.LayoutParams llp =
79                             new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
80                                     LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
81                     llp.setMarginEnd(0);
82                     accAmount.setLayoutParams(llp);
83                     accAmount.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
84                     accAmount.setMinWidth(dp2px(ctx, 60));
85                     row.addView(accAmount);
86                     holder.tableAccounts.addView(row);
87                 }
88                 else {
89                     accName = (TextView) row.getChildAt(0);
90                     accAmount = (TextView) row.getChildAt(1);
91                 }
92                 accName.setText(acc.getShortAccountName());
93                 accAmount.setText(acc.toString());
94             }
95             if (holder.tableAccounts.getChildCount() > rowIndex) {
96                 holder.tableAccounts
97                         .removeViews(rowIndex, holder.tableAccounts.getChildCount() - rowIndex);
98             }
99
100             if (position % 2 == 0) {
101                 holder.row.setBackgroundColor(Globals.table_row_even_bg);
102             }
103             else {
104                 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
105             }
106         }
107     }
108
109     @NonNull
110     @Override
111     public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
112         Log.d("perf", "onCreateViewHolder called");
113         View row = LayoutInflater.from(parent.getContext())
114                 .inflate(R.layout.transaction_list_row, parent, false);
115         return new TransactionRowHolder(row);
116     }
117
118     @Override
119     public int getItemCount() {
120         return transactions.size();
121     }
122     class TransactionRowHolder extends RecyclerView.ViewHolder {
123         TextView tvDescription;
124         LinearLayout tableAccounts;
125         ConstraintLayout row;
126         public TransactionRowHolder(@NonNull View itemView) {
127             super(itemView);
128             this.row = (ConstraintLayout) itemView;
129             this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
130             this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);
131         }
132     }
133 }