]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
new currency ID: without (not working) dance around duplicate IDs
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / Currency.java
index 3cb70f8bd6cec7ea199a4d81f9bfdcf17828a4c2..28e6d03e9fe6c2007c1b4ece489472c027ab030b 100644 (file)
@@ -24,6 +24,7 @@ import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.DiffUtil;
 
 import net.ktnx.mobileledger.App;
+import net.ktnx.mobileledger.utils.Misc;
 
 public class Currency {
     public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
@@ -59,30 +60,43 @@ public class Currency {
     }
     public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
         SQLiteDatabase db = App.getDatabase();
-        int attempts = 0;
-        while (true) {
-            if (++attempts > 10)
-                throw new RuntimeException("Giving up getting next ID after 10 attempts");
 
-            try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
-                c.moveToNext();
-                int nextId = c.getInt(0) + 1;
-                db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
-                        new Object[]{nextId, name, position.toString(), hasGap});
-
-                this.id = nextId;
-                break;
-            }
+        try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
+            c.moveToNext();
+            this.id = c.getInt(0) + 1;
         }
+        db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
+                new Object[]{this.id, name, position.toString(), hasGap});
 
         this.name = name;
         this.position = position;
         this.hasGap = hasGap;
     }
     public static Currency loadByName(String name) {
-        MobileLedgerProfile profile = Data.profile.getValue();
+        MobileLedgerProfile profile = Data.getProfile();
         return profile.loadCurrencyByName(name);
     }
+    static public boolean equal(Currency left, Currency right) {
+        if (left == null) {
+            return right == null;
+        }
+        else
+            return left.equals(right);
+    }
+    static public boolean equal(Currency left, String right) {
+        right = Misc.emptyIsNull(right);
+        if (left == null) {
+            return right == null;
+        }
+        else {
+            String leftName = Misc.emptyIsNull(left.getName());
+            if (leftName == null) {
+                return right == null;
+            }
+            else
+                return leftName.equals(right);
+        }
+    }
     public int getId() {
         return id;
     }