]> git.ktnx.net Git - mobile-ledger.git/commitdiff
store new currencies upon account amounts storage
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 17 Sep 2020 07:37:28 +0000 (07:37 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 17 Sep 2020 13:25:28 +0000 (13:25 +0000)
fixes a crash when a previous transaction is selected in the new
transaction screen that uses currencies not added via the currency
dialog

app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java

index 4ac99fe14ad0149dffd985c7dc666c34710ac7a6..62ab8f4b867b7eb765e02721f94599a0ce0b8713 100644 (file)
@@ -20,6 +20,7 @@ package net.ktnx.mobileledger.model;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.text.TextUtils;
 import android.util.SparseArray;
 
 import androidx.annotation.Nullable;
@@ -332,6 +333,24 @@ public final class MobileLedgerProfile {
     }
     public void storeAccountValue(SQLiteDatabase db, int generation, String name, String currency,
                                   Float amount) {
+        if (!TextUtils.isEmpty(currency)) {
+            boolean exists;
+            try (Cursor c = db.rawQuery("select 1 from currencies where name=?",
+                    new String[]{currency}))
+            {
+                exists = c.moveToFirst();
+            }
+            if (!exists) {
+                db.execSQL(
+                        "insert into currencies(id, name, position, has_gap) values((select max" +
+                        "(id) from currencies)+1, ?, ?, ?)", new Object[]{currency,
+                                                                          Objects.requireNonNull(
+                                                                                  Data.currencySymbolPosition.getValue()).toString(),
+                                                                          Data.currencyGap.getValue()
+                        });
+            }
+        }
+
         db.execSQL("replace into account_values(profile, account, " +
                    "currency, value, generation) values(?, ?, ?, ?, ?);",
                 new Object[]{uuid, name, Misc.emptyIsNull(currency), amount, generation});