]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/App.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
index 7935beba307d7ffe4ff1eef8dcbef45a38535e2d..a9a416e7bb3c15a8e49c51f1aedb45f1307d0bee 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;
 
 import android.app.Application;
+import android.content.SharedPreferences;
 import android.content.res.Configuration;
 import android.content.res.Resources;
-import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
 
-import androidx.room.Room;
-
-import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
+import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
 import org.jetbrains.annotations.NotNull;
 
@@ -41,26 +38,12 @@ import java.net.URL;
 import java.util.Locale;
 
 public class App extends Application {
+    public static final String PREF_NAME = "MoLe";
+    public static final String PREF_THEME_HUE = "theme-hue";
+    public static final String PREF_PROFILE_ID = "profile-id";
     public static App instance;
     private static ProfileDetailModel profileModel;
-    private MobileLedgerDatabase dbHelper;
     private boolean monthNamesPrepared = false;
-    private DB roomDatabase;
-    public static SQLiteDatabase getDatabase() {
-        if (instance == null)
-            throw new RuntimeException("Application not created yet");
-
-        return instance.getDB();
-    }
-    public static DB getRoomDB() {
-        if (instance == null)
-            throw new RuntimeException("Application not created yet");
-
-        return instance.getRoomDatabase();
-    }
-    public DB getRoomDatabase(){
-        return roomDatabase;
-    }
     public static void prepareMonthNames() {
         instance.prepareMonthNames(false);
     }
@@ -70,6 +53,21 @@ public class App extends Application {
     public static void resetAuthenticationData() {
         profileModel = null;
     }
+    public static void storeStartupProfileAndTheme(long currentProfileId, int currentTheme) {
+        SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
+        SharedPreferences.Editor editor = prefs.edit();
+        editor.putLong(PREF_PROFILE_ID, currentProfileId);
+        editor.putInt(PREF_THEME_HUE, currentTheme);
+        editor.apply();
+    }
+    public static long getStartupProfile() {
+        SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
+        return prefs.getLong(PREF_PROFILE_ID, -1);
+    }
+    public static int getStartupTheme() {
+        SharedPreferences prefs = instance.getSharedPreferences(PREF_NAME, MODE_PRIVATE);
+        return prefs.getInt(PREF_THEME_HUE, Colors.DEFAULT_HUE_DEG);
+    }
     private String getAuthURL() {
         if (profileModel != null)
             return profileModel.getUrl();
@@ -80,7 +78,7 @@ public class App extends Application {
         if (profileModel != null)
             return profileModel.getAuthUserName();
         return Data.getProfile()
-                   .getAuthUserName();
+                   .getAuthUser();
     }
     private String getAuthPassword() {
         if (profileModel != null)
@@ -92,15 +90,13 @@ public class App extends Application {
         if (profileModel != null)
             return profileModel.getUseAuthentication();
         return Data.getProfile()
-                   .isAuthEnabled();
+                   .useAuthentication();
     }
     @Override
     public void onCreate() {
         Logger.debug("flow", "App onCreate()");
         instance = this;
         super.onCreate();
-        roomDatabase = Room.databaseBuilder(this, DB.class, MobileLedgerDatabase.DB_NAME)
-                              .build();
         Data.refreshCurrencyData(Locale.getDefault());
         Authenticator.setDefault(new Authenticator() {
             @Override
@@ -135,29 +131,10 @@ public class App extends Application {
         monthNamesPrepared = true;
     }
     @Override
-    public void onTerminate() {
-        Logger.debug("flow", "App onTerminate()");
-        if (dbHelper != null)
-            dbHelper.close();
-        super.onTerminate();
-    }
-    @Override
     public void onConfigurationChanged(@NotNull Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         prepareMonthNames(true);
         Data.refreshCurrencyData(Locale.getDefault());
         Data.locale.setValue(Locale.getDefault());
     }
-    public SQLiteDatabase getDB() {
-        if (dbHelper == null)
-            initDb();
-
-        return dbHelper.getWritableDatabase();
-    }
-    private synchronized void initDb() {
-        if (dbHelper != null)
-            return;
-
-        dbHelper = new MobileLedgerDatabase(this);
-    }
 }