]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java
fix duplicate accounts in template account autocompletion
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / AccountAutocompleteAdapter.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.db;
19
20 import android.content.Context;
21 import android.widget.ArrayAdapter;
22 import android.widget.Filter;
23
24 import androidx.annotation.NonNull;
25
26 import net.ktnx.mobileledger.dao.AccountDAO;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
32     private final AccountFilter filter = new AccountFilter();
33     private final AccountDAO dao = DB.get()
34                                      .getAccountDAO();
35     private String profileUUID;
36     public AccountAutocompleteAdapter(Context context) {
37         super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
38     }
39     public void setProfileUUID(String profileUUID) {
40         this.profileUUID = profileUUID;
41     }
42     @NonNull
43     @Override
44     public Filter getFilter() {
45         return filter;
46     }
47     //    @NonNull
48 //    @Override
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,
54 //                                         false);
55 //        }
56 //        Account item = getItem(position);
57 //        ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
58 //        return view;
59 //    }
60     class AccountFilter extends Filter {
61         @Override
62         protected FilterResults performFiltering(CharSequence constraint) {
63             FilterResults results = new FilterResults();
64             if (constraint == null) {
65                 results.count = 0;
66                 return results;
67             }
68
69             final List<String> matches = AccountDAO.unbox(
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();
75
76             return results;
77         }
78         @Override
79         @SuppressWarnings("unchecked")
80         protected void publishResults(CharSequence constraint, FilterResults results) {
81             if (results.values == null) {
82                 notifyDataSetInvalidated();
83             }
84             else {
85                 setNotifyOnChange(false);
86                 clear();
87                 addAll((List<String>) results.values);
88                 notifyDataSetChanged();
89             }
90         }
91     }
92 }