From c42d517f6b13652a7fe9edf21b2441bf35ee75b1 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 24 Jul 2021 22:14:25 +0700 Subject: [PATCH] Apply default night mode earlier (#5593) --- app/src/main/java/eu/kanade/tachiyomi/App.kt | 14 ++++++++++++++ .../ui/base/activity/BaseThemedActivity.kt | 15 ++------------- .../kanade/tachiyomi/ui/reader/ReaderActivity.kt | 4 ++-- .../ui/setting/SettingsGeneralController.kt | 5 ----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index fa7176854..f74985a48 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -9,6 +9,7 @@ import android.content.Intent import android.content.IntentFilter import android.os.Build import android.webkit.WebView +import androidx.appcompat.app.AppCompatDelegate import androidx.core.app.NotificationManagerCompat import androidx.core.content.getSystemService import androidx.lifecycle.Lifecycle @@ -25,7 +26,9 @@ import eu.kanade.tachiyomi.data.coil.ByteBufferFetcher import eu.kanade.tachiyomi.data.coil.MangaCoverFetcher import eu.kanade.tachiyomi.data.coil.TachiyomiImageDecoder import eu.kanade.tachiyomi.data.notification.Notifications +import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.notification @@ -96,6 +99,17 @@ open class App : Application(), LifecycleObserver, ImageLoaderFactory { } } .launchIn(ProcessLifecycleOwner.get().lifecycleScope) + + preferences.themeMode() + .asImmediateFlow { + AppCompatDelegate.setDefaultNightMode( + when (it) { + PreferenceValues.ThemeMode.light -> AppCompatDelegate.MODE_NIGHT_NO + PreferenceValues.ThemeMode.dark -> AppCompatDelegate.MODE_NIGHT_YES + PreferenceValues.ThemeMode.system -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + } + ) + }.launchIn(ProcessLifecycleOwner.get().lifecycleScope) } override fun attachBaseContext(base: Context) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt index ffe30c6f9..e32b734a7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.base.activity import android.content.Context import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.app.AppCompatDelegate import androidx.lifecycle.lifecycleScope import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferenceValues @@ -25,7 +24,7 @@ abstract class BaseThemedActivity : AppCompatActivity() { } override fun onCreate(savedInstanceState: Bundle?) { - applyThemePreferences(preferences) + applyAppTheme(preferences) Injekt.get().incognitoMode() .asImmediateFlow { @@ -37,7 +36,7 @@ abstract class BaseThemedActivity : AppCompatActivity() { } companion object { - fun AppCompatActivity.applyThemePreferences(preferences: PreferencesHelper) { + fun AppCompatActivity.applyAppTheme(preferences: PreferencesHelper) { val resIds = mutableListOf() when (preferences.appTheme().get()) { PreferenceValues.AppTheme.MONET -> { @@ -77,16 +76,6 @@ abstract class BaseThemedActivity : AppCompatActivity() { resIds.forEach { setTheme(it) } - - lifecycleScope.launchWhenCreated { - AppCompatDelegate.setDefaultNightMode( - when (preferences.themeMode().get()) { - PreferenceValues.ThemeMode.light -> AppCompatDelegate.MODE_NIGHT_NO - PreferenceValues.ThemeMode.dark -> AppCompatDelegate.MODE_NIGHT_YES - PreferenceValues.ThemeMode.system -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - } - ) - } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index c0a884318..3d1456080 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -46,7 +46,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.toggle import eu.kanade.tachiyomi.databinding.ReaderActivityBinding import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity -import eu.kanade.tachiyomi.ui.base.activity.BaseThemedActivity.Companion.applyThemePreferences +import eu.kanade.tachiyomi.ui.base.activity.BaseThemedActivity.Companion.applyAppTheme import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.AddToLibraryFirst @@ -141,7 +141,7 @@ class ReaderActivity : BaseRxActivity() * Called when the activity is created. Initializes the presenter and configuration. */ override fun onCreate(savedInstanceState: Bundle?) { - applyThemePreferences(preferences) + applyAppTheme(preferences) super.onCreate(savedInstanceState) binding = ReaderActivityBinding.inflate(layoutInflater) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index a517b517d..6fab558ad 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -112,11 +112,6 @@ class SettingsGeneralController : SettingsController() { } summary = "%s" - - onChange { - activity?.recreate() - true - } } listPreference { key = Keys.appTheme