]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java
account name auto-complete: convert input to upper case
[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 import net.ktnx.mobileledger.utils.Logger;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
33     private final AccountFilter filter = new AccountFilter();
34     private final AccountDAO dao = DB.get()
35                                      .getAccountDAO();
36     private String profileUUID;
37     public AccountAutocompleteAdapter(Context context) {
38         super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
39     }
40     public void setProfileUUID(String profileUUID) {
41         this.profileUUID = profileUUID;
42     }
43     @NonNull
44     @Override
45     public Filter getFilter() {
46         return filter;
47     }
48     //    @NonNull
49 //    @Override
50 //    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
51 //        View view = convertView;
52 //        if (view == null) {
53 //            view = LayoutInflater.from(parent.getContext())
54 //                                 .inflate(android.R.layout.simple_dropdown_item_1line, parent,
55 //                                         false);
56 //        }
57 //        Account item = getItem(position);
58 //        ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
59 //        return view;
60 //    }
61     class AccountFilter extends Filter {
62         @Override
63         protected FilterResults performFiltering(CharSequence constraint) {
64             FilterResults results = new FilterResults();
65             if (constraint == null) {
66                 results.count = 0;
67                 return results;
68             }
69
70             Logger.debug("acc", String.format("Looking for account '%s'", constraint));
71             final List<String> matches = AccountDAO.unbox(
72                     (profileUUID == null) ? dao.lookupByNameSync(String.valueOf(constraint)
73                                                                        .toUpperCase())
74                                           : dao.lookupInProfileByNameSync(profileUUID,
75                                                   String.valueOf(constraint)
76                                                         .toUpperCase()));
77             results.values = matches;
78             results.count = matches.size();
79
80             return results;
81         }
82         @Override
83         @SuppressWarnings("unchecked")
84         protected void publishResults(CharSequence constraint, FilterResults results) {
85             if (results.values == null) {
86                 notifyDataSetInvalidated();
87             }
88             else {
89                 setNotifyOnChange(false);
90                 clear();
91                 addAll((List<String>) results.values);
92                 notifyDataSetChanged();
93             }
94         }
95     }
96 }