]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/App.java
move setting case_sensitive_like=ON to helper.onOpen
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
index 920187e9fb54d98b69e0766ff097031ae0b53c7a..b47e6d7faf14533a272517e4ac971badb0266061 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 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.preference.PreferenceManager;
 import android.util.Log;
 
 import net.ktnx.mobileledger.model.Data;
@@ -31,16 +29,18 @@ import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
+import org.jetbrains.annotations.NotNull;
+
 import java.net.Authenticator;
 import java.net.MalformedURLException;
 import java.net.PasswordAuthentication;
 import java.net.URL;
-
-import static net.ktnx.mobileledger.ui.activity.SettingsActivity.PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS;
+import java.util.Locale;
 
 public class App extends Application {
     public static App instance;
     private MobileLedgerDatabase dbHelper;
+    private boolean monthNamesPrepared = false;
     public static SQLiteDatabase getDatabase() {
         if (instance == null) throw new RuntimeException("Application not created yet");
 
@@ -51,13 +51,7 @@ public class App extends Application {
         Logger.debug("flow", "App onCreate()");
         instance = this;
         super.onCreate();
-        updateMonthNames();
-        SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
-        Data.optShowOnlyStarred.set(p.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
-        SharedPreferences.OnSharedPreferenceChangeListener handler =
-                (preference, value) -> Data.optShowOnlyStarred
-                        .set(preference.getBoolean(PREF_KEY_SHOW_ONLY_STARRED_ACCOUNTS, false));
-        p.registerOnSharedPreferenceChangeListener(handler);
+        Data.refreshCurrencyData(Locale.getDefault());
         Authenticator.setDefault(new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
@@ -83,26 +77,35 @@ public class App extends Application {
             }
         });
     }
-    private void updateMonthNames() {
+    public static void prepareMonthNames() {
+        instance.prepareMonthNames(false);
+    }
+    private void prepareMonthNames(boolean force) {
+        if (force || monthNamesPrepared)
+            return;
         Resources rm = getResources();
         Globals.monthNames = rm.getStringArray(R.array.month_names);
+        monthNamesPrepared = true;
     }
     @Override
     public void onTerminate() {
         Logger.debug("flow", "App onTerminate()");
-        if (dbHelper != null) dbHelper.close();
+        if (dbHelper != null)
+            dbHelper.close();
         super.onTerminate();
     }
     @Override
-    public void onConfigurationChanged(Configuration newConfig) {
+    public void onConfigurationChanged(@NotNull Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
-        updateMonthNames();
+        prepareMonthNames(true);
+        Data.refreshCurrencyData(Locale.getDefault());
+        Data.locale.setValue(Locale.getDefault());
     }
     public SQLiteDatabase getDB() {
-        if (dbHelper == null) initDb();
+        if (dbHelper == null)
+            initDb();
 
         final SQLiteDatabase db = dbHelper.getWritableDatabase();
-        db.execSQL("pragma case_sensitive_like=ON;");
 
         return db;
     }