]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java
more pronounced day/month delimiters in the transaction list
[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 import static net.ktnx.mobileledger.db.Profile.NO_PROFILE_ID;
33
34 public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
35     private final AccountFilter filter = new AccountFilter();
36     private final AccountDAO dao = DB.get()
37                                      .getAccountDAO();
38     private long profileId = NO_PROFILE_ID;
39     public AccountAutocompleteAdapter(Context context) {
40         super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
41     }
42     public AccountAutocompleteAdapter(Context context, @NonNull Profile profile) {
43         this(context);
44         profileId = profile.getId();
45     }
46     public void setProfileId(long profileId) {
47         this.profileId = profileId;
48     }
49     @NonNull
50     @Override
51     public Filter getFilter() {
52         return filter;
53     }
54     //    @NonNull
55 //    @Override
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,
61 //                                         false);
62 //        }
63 //        Account item = getItem(position);
64 //        ((TextView) view.findViewById(android.R.id.text1)).setText(item.getName());
65 //        return view;
66 //    }
67     class AccountFilter extends Filter {
68         @Override
69         protected FilterResults performFiltering(CharSequence constraint) {
70             FilterResults results = new FilterResults();
71             if (constraint == null) {
72                 results.count = 0;
73                 return results;
74             }
75
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)
82                                   .toUpperCase()));
83             results.values = matches;
84             results.count = matches.size();
85
86             return results;
87         }
88         @Override
89         @SuppressWarnings("unchecked")
90         protected void publishResults(CharSequence constraint, FilterResults results) {
91             if (results.values == null) {
92                 notifyDataSetInvalidated();
93             }
94             else {
95                 setNotifyOnChange(false);
96                 clear();
97                 addAll((List<String>) results.values);
98                 notifyDataSetChanged();
99             }
100         }
101     }
102 }