]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/TransactionDescriptionAutocompleteAdapter.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / TransactionDescriptionAutocompleteAdapter.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.TransactionDAO;
27 import net.ktnx.mobileledger.utils.Logger;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class TransactionDescriptionAutocompleteAdapter extends ArrayAdapter<String> {
33     private final TransactionFilter filter = new TransactionFilter();
34     private final TransactionDAO dao = DB.get()
35                                          .getTransactionDAO();
36     public TransactionDescriptionAutocompleteAdapter(Context context) {
37         super(context, android.R.layout.simple_dropdown_item_1line, new ArrayList<>());
38     }
39     @NonNull
40     @Override
41     public Filter getFilter() {
42         return filter;
43     }
44     class TransactionFilter extends Filter {
45         @Override
46         protected FilterResults performFiltering(CharSequence constraint) {
47             FilterResults results = new FilterResults();
48             if (constraint == null) {
49                 results.count = 0;
50                 return results;
51             }
52
53             Logger.debug("acc", String.format("Looking for description '%s'", constraint));
54             final List<String> matches = TransactionDAO.unbox(dao.lookupDescriptionSync(
55                     String.valueOf(constraint)
56                           .toUpperCase()));
57             results.values = matches;
58             results.count = matches.size();
59
60             return results;
61         }
62         @Override
63         @SuppressWarnings("unchecked")
64         protected void publishResults(CharSequence constraint, FilterResults results) {
65             if (results.values == null) {
66                 notifyDataSetInvalidated();
67             }
68             else {
69                 setNotifyOnChange(false);
70                 clear();
71                 addAll((List<String>) results.values);
72                 notifyDataSetChanged();
73             }
74         }
75     }
76 }