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
This commit is contained in:
stevenyomi 2022-10-20 00:57:05 +08:00 committed by GitHub
parent 84bcd8d1d2
commit d03cbbe0cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
@ -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) } }
}

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
@ -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<Preference> {
val scope = rememberCoroutineScope()
val prefs = remember { Injekt.get<BasePreferences>() }
val libraryPrefs = remember { Injekt.get<LibraryPreferences>() }
return mutableListOf<Preference>().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)
}
}
}

View file

@ -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<UiPreferences>().themeMode().get())
// Updates widget update
Injekt.get<DatabaseHandler>()
.subscribeToList { updatesViewQueries.updates(after = UpdatesGridGlanceWidget.DateLimit.timeInMillis) }