]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmountsAutocompleteAdapter.java
show current account balance when choosing account in new transactions
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / AccountWithAmountsAutocompleteAdapter.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.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.ArrayAdapter;
25 import android.widget.Filter;
26 import android.widget.TextView;
27
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.dao.AccountDAO;
33 import net.ktnx.mobileledger.model.Data;
34 import net.ktnx.mobileledger.utils.Logger;
35 import net.ktnx.mobileledger.utils.Misc;
36
37 import java.util.List;
38
39 public class AccountWithAmountsAutocompleteAdapter extends ArrayAdapter<AccountWithAmounts> {
40     private final AccountFilter filter = new AccountFilter();
41     private final long profileId;
42     public AccountWithAmountsAutocompleteAdapter(Context context, @NonNull Profile profile) {
43         super(context, R.layout.account_autocomplete_row);
44         profileId = profile.getId();
45     }
46     @NonNull
47     @Override
48     public Filter getFilter() {
49         return filter;
50     }
51     @NonNull
52     @Override
53     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
54         View view = convertView;
55         if (view == null) {
56             view = LayoutInflater.from(parent.getContext())
57                                  .inflate(R.layout.account_autocomplete_row, parent, false);
58         }
59         AccountWithAmounts item = getItem(position);
60         ((TextView) view.findViewById(R.id.account_name)).setText(item.account.getName());
61         StringBuilder amountsText = new StringBuilder();
62         for (AccountValue amt : item.amounts) {
63             if (amountsText.length() != 0)
64                 amountsText.append('\n');
65             String currency = amt.getCurrency();
66             if (Misc.emptyIsNull(currency) != null)
67                 amountsText.append(currency)
68                            .append(' ');
69             amountsText.append(Data.formatNumber(amt.getValue()));
70         }
71         ((TextView) view.findViewById(R.id.amounts)).setText(amountsText.toString());
72
73         return view;
74     }
75     class AccountFilter extends Filter {
76         private final AccountDAO dao = DB.get()
77                                          .getAccountDAO();
78         @Override
79         protected FilterResults performFiltering(CharSequence constraint) {
80             FilterResults results = new FilterResults();
81             if (constraint == null) {
82                 results.count = 0;
83                 return results;
84             }
85
86             Logger.debug("acc", String.format("Looking for account '%s'", constraint));
87             final List<AccountWithAmounts> matches =
88                     dao.lookupWithAmountsInProfileByNameSync(profileId, String.valueOf(constraint)
89                                                                               .toUpperCase());
90             results.values = matches;
91             results.count = matches.size();
92
93             return results;
94         }
95         @Override
96         @SuppressWarnings("unchecked")
97         protected void publishResults(CharSequence constraint, FilterResults results) {
98             if (results.values == null) {
99                 notifyDataSetInvalidated();
100             }
101             else {
102                 setNotifyOnChange(false);
103                 clear();
104                 addAll((List<AccountWithAmounts>) results.values);
105                 notifyDataSetChanged();
106             }
107         }
108     }
109 }