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 5f0a74be9..0954fe690 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 cc909933e..30749e011 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 06939f9d1..906b8f4e1 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 f7d44ce91..23956a826 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) }