X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FMobileLedgerProfile.java;h=c9e358e292451c83468ddefc923c2393a9df3fa4;hb=f0fecef867dd49fe41fc733c11418f95a270be4a;hp=229e53483979a7927d5184df36d46952a8059089;hpb=20c03b7a5eb152d42fbbe9ecbaae27530563b398;p=mobile-ledger-staging.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 229e5348..c9e358e2 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -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; @@ -121,7 +122,7 @@ public final class MobileLedgerProfile { result = item; } } - Data.profiles.setValue(list); + Data.profiles.postValue(list); return result; } public static void storeProfilesOrder() { @@ -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}); @@ -433,12 +452,12 @@ public final class MobileLedgerProfile { db.beginTransactionNonExclusive(); try { Object[] uuid_param = new Object[]{uuid}; - db.execSQL("delete from profiles where uuid=?", uuid_param); - db.execSQL("delete from accounts where profile=?", uuid_param); - db.execSQL("delete from account_values where profile=?", uuid_param); - db.execSQL("delete from transactions where profile=?", uuid_param); db.execSQL("delete from transaction_accounts where profile=?", uuid_param); + db.execSQL("delete from transactions where profile=?", uuid_param); + db.execSQL("delete from account_values where profile=?", uuid_param); + db.execSQL("delete from accounts where profile=?", uuid_param); db.execSQL("delete from options where profile=?", uuid_param); + db.execSQL("delete from profiles where uuid=?", uuid_param); db.setTransactionSuccessful(); } finally { @@ -527,7 +546,7 @@ public final class MobileLedgerProfile { { while (c.moveToNext()) { Currency currency = new Currency(c.getInt(0), c.getString(1), - Currency.Position.valueOf(c.getInt(2)), c.getInt(3) == 1); + Currency.Position.valueOf(c.getString(2)), c.getInt(3) == 1); result.add(currency); } } @@ -548,7 +567,7 @@ public final class MobileLedgerProfile { { if (cursor.moveToFirst()) { return new Currency(cursor.getInt(0), cursor.getString(1), - Currency.Position.valueOf(cursor.getInt(2)), cursor.getInt(3) == 1); + Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1); } return null; }