2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe 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.
9 * MoLe 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.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.database.Cursor;
21 import android.view.LayoutInflater;
22 import android.view.ViewGroup;
23 import android.widget.LinearLayout;
24 import android.widget.TableRow;
26 import androidx.annotation.NonNull;
27 import androidx.recyclerview.widget.RecyclerView;
29 import net.ktnx.mobileledger.App;
30 import net.ktnx.mobileledger.R;
31 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
35 import net.ktnx.mobileledger.model.MobileLedgerProfile;
36 import net.ktnx.mobileledger.utils.Logger;
38 import java.util.ArrayList;
39 import java.util.Locale;
41 import static net.ktnx.mobileledger.utils.Logger.debug;
43 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder>
44 implements DescriptionSelectedCallback {
45 NewTransactionModel model;
46 private MobileLedgerProfile mProfile;
47 NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
51 int size = model.getAccountCount();
53 Logger.debug("new-transaction",
54 String.format(Locale.US, "%d accounts is too little, Calling addRow()", size));
58 public void setProfile(MobileLedgerProfile profile) {
62 final int newAccountCount = model.addAccount(new LedgerTransactionAccount(""));
63 Logger.debug("new-transaction",
64 String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
65 // the header is at position 0
66 notifyItemInserted(newAccountCount);
67 model.sendCountNotifications(); // needed after holders' positions have changed
68 return newAccountCount;
72 public NewTransactionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
73 LinearLayout row = (LinearLayout) LayoutInflater.from(parent.getContext())
74 .inflate(R.layout.new_transaction_row,
76 return new NewTransactionItemHolder(row, this);
79 public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
80 Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
81 holder.setData(model.getItem(position));
82 Logger.debug("bind", String.format(Locale.US, "Bound item at position %d", position));
85 public int getItemCount() {
86 final int itemCount = model.getAccountCount() + 2;
87 Logger.debug("new-transaction",
88 String.format(Locale.US, "getItemCount() returning %d", itemCount));
91 boolean accountListIsEmpty() {
92 for (int i = 0; i < model.getAccountCount(); i++) {
93 LedgerTransactionAccount acc = model.getAccount(i);
94 if (!acc.getAccountName()
95 .isEmpty()) return false;
96 if (acc.isAmountSet()) return false;
101 public void descriptionSelected(String description) {
102 debug("descr selected", description);
103 if (!accountListIsEmpty()) return;
105 String accFilter = mProfile.getPreferredAccountsFilter();
107 ArrayList<String> params = new ArrayList<>();
108 StringBuilder sb = new StringBuilder(
109 "select t.profile, t.id from transactions t where t.description=?");
110 params.add(description);
112 if (accFilter != null) {
113 sb.append(" AND EXISTS (")
114 .append("SELECT 1 FROM transaction_accounts ta ")
115 .append("WHERE ta.profile = t.profile")
116 .append(" AND ta.transaction_id = t.id")
117 .append(" AND UPPER(ta.account_name) LIKE '%'||?||'%')");
118 params.add(accFilter.toUpperCase());
121 sb.append(" ORDER BY date desc limit 1");
123 final String sql = sb.toString();
125 debug("descr", params.toString());
127 try (Cursor c = App.getDatabase()
128 .rawQuery(sql, params.toArray(new String[]{})))
130 if (!c.moveToNext()) return;
132 String profileUUID = c.getString(0);
133 int transactionId = c.getInt(1);
134 LedgerTransaction tr;
135 MobileLedgerProfile profile = Data.getProfile(profileUUID);
136 if (profile == null) throw new RuntimeException(String.format(
137 "Unable to find profile %s, which is supposed to contain " +
138 "transaction %d with description %s", profileUUID, transactionId, description));
140 tr = profile.loadTransaction(transactionId);
141 ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
142 TableRow firstNegative = null;
143 int negativeCount = 0;
144 for (int i = 0; i < accounts.size(); i++) {
145 LedgerTransactionAccount acc = accounts.get(i);
146 NewTransactionModel.Item item;
147 if (model.getAccountCount() < i) {
148 model.addAccount(acc);
149 notifyItemInserted(i + 1);
151 item = model.getItem(i + 1);
154 .setAccountName(acc.getAccountName());
155 if (acc.isAmountSet()) item.getAccount()
156 .setAmount(acc.getAmount());
157 else item.getAccount()
159 notifyItemChanged(i + 1);
162 model.checkTransactionSubmittable(this);
163 model.setFocusedItem(1);
165 public void toggleAllEditing(boolean editable) {
166 for (int i = 0; i < model.getAccountCount(); i++) {
168 .setEditable(editable);
169 notifyItemChanged(i + 1);
170 // TODO perhaps do only one notification about the whole range [1…count]?
173 public void reset() {
174 int presentItemCount = model.getAccountCount();
176 notifyItemChanged(0); // header changed
177 notifyItemRangeChanged(1, 2); // the two empty rows
178 if (presentItemCount > 2)
179 notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone