]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java
focus the first amount after loading a previous transaction
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionModel.java
index 060046c43ce4f87fb3b7fb87885356b0e9b501ef..ac0cf4b39a703ac1f57669705f37b6db3a4b80df 100644 (file)
@@ -451,6 +451,8 @@ public class NewTransactionModel extends ViewModel {
             else
                 item.resetAmount();
         }
+        if (BuildConfig.DEBUG)
+            dumpItemList("Loaded previous transaction", newList);
 
         if (singleNegativeIndex != -1) {
             firstNegative.resetAmount();
@@ -461,9 +463,9 @@ public class NewTransactionModel extends ViewModel {
             moveItemLast(newList, singlePositiveIndex);
         }
 
-        noteFocusChanged(1, FocusedElement.Description);
-
         setItems(newList);
+
+        noteFocusChanged(1, FocusedElement.Amount);
     }
     /**
      * A transaction is submittable if:
@@ -738,8 +740,9 @@ public class NewTransactionModel extends ViewModel {
                         list = copyList(list);
                         liveListCopied = true;
                     }
-                    Item item = emptyItems.remove(1);
-                    list.remove(item);
+                    // the list is a copy, so the empty item is no longer present
+                    Item itemToRemove = emptyItems.remove(1);
+                    removeItemById(list, itemToRemove.id);
                     listChanged = true;
                 }
 
@@ -752,8 +755,8 @@ public class NewTransactionModel extends ViewModel {
                             list = copyList(list);
                             liveListCopied = true;
                         }
-                        Item item = emptyItems.get(0);
-                        list.remove(item);
+                        // the list is a copy, so the empty item is no longer present
+                        removeItemById(list, emptyItems.get(0).id);
                         listChanged = true;
                     }
                 }
@@ -791,6 +794,19 @@ public class NewTransactionModel extends ViewModel {
             setItemsWithoutSubmittableChecks(list);
         }
     }
+    private void removeItemById(@NotNull List<Item> list, int id) {
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
+            list.removeIf(item -> item.id == id);
+        }
+        else {
+            for (Item item : list) {
+                if (item.id == id) {
+                    list.remove(item);
+                    break;
+                }
+            }
+        }
+    }
     @SuppressLint("DefaultLocale")
     private void dumpItemList(@NotNull String msg, @NotNull List<Item> list) {
         Logger.debug("submittable", "== Dump of all items " + msg);