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 import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID;
34 public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
35 private final AccountFilter filter = new AccountFilter();
36 private final AccountDAO dao = DB.get()
38 private long profileId = NO_PROFILE_ID;
39 public AccountAutocompleteAdapter(Context context) {
40 super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
42 public AccountAutocompleteAdapter(Context context, @NonNull Profile profile) {
44 profileId = profile.getId();
46 public void setProfileId(long profileId) {
47 this.profileId = profileId;
51 public Filter getFilter() {
56 // public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
57 // View view = convertView;
58 // if (view == null) {
59 // view = LayoutInflater.from(parent.getContext())
60 // .inflate(android.R.layout.simple_dropdown_item_1line, parent,
63 // Account item = getItem(position);
64 // ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
67 class AccountFilter extends Filter {
69 protected FilterResults performFiltering(CharSequence constraint) {
70 FilterResults results = new FilterResults();
71 if (constraint == null) {
76 Logger.debug("acc", String.format("Looking for account '%s'", constraint));
77 final List<String> matches = AccountDAO.unbox(
78 (profileId == NO_PROFILE_ID) ? dao.lookupNamesByNameSync(
79 String.valueOf(constraint)
80 .toUpperCase()) : dao.lookupNamesInProfileByNameSync(profileId,
81 String.valueOf(constraint)
83 results.values = matches;
84 results.count = matches.size();
89 @SuppressWarnings("unchecked")
90 protected void publishResults(CharSequence constraint, FilterResults results) {
91 if (results.values == null) {
92 notifyDataSetInvalidated();
95 setNotifyOnChange(false);
97 addAll((List<String>) results.values);
98 notifyDataSetChanged();