From: Damyan Ivanov Date: Fri, 29 Jan 2021 05:23:05 +0000 (+0000) Subject: make Currency loadable by id X-Git-Tag: v0.17.0~209 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=ac0613b80e71c41dc885d2eed7c00a5cfa587c1d;p=mobile-ledger.git make Currency loadable by id --- diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java index 5f488e28..1c7dbf7f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java @@ -76,6 +76,10 @@ public class Currency { MobileLedgerProfile profile = Data.getProfile(); return profile.loadCurrencyByName(name); } + public static Currency loadById(int id) { + MobileLedgerProfile profile = Data.getProfile(); + return profile.loadCurrencyById(id); + } static public boolean equal(Currency left, Currency right) { if (left == null) { return right == null; 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 0e8c0448..b8085765 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -629,6 +629,25 @@ public final class MobileLedgerProfile { new AccountAndTransactionListSaver(this, accounts, transactions); accountAndTransactionListSaver.start(); } + private Currency tryLoadCurrencyById(SQLiteDatabase db, int id) { + try (Cursor cursor = db.rawQuery( + "SELECT c.id, c.name, c.position, c.has_gap FROM currencies c WHERE c.id=?", + new String[]{String.valueOf(id)})) + { + if (cursor.moveToFirst()) { + return new Currency(cursor.getInt(0), cursor.getString(1), + Currency.Position.valueOf(cursor.getString(2)), cursor.getInt(3) == 1); + } + return null; + } + } + public Currency loadCurrencyById(int id) { + SQLiteDatabase db = App.getDatabase(); + Currency result = tryLoadCurrencyById(db, id); + if (result == null) + throw new RuntimeException(String.format("Unable to load currency with id '%d'", id)); + return result; + } public enum FutureDates { None(0), OneWeek(7), TwoWeeks(14), OneMonth(30), TwoMonths(60), ThreeMonths(90),