From d03cbbe0cdc699ad15618a326c91e811f5c2e385 Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Thu, 20 Oct 2022 00:57:05 +0800 Subject: [PATCH] Fix language/theme settings dialog and remove delay (#8244) * Fix language/theme settings dialog and remove delay * inline UI preferences * use `by remember` * remove unused variable * remove unused import --- .../eu/kanade/domain/ui/model/ThemeMode.kt | 12 ++++++++ .../screen/SettingsAppearanceScreen.kt | 19 ++---------- .../settings/screen/SettingsGeneralScreen.kt | 29 ++++++++++--------- app/src/main/java/eu/kanade/tachiyomi/App.kt | 4 +++ 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/ui/model/ThemeMode.kt b/app/src/main/java/eu/kanade/domain/ui/model/ThemeMode.kt index 5f0a74be93..0954fe690c 100644 --- a/app/src/main/java/eu/kanade/domain/ui/model/ThemeMode.kt +++ b/app/src/main/java/eu/kanade/domain/ui/model/ThemeMode.kt @@ -1,7 +1,19 @@ package eu.kanade.domain.ui.model +import androidx.appcompat.app.AppCompatDelegate + enum class ThemeMode { LIGHT, DARK, SYSTEM, } + +fun setAppCompatDelegateThemeMode(themeMode: ThemeMode) { + AppCompatDelegate.setDefaultNightMode( + when (themeMode) { + ThemeMode.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO + ThemeMode.DARK -> AppCompatDelegate.MODE_NIGHT_YES + ThemeMode.SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + }, + ) +} diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt index cc909933e9..30749e0110 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsAppearanceScreen.kt @@ -4,7 +4,6 @@ import android.app.Activity import android.content.Context import android.os.Build import androidx.annotation.StringRes -import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.ReadOnlyComposable @@ -15,12 +14,12 @@ import androidx.compose.ui.res.stringResource import androidx.core.app.ActivityCompat import eu.kanade.domain.ui.UiPreferences import eu.kanade.domain.ui.model.ThemeMode +import eu.kanade.domain.ui.model.setAppCompatDelegateThemeMode import eu.kanade.presentation.more.settings.Preference import eu.kanade.presentation.util.collectAsState import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.system.isTablet import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.merge import uy.kohesive.injekt.Injekt @@ -56,25 +55,13 @@ class SettingsAppearanceScreen : SearchableSettings { val appThemePref = uiPreferences.appTheme() val amoledPref = uiPreferences.themeDarkAmoled() - LaunchedEffect(Unit) { - themeModePref.changes() - .drop(1) - .debounce(1000) - .collectLatest { - AppCompatDelegate.setDefaultNightMode( - when (it) { - ThemeMode.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO - ThemeMode.DARK -> AppCompatDelegate.MODE_NIGHT_YES - ThemeMode.SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - }, - ) - } + LaunchedEffect(themeMode) { + setAppCompatDelegateThemeMode(themeMode) } LaunchedEffect(Unit) { merge(appThemePref.changes(), amoledPref.changes()) .drop(2) - .debounce(1000) .collectLatest { (context as? Activity)?.let { ActivityCompat.recreate(it) } } } diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsGeneralScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsGeneralScreen.kt index 06939f9d16..906b8f4e17 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsGeneralScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsGeneralScreen.kt @@ -7,9 +7,12 @@ import android.provider.Settings import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatDelegate import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.ReadOnlyComposable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.core.os.LocaleListCompat @@ -18,8 +21,6 @@ import eu.kanade.domain.library.service.LibraryPreferences import eu.kanade.presentation.more.settings.Preference import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.system.LocaleHelper -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import org.xmlpull.v1.XmlPullParser import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -33,7 +34,6 @@ class SettingsGeneralScreen : SearchableSettings { @Composable override fun getPreferences(): List { - val scope = rememberCoroutineScope() val prefs = remember { Injekt.get() } val libraryPrefs = remember { Injekt.get() } return mutableListOf().apply { @@ -67,7 +67,7 @@ class SettingsGeneralScreen : SearchableSettings { } val langs = remember { getLangs(context) } - val currentLanguage = remember { AppCompatDelegate.getApplicationLocales().get(0)?.toLanguageTag() ?: "" } + var currentLanguage by remember { mutableStateOf(AppCompatDelegate.getApplicationLocales().get(0)?.toLanguageTag() ?: "") } add( Preference.PreferenceItem.BasicListPreference( value = currentLanguage, @@ -75,19 +75,20 @@ class SettingsGeneralScreen : SearchableSettings { subtitle = "%s", entries = langs, onValueChanged = { newValue -> - scope.launch { - delay(1000) - val locale = if (newValue.isEmpty()) { - LocaleListCompat.getEmptyLocaleList() - } else { - LocaleListCompat.forLanguageTags(newValue) - } - AppCompatDelegate.setApplicationLocales(locale) - } + currentLanguage = newValue true }, ), ) + + LaunchedEffect(currentLanguage) { + val locale = if (currentLanguage.isEmpty()) { + LocaleListCompat.getEmptyLocaleList() + } else { + LocaleListCompat.forLanguageTags(currentLanguage) + } + AppCompatDelegate.setApplicationLocales(locale) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/App.kt b/app/src/main/java/eu/kanade/tachiyomi/App.kt index f7d44ce915..23956a826b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/App.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/App.kt @@ -27,6 +27,8 @@ import coil.util.DebugLogger import eu.kanade.data.DatabaseHandler import eu.kanade.domain.DomainModule import eu.kanade.domain.base.BasePreferences +import eu.kanade.domain.ui.UiPreferences +import eu.kanade.domain.ui.model.setAppCompatDelegateThemeMode import eu.kanade.tachiyomi.crash.CrashActivity import eu.kanade.tachiyomi.crash.GlobalExceptionHandler import eu.kanade.tachiyomi.data.coil.DomainMangaKeyer @@ -121,6 +123,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory { } .launchIn(ProcessLifecycleOwner.get().lifecycleScope) + setAppCompatDelegateThemeMode(Injekt.get().themeMode().get()) + // Updates widget update Injekt.get() .subscribeToList { updatesViewQueries.updates(after = UpdatesGridGlanceWidget.DateLimit.timeInMillis) }