]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
make Currency loadable by id
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
index a3157cd83c9b9418aaf850a6fa4061a55e87b6a3..b8085765eb069048f78cfe5621e93ef8e5887268 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2020 Damyan Ivanov.
+ * Copyright © 2021 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
 
 package net.ktnx.mobileledger.model;
 
+import android.content.Context;
+import android.content.Intent;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
+import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.SparseArray;
 
@@ -29,6 +32,8 @@ import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DbOpQueue;
 import net.ktnx.mobileledger.json.API;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailActivity;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailFragment;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 import net.ktnx.mobileledger.utils.SimpleDate;
@@ -161,6 +166,17 @@ public final class MobileLedgerProfile {
             db.endTransaction();
         }
     }
+    static public void startEditProfileActivity(Context context, MobileLedgerProfile profile) {
+        Intent intent = new Intent(context, ProfileDetailActivity.class);
+        Bundle args = new Bundle();
+        if (profile != null) {
+            int index = Data.getProfileIndex(profile);
+            if (index != -1)
+                intent.putExtra(ProfileDetailFragment.ARG_ITEM_ID, index);
+        }
+        intent.putExtras(args);
+        context.startActivity(intent, args);
+    }
     public HledgerVersion getDetectedVersion() {
         return detectedVersion;
     }
@@ -613,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),