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