]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/db/DB.java
drop MobileLedgerDatabase, move initComplete to DB
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / db / DB.java
index 7dcb0897746b29ecb5696716b51c9924f647b58b..cff1d9c14f62452ad80c0af679e81099477a1263 100644 (file)
 package net.ktnx.mobileledger.db;
 
 import android.content.res.Resources;
+import android.database.Cursor;
 import android.database.SQLException;
 
 import androidx.annotation.NonNull;
+import androidx.lifecycle.MutableLiveData;
 import androidx.room.Database;
 import androidx.room.Room;
 import androidx.room.RoomDatabase;
@@ -29,10 +31,16 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
 
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.dao.AccountDAO;
+import net.ktnx.mobileledger.dao.AccountValueDAO;
 import net.ktnx.mobileledger.dao.CurrencyDAO;
+import net.ktnx.mobileledger.dao.DescriptionHistoryDAO;
+import net.ktnx.mobileledger.dao.OptionDAO;
+import net.ktnx.mobileledger.dao.ProfileDAO;
 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
+import net.ktnx.mobileledger.dao.TransactionAccountDAO;
 import net.ktnx.mobileledger.dao.TransactionDAO;
+import net.ktnx.mobileledger.utils.Logger;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -52,6 +60,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 abstract public class DB extends RoomDatabase {
     public static final int REVISION = 59;
     public static final String DB_NAME = "MoLe.db";
+    public static final MutableLiveData<Boolean> initComplete = new MutableLiveData<>(false);
     private static DB instance;
     public static DB get() {
         if (instance != null)
@@ -72,7 +81,8 @@ abstract public class DB extends RoomDatabase {
                                                                  multiVersionMigration(34, 40),
                                                                  singleVersionMigration(41),
                                                                  multiVersionMigration(41, 58),
-                                                                 })
+                                                                 singleVersionMigration(59)
+                                  })
                                   .addCallback(new Callback() {
                                       @Override
                                       public void onOpen(@NonNull SupportSQLiteDatabase db) {
@@ -92,6 +102,34 @@ abstract public class DB extends RoomDatabase {
                 String fileName = String.format(Locale.US, "db_%d", toVersion);
 
                 applyRevisionFile(db, fileName);
+
+                // when migrating to version 59, migrate profile/theme options to the
+                // SharedPreferences
+                if (toVersion == 59) {
+                    try (Cursor c = db.query(
+                            "SELECT p.id, p.theme_hue FROM profiles p WHERE p.id=(SELECT o.value " +
+                            "FROM options WHERE o.profile_uid IS NULL AND o.name=?",
+                            new Object[]{"profile_id"}))
+                    {
+                        if (c.moveToFirst()) {
+                            long currentProfileId = c.getLong(0);
+                            int currentTheme = c.getInt(1);
+
+                            if (currentProfileId >= 0 && currentTheme >= 0) {
+                                App.storeStartupProfileAndTheme(currentProfileId, currentTheme);
+                            }
+                        }
+                    }
+                }
+            }
+        };
+    }
+    private static Migration dummyVersionMigration(int toVersion) {
+        return new Migration(toVersion - 1, toVersion) {
+            @Override
+            public void migrate(@NonNull SupportSQLiteDatabase db) {
+                Logger.debug("db",
+                        String.format(Locale.ROOT, "Dummy DB migration to version %d", toVersion));
             }
         };
     }
@@ -167,5 +205,15 @@ abstract public class DB extends RoomDatabase {
 
     public abstract AccountDAO getAccountDAO();
 
+    public abstract AccountValueDAO getAccountValueDAO();
+
     public abstract TransactionDAO getTransactionDAO();
+
+    public abstract TransactionAccountDAO getTransactionAccountDAO();
+
+    public abstract OptionDAO getOptionDAO();
+
+    public abstract DescriptionHistoryDAO getDescriptionHistoryDAO();
+
+    public abstract ProfileDAO getProfileDAO();
 }