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

(cherry picked from commit d03cbbe0cdc699ad15618a326c91e811f5c2e385)

# Conflicts:
#	app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsGeneralScreen.kt
This commit is contained in:
stevenyomi 2022-10-20 00:57:05 +08:00 committed by Jobobby04
parent b29fc922cb
commit cc605759d3
4 changed files with 34 additions and 30 deletions

View File

@ -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
},
)
}

View File

@ -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
@ -59,25 +58,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) } }
}

View File

@ -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
@ -20,8 +23,6 @@ import eu.kanade.domain.ui.UiPreferences
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
@ -35,7 +36,6 @@ class SettingsGeneralScreen : SearchableSettings {
@Composable
override fun getPreferences(): List<Preference> {
val scope = rememberCoroutineScope()
val prefs = remember { Injekt.get<BasePreferences>() }
val libraryPrefs = remember { Injekt.get<LibraryPreferences>() }
// SY -->
@ -73,7 +73,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,
@ -81,20 +81,21 @@ 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)
}
// SY -->
add(
Preference.PreferenceGroup(

View File

@ -42,6 +42,8 @@ import eu.kanade.data.DatabaseHandler
import eu.kanade.domain.DomainModule
import eu.kanade.domain.SYDomainModule
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
@ -156,6 +158,8 @@ class App : Application(), DefaultLifecycleObserver, ImageLoaderFactory {
}
.launchIn(ProcessLifecycleOwner.get().lifecycleScope)
setAppCompatDelegateThemeMode(Injekt.get<UiPreferences>().themeMode().get())
// Updates widget update
Injekt.get<DatabaseHandler>()
.subscribeToList { updatesViewQueries.updates(after = UpdatesGridGlanceWidget.DateLimit.timeInMillis) }