diff options
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/Application.kt | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/Application.kt b/app/src/main/java/com/zeapo/pwdstore/Application.kt index d6c618b1..b476dd45 100644 --- a/app/src/main/java/com/zeapo/pwdstore/Application.kt +++ b/app/src/main/java/com/zeapo/pwdstore/Application.kt @@ -4,11 +4,19 @@ */ package com.zeapo.pwdstore +import android.content.SharedPreferences +import androidx.appcompat.app.AppCompatDelegate +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO +import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES +import androidx.preference.PreferenceManager import com.haroldadmin.whatthestack.WhatTheStack import timber.log.Timber @Suppress("Unused") -class Application : android.app.Application() { +class Application : android.app.Application(), SharedPreferences.OnSharedPreferenceChangeListener { + private var prefs: SharedPreferences? = null override fun onCreate() { super.onCreate() @@ -16,5 +24,28 @@ class Application : android.app.Application() { if (BuildConfig.ENABLE_DEBUG_FEATURES) { WhatTheStack(this).init() } + prefs = PreferenceManager.getDefaultSharedPreferences(this) + prefs?.registerOnSharedPreferenceChangeListener(this) + setNightMode() + } + + override fun onTerminate() { + prefs?.unregisterOnSharedPreferenceChangeListener(this) + super.onTerminate() + } + + override fun onSharedPreferenceChanged(prefs: SharedPreferences, key: String) { + if (key == "app_theme") { + setNightMode() + } + } + + private fun setNightMode() { + AppCompatDelegate.setDefaultNightMode(when (prefs?.getString("app_theme", getString(R.string.app_theme_def))) { + "light" -> MODE_NIGHT_NO + "dark" -> MODE_NIGHT_YES + "follow_system" -> MODE_NIGHT_FOLLOW_SYSTEM + else -> MODE_NIGHT_AUTO_BATTERY + }) } } |