From bc463f3c64ab04ae766aa2f161cce8d5f0e4b9f3 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 29 Mar 2020 20:03:50 +0530 Subject: Significantly improve app theming (#679) * Update CHANGELOG * Use outlined box style in folder creation dialog * Add user-facing choice for app theme Signed-off-by: Harsh Shandilya --- .../main/java/com/zeapo/pwdstore/Application.kt | 33 +++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'app/src/main/java') 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 + }) } } -- cgit v1.2.3