2 * Copyright © 2021 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.db;
20 import android.content.Context;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ArrayAdapter;
25 import android.widget.Filter;
26 import android.widget.TextView;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.dao.AccountDAO;
33 import net.ktnx.mobileledger.model.Data;
34 import net.ktnx.mobileledger.utils.Logger;
35 import net.ktnx.mobileledger.utils.Misc;
37 import java.util.List;
39 public class AccountWithAmountsAutocompleteAdapter extends ArrayAdapter<AccountWithAmounts> {
40 private final AccountFilter filter = new AccountFilter();
41 private final long profileId;
42 public AccountWithAmountsAutocompleteAdapter(Context context, @NonNull Profile profile) {
43 super(context, R.layout.account_autocomplete_row);
44 profileId = profile.getId();
48 public Filter getFilter() {
53 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
54 View view = convertView;
56 view = LayoutInflater.from(parent.getContext())
57 .inflate(R.layout.account_autocomplete_row, parent, false);
59 AccountWithAmounts item = getItem(position);
60 ((TextView) view.findViewById(R.id.account_name)).setText(item.account.getName());
61 StringBuilder amountsText = new StringBuilder();
62 for (AccountValue amt : item.amounts) {
63 if (amountsText.length() != 0)
64 amountsText.append('\n');
65 String currency = amt.getCurrency();
66 if (Misc.emptyIsNull(currency) != null)
67 amountsText.append(currency)
69 amountsText.append(Data.formatNumber(amt.getValue()));
71 ((TextView) view.findViewById(R.id.amounts)).setText(amountsText.toString());
75 class AccountFilter extends Filter {
76 private final AccountDAO dao = DB.get()
79 protected FilterResults performFiltering(CharSequence constraint) {
80 FilterResults results = new FilterResults();
81 if (constraint == null) {
86 Logger.debug("acc", String.format("Looking for account '%s'", constraint));
87 final List<AccountWithAmounts> matches =
88 dao.lookupWithAmountsInProfileByNameSync(profileId, String.valueOf(constraint)
90 results.values = matches;
91 results.count = matches.size();
96 @SuppressWarnings("unchecked")
97 protected void publishResults(CharSequence constraint, FilterResults results) {
98 if (results.values == null) {
99 notifyDataSetInvalidated();
102 setNotifyOnChange(false);
104 addAll((List<AccountWithAmounts>) results.values);
105 notifyDataSetChanged();