]> git.ktnx.net Git - mobile-ledger.git/commitdiff
show current account balance when choosing account in new transactions
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 7 May 2021 20:47:39 +0000 (23:47 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 8 May 2021 15:19:11 +0000 (18:19 +0300)
app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java
app/src/main/java/net/ktnx/mobileledger/db/AccountAutocompleteAdapter.java
app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmounts.java
app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmountsAutocompleteAdapter.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java
app/src/main/res/layout/account_autocomplete_row.xml [new file with mode: 0644]

index 429e1643465f99f9d2164d61a4fd29aef763de76..0cfed1b64c2e5ad4d13845197839069356a54c80 100644 (file)
@@ -106,9 +106,9 @@ public abstract class AccountDAO extends BaseDAO<Account> {
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
            "ORDER BY ordering, name_upper, rowid ")
-    public abstract LiveData<List<AccountNameContainer>> lookupInProfileByName(long profileId,
-                                                                               @NonNull
-                                                                                       String term);
+    public abstract LiveData<List<AccountNameContainer>> lookupNamesInProfileByName(long profileId,
+                                                                                    @NonNull
+                                                                                            String term);
 
     @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
@@ -116,22 +116,32 @@ public abstract class AccountDAO extends BaseDAO<Account> {
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
            "ORDER BY ordering, name_upper, rowid ")
-    public abstract List<AccountNameContainer> lookupInProfileByNameSync(long profileId,
-                                                                         @NonNull String term);
+    public abstract List<AccountNameContainer> lookupNamesInProfileByNameSync(long profileId,
+                                                                              @NonNull String term);
+
+    @Query("SELECT * FROM accounts " +
+           "WHERE profile_id=:profileId AND name_upper LIKE '%%'||:term||'%%' " +
+           "ORDER BY  CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
+           "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
+           "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
+           "               ELSE 9 END, name_upper, rowid ")
+    public abstract List<AccountWithAmounts> lookupWithAmountsInProfileByNameSync(long profileId,
+                                                                                  @NonNull
+                                                                                          String term);
 
     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
-    public abstract LiveData<List<AccountNameContainer>> lookupByName(@NonNull String term);
+    public abstract LiveData<List<AccountNameContainer>> lookupNamesByName(@NonNull String term);
 
     @Query("SELECT DISTINCT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " +
            "               WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " +
            "               WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " +
            "               ELSE 9 END AS ordering " + "FROM accounts " +
            "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ")
-    public abstract List<AccountNameContainer> lookupByNameSync(@NonNull String term);
+    public abstract List<AccountNameContainer> lookupNamesByNameSync(@NonNull String term);
 
     @Query("SELECT * FROM accounts WHERE profile_id = :profileId")
     public abstract List<Account> allForProfileSync(long profileId);
index 76673edfb7f6667c082d52843a99a482c6142f3b..6a92b6170214a9026fba0e57e40c640b4d218c60 100644 (file)
@@ -75,11 +75,11 @@ public class AccountAutocompleteAdapter extends ArrayAdapter<String> {
 
             Logger.debug("acc", String.format("Looking for account '%s'", constraint));
             final List<String> matches = AccountDAO.unbox(
-                    (profileId == NO_PROFILE_ID) ? dao.lookupByNameSync(String.valueOf(constraint)
-                                                                              .toUpperCase())
-                                                 : dao.lookupInProfileByNameSync(profileId,
-                                                         String.valueOf(constraint)
-                                                               .toUpperCase()));
+                    (profileId == NO_PROFILE_ID) ? dao.lookupNamesByNameSync(
+                            String.valueOf(constraint)
+                                  .toUpperCase()) : dao.lookupNamesInProfileByNameSync(profileId,
+                            String.valueOf(constraint)
+                                  .toUpperCase()));
             results.values = matches;
             results.count = matches.size();
 
index 499a2b9c75cf8104be6e28abf6a65c76990eec14..86742ca23b831d23d14fbfc54c48585d95a4531a 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.ktnx.mobileledger.db;
 
+import androidx.annotation.NonNull;
 import androidx.room.Embedded;
 import androidx.room.Relation;
 
@@ -27,4 +28,9 @@ public class AccountWithAmounts {
     public Account account;
     @Relation(parentColumn = "id", entityColumn = "account_id")
     public List<AccountValue> amounts;
+    @NonNull
+    @Override
+    public String toString() {
+        return account.getName();
+    }
 }
diff --git a/app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmountsAutocompleteAdapter.java b/app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmountsAutocompleteAdapter.java
new file mode 100644 (file)
index 0000000..e99df9f
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright © 2021 Damyan Ivanov.
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package net.ktnx.mobileledger.db;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.Filter;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
+
+import java.util.List;
+
+public class AccountWithAmountsAutocompleteAdapter extends ArrayAdapter<AccountWithAmounts> {
+    private final AccountFilter filter = new AccountFilter();
+    private final long profileId;
+    public AccountWithAmountsAutocompleteAdapter(Context context, @NonNull Profile profile) {
+        super(context, R.layout.account_autocomplete_row);
+        profileId = profile.getId();
+    }
+    @NonNull
+    @Override
+    public Filter getFilter() {
+        return filter;
+    }
+    @NonNull
+    @Override
+    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
+        View view = convertView;
+        if (view == null) {
+            view = LayoutInflater.from(parent.getContext())
+                                 .inflate(R.layout.account_autocomplete_row, parent, false);
+        }
+        AccountWithAmounts item = getItem(position);
+        ((TextView) view.findViewById(R.id.account_name)).setText(item.account.getName());
+        StringBuilder amountsText = new StringBuilder();
+        for (AccountValue amt : item.amounts) {
+            if (amountsText.length() != 0)
+                amountsText.append('\n');
+            String currency = amt.getCurrency();
+            if (Misc.emptyIsNull(currency) != null)
+                amountsText.append(currency)
+                           .append(' ');
+            amountsText.append(Data.formatNumber(amt.getValue()));
+        }
+        ((TextView) view.findViewById(R.id.amounts)).setText(amountsText.toString());
+
+        return view;
+    }
+    class AccountFilter extends Filter {
+        private final AccountDAO dao = DB.get()
+                                         .getAccountDAO();
+        @Override
+        protected FilterResults performFiltering(CharSequence constraint) {
+            FilterResults results = new FilterResults();
+            if (constraint == null) {
+                results.count = 0;
+                return results;
+            }
+
+            Logger.debug("acc", String.format("Looking for account '%s'", constraint));
+            final List<AccountWithAmounts> matches =
+                    dao.lookupWithAmountsInProfileByNameSync(profileId, String.valueOf(constraint)
+                                                                              .toUpperCase());
+            results.values = matches;
+            results.count = matches.size();
+
+            return results;
+        }
+        @Override
+        @SuppressWarnings("unchecked")
+        protected void publishResults(CharSequence constraint, FilterResults results) {
+            if (results.values == null) {
+                notifyDataSetInvalidated();
+            }
+            else {
+                setNotifyOnChange(false);
+                clear();
+                addAll((List<AccountWithAmounts>) results.values);
+                notifyDataSetChanged();
+            }
+        }
+    }
+}
index 7039f53a9b2ea479835c729b6ceb18678d506d44..229789e25c81a3bdf981042ce1b36334f315586f 100644 (file)
@@ -35,7 +35,7 @@ import androidx.recyclerview.widget.RecyclerView;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.databinding.NewTransactionAccountRowBinding;
-import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
+import net.ktnx.mobileledger.db.AccountWithAmountsAutocompleteAdapter;
 import net.ktnx.mobileledger.model.Currency;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
@@ -125,8 +125,9 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
         NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
                                                                     .getContext();
 
-        b.accountRowAccName.setAdapter(new AccountAutocompleteAdapter(b.getRoot()
-                                                                       .getContext(), mProfile));
+        b.accountRowAccName.setAdapter(new AccountWithAmountsAutocompleteAdapter(b.getRoot()
+                                                                                  .getContext(),
+                mProfile));
 
         decimalSeparator = "";
         Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf(
@@ -497,8 +498,8 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
                                     presentAccountName, incomingAccountName,
                                     acc.getAccountNameCursorPosition()));
                     // avoid triggering completion pop-up
-                    AccountAutocompleteAdapter a =
-                            (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
+                    AccountWithAmountsAutocompleteAdapter a =
+                            (AccountWithAmountsAutocompleteAdapter) b.accountRowAccName.getAdapter();
                     try {
                         b.accountRowAccName.setAdapter(null);
                         b.accountRowAccName.setText(incomingAccountName);
diff --git a/app/src/main/res/layout/account_autocomplete_row.xml b/app/src/main/res/layout/account_autocomplete_row.xml
new file mode 100644 (file)
index 0000000..19ea353
--- /dev/null
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+  ~ Copyright © 2021 Damyan Ivanov.
+  ~ This file is part of MoLe.
+  ~ MoLe is free software: you can distribute it and/or modify it
+  ~ under the term of the GNU General Public License as published by
+  ~ the Free Software Foundation, either version 3 of the License, or
+  ~ (at your opinion), any later version.
+  ~
+  ~ MoLe is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  ~ GNU General Public License terms for details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with MoLe. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="@dimen/thumb_row_height"
+    android:padding="@dimen/half_text_margin"
+    >
+
+    <TextView
+        android:id="@+id/account_name"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:text="TextView\non two lines"
+        android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"
+        />
+    <TextView
+        android:id="@+id/amounts"
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="@dimen/text_margin"
+        android:gravity="end"
+        android:text="TextView"
+        android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@id/account_name"
+        app:layout_goneMarginStart="0dp"
+        />
+</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file