]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java
stop resetting the date when an old transaction is loaded into the form
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionModel.java
index 7ff061a6ea30994bb15437d3297ee4683334f795..311226dacd6ec85504e562bc30bab73bf1697e63 100644 (file)
@@ -18,8 +18,6 @@
 package net.ktnx.mobileledger.ui.new_transaction;
 
 import android.annotation.SuppressLint;
-import android.os.Handler;
-import android.os.Looper;
 import android.text.TextUtils;
 
 import androidx.annotation.NonNull;
@@ -31,6 +29,7 @@ import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
 import net.ktnx.mobileledger.BuildConfig;
+import net.ktnx.mobileledger.db.Currency;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.db.TemplateAccount;
@@ -292,25 +291,30 @@ public class NewTransactionModel extends ViewModel {
                   if (amount != null && acc.getNegateAmount() != null && acc.getNegateAmount())
                       amount = -amount;
 
-                  // TODO currency
                   TransactionAccount accRow = new TransactionAccount(accountName);
                   accRow.setComment(accountComment);
                   if (amount != null)
                       accRow.setAmount(amount);
+                  accRow.setCurrency(
+                          extractCurrencyFromMatches(matchResult, acc.getCurrencyMatchGroup(),
+                                  acc.getCurrencyObject()));
 
                   newItems.add(accRow);
               }
 
-              new Handler(Looper.getMainLooper()).post(() -> replaceItems(newItems));
+              Misc.onMainThread(() -> replaceItems(newItems));
           });
     }
+    private String extractCurrencyFromMatches(MatchResult m, Integer group, Currency literal) {
+        return extractStringFromMatches(m, group, (literal == null) ? "" : literal.getName());
+    }
     private int extractIntFromMatches(MatchResult m, Integer group, Integer literal) {
         if (literal != null)
             return literal;
 
         if (group != null) {
             int grp = group;
-            if (grp > 0 & grp <= m.groupCount())
+            if (grp > 0 && grp <= m.groupCount())
                 try {
                     return Integer.parseInt(m.group(grp));
                 }
@@ -327,7 +331,7 @@ public class NewTransactionModel extends ViewModel {
 
         if (group != null) {
             int grp = group;
-            if (grp > 0 & grp <= m.groupCount())
+            if (grp > 0 && grp <= m.groupCount())
                 return m.group(grp);
         }
 
@@ -339,7 +343,7 @@ public class NewTransactionModel extends ViewModel {
 
         if (group != null) {
             int grp = group;
-            if (grp > 0 & grp <= m.groupCount())
+            if (grp > 0 && grp <= m.groupCount())
                 try {
                     return Float.valueOf(m.group(grp));
                 }
@@ -428,7 +432,6 @@ public class NewTransactionModel extends ViewModel {
         List<Item> list = Objects.requireNonNull(items.getValue());
         TransactionHead head = list.get(0)
                                    .toTransactionHead();
-        SimpleDate date = head.getDate();
         LedgerTransaction tr = head.asLedgerTransaction();
 
         tr.setComment(head.getComment());
@@ -466,8 +469,12 @@ public class NewTransactionModel extends ViewModel {
         List<Item> newList = new ArrayList<>();
         Item.resetIdDispenser();
 
+        Item currentHead = items.getValue()
+                                .get(0);
         TransactionHead head = new TransactionHead(tr.transaction.getDescription());
         head.setComment(tr.transaction.getComment());
+        if (currentHead instanceof TransactionHead)
+            head.setDate(((TransactionHead) currentHead).date);
 
         newList.add(head);
 
@@ -523,7 +530,7 @@ public class NewTransactionModel extends ViewModel {
             moveItemLast(newList, singlePositiveIndex);
         }
 
-        new Handler(Looper.getMainLooper()).post(() -> {
+        Misc.onMainThread(() -> {
             setItems(newList);
             noteFocusChanged(1, FocusedElement.Amount);
         });
@@ -1029,7 +1036,8 @@ public class NewTransactionModel extends ViewModel {
             return ItemType.generalData;
         }
         public LedgerTransaction asLedgerTransaction() {
-            return new LedgerTransaction(0, date, description, Data.getProfile());
+            return new LedgerTransaction(0, (date == null) ? SimpleDate.today() : date, description,
+                    Data.getProfile());
         }
         public boolean equalContents(TransactionHead other) {
             if (other == null)