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;
28 import java.util.ArrayList;
29 import java.util.List;
31 public class AccountAutocompleteAdapter extends ArrayAdapter<Account> {
32 private final AccountFilter filter = new AccountFilter();
33 private final AccountDAO dao = DB.get()
35 private String profileUUID;
36 public AccountAutocompleteAdapter(Context context) {
37 super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
39 public void setProfileUUID(String profileUUID) {
40 this.profileUUID = profileUUID;
44 public Filter getFilter() {
49 // public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
50 // View view = convertView;
51 // if (view == null) {
52 // view = LayoutInflater.from(parent.getContext())
53 // .inflate(android.R.layout.simple_dropdown_item_1line, parent,
56 // Account item = getItem(position);
57 // ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
60 class AccountFilter extends Filter {
62 protected FilterResults performFiltering(CharSequence constraint) {
63 FilterResults results = new FilterResults();
64 if (constraint == null) {
69 final List<Account> matches =
70 (profileUUID == null) ? dao.lookupByNameSync(String.valueOf(constraint))
71 : dao.lookupInProfileByNameSync(profileUUID,
72 String.valueOf(constraint));
73 results.values = matches;
74 results.count = matches.size();
79 @SuppressWarnings("unchecked")
80 protected void publishResults(CharSequence constraint, FilterResults results) {
81 if (results.values == null) {
82 notifyDataSetInvalidated();
85 setNotifyOnChange(false);
87 addAll((List<Account>) results.values);
88 notifyDataSetChanged();