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.widget.ArrayAdapter;
22 import android.widget.Filter;
24 import androidx.annotation.NonNull;
26 import net.ktnx.mobileledger.dao.AccountDAO;
27 import net.ktnx.mobileledger.utils.Logger;
29 import java.util.ArrayList;
30 import java.util.List;
32 public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
33 private final AccountFilter filter = new AccountFilter();
34 private final AccountDAO dao = DB.get()
36 private long profileId;
37 public AccountAutocompleteAdapter(Context context) {
38 super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
40 public AccountAutocompleteAdapter(Context context, @NonNull Profile profile) {
42 profileId = profile.getId();
44 public void setProfileId(long profileId) {
45 this.profileId = profileId;
49 public Filter getFilter() {
54 // public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
55 // View view = convertView;
56 // if (view == null) {
57 // view = LayoutInflater.from(parent.getContext())
58 // .inflate(android.R.layout.simple_dropdown_item_1line, parent,
61 // Account item = getItem(position);
62 // ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
65 class AccountFilter extends Filter {
67 protected FilterResults performFiltering(CharSequence constraint) {
68 FilterResults results = new FilterResults();
69 if (constraint == null) {
74 Logger.debug("acc", String.format("Looking for account '%s'", constraint));
75 final List<String> matches = AccountDAO.unbox((profileId == 0) ? dao.lookupByNameSync(
76 String.valueOf(constraint)
77 .toUpperCase()) : dao.lookupInProfileByNameSync(profileId,
78 String.valueOf(constraint)
80 results.values = matches;
81 results.count = matches.size();
86 @SuppressWarnings("unchecked")
87 protected void publishResults(CharSequence constraint, FilterResults results) {
88 if (results.values == null) {
89 notifyDataSetInvalidated();
92 setNotifyOnChange(false);
94 addAll((List<String>) results.values);
95 notifyDataSetChanged();