管理本機資源環境
您可能需要管理應用程式內設定,讓使用者能夠自訂體驗,例如更改語言或佈景主題。 若要動態更新應用程式的資源環境,您可以配置應用程式使用的以下資源相關設定:
地區設定
每個平台處理地區設定(如語言和地區)的方式各不相同。作為臨時解決方案,在實作通用的公開 API 之前,您需要在共用程式碼中定義一個通用的進入點。然後,使用平台特定的 API 為每個平台提供對應的宣告:
- Android:
context.resources.configuration.locale - iOS:
NSLocale.preferredLanguages - 桌面:
Locale.getDefault() - Web:
window.navigator.languages
在
common原始碼集中,使用expect關鍵字定義預期的LocalAppLocale物件:kotlinvar customAppLocale by mutableStateOf<String?>(null) expect object LocalAppLocale { val current: String @Composable get @Composable infix fun provides(value: String?): ProvidedValue<*> } @Composable fun AppEnvironment(content: @Composable () -> Unit) { CompositionLocalProvider( LocalAppLocale provides customAppLocale, ) { key(customAppLocale) { content() } } }在 Android 原始碼集中,新增使用
context.resources.configuration.locale的actual實作:kotlinactual object LocalAppLocale { private var default: Locale? = null actual val current: String @Composable get() = Locale.getDefault().toString() @Composable actual infix fun provides(value: String?): ProvidedValue<*> { val configuration = LocalConfiguration.current if (default == null) { default = Locale.getDefault() } val new = when(value) { null -> default!! else -> Locale(value) } Locale.setDefault(new) configuration.setLocale(new) val resources = LocalContext.current.resources resources.updateConfiguration(configuration, resources.displayMetrics) return LocalConfiguration.provides(configuration) } }在 iOS 原始碼集中,新增修改
NSLocale.preferredLanguages的actual實作:kotlin@OptIn(InternalComposeUiApi::class) actual object LocalAppLocale { private const val LANG_KEY = "AppleLanguages" private val default = NSLocale.preferredLanguages.first() as String private val LocalAppLocale = staticCompositionLocalOf { default } actual val current: String @Composable get() = LocalAppLocale.current @Composable actual infix fun provides(value: String?): ProvidedValue<*> { val new = value ?: default if (value == null) { NSUserDefaults.standardUserDefaults.removeObjectForKey(LANG_KEY) } else { NSUserDefaults.standardUserDefaults.setObject(arrayListOf(new), LANG_KEY) } return LocalAppLocale.provides(new) } }在桌面原始碼集中,新增使用
Locale.getDefault()更新 JVM 預設地區設定的actual實作:kotlinactual object LocalAppLocale { private var default: Locale? = null private val LocalAppLocale = staticCompositionLocalOf { Locale.getDefault().toString() } actual val current: String @Composable get() = LocalAppLocale.current @Composable actual infix fun provides(value: String?): ProvidedValue<*> { if (default == null) { default = Locale.getDefault() } val new = when(value) { null -> default!! else -> Locale(value) } Locale.setDefault(new) return LocalAppLocale.provides(new.toString()) } }對於 Web 平台,繞過
window.navigator.languages屬性的唯讀限制,以引入自訂的地區設定邏輯:kotlinexternal object window { var __customLocale: String? } actual object LocalAppLocale { private val LocalAppLocale = staticCompositionLocalOf { Locale.current } actual val current: String @Composable get() = LocalAppLocale.current.toString() @Composable actual infix fun provides(value: String?): ProvidedValue<*> { window.__customLocale = value?.replace('_', '-') return LocalAppLocale.provides(Locale.current) } }接著,在瀏覽器的
index.html中,在載入應用程式指令碼之前放入以下程式碼:html<html lang="en"> <head> <meta charset="UTF-8"> ... <script> var currentLanguagesImplementation = Object.getOwnPropertyDescriptor(Navigator.prototype, "languages"); var newLanguagesImplementation = Object.assign({}, currentLanguagesImplementation, { get: function () { if (window.__customLocale) { return [window.__customLocale]; } else { return currentLanguagesImplementation.get.apply(this); } } }); Object.defineProperty(Navigator.prototype, "languages", newLanguagesImplementation) </script> <script src="skiko.js"></script> ... </head> <body></body> <script src="webApp.js"></script> </html>
佈景主題
Compose Multiplatform 透過 isSystemInDarkTheme() 定義目前的佈景主題。不同平台的佈景主題處理方式各不相同:
- Android 透過以下按位元運算定義佈景主題:kotlin
Resources.getConfiguration().uiMode and Configuration.UI_MODE_NIGHT_MASK - iOS、桌面與 Web 平台使用
LocalSystemTheme.current。
作為臨時解決方案,在實作通用的公開 API 之前,您可以使用 expect-actual 機制來管理平台特定的佈景主題自訂:
在共用程式碼中,使用
expect關鍵字定義預期的LocalAppTheme物件:kotlinvar customAppThemeIsDark by mutableStateOf<Boolean?>(null) expect object LocalAppTheme { val current: Boolean @Composable get @Composable infix fun provides(value: Boolean?): ProvidedValue<*> } @Composable fun AppEnvironment(content: @Composable () -> Unit) { CompositionLocalProvider( LocalAppTheme provides customAppThemeIsDark, ) { key(customAppThemeIsDark) { content() } } }在 Android 程式碼中,新增使用
LocalConfigurationAPI 的actual實作:kotlinactual object LocalAppTheme { actual val current: Boolean @Composable get() = (LocalConfiguration.current.uiMode and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES @Composable actual infix fun provides(value: Boolean?): ProvidedValue<*> { val new = if (value == null) { LocalConfiguration.current } else { Configuration(LocalConfiguration.current).apply { uiMode = when (value) { true -> (uiMode and UI_MODE_NIGHT_MASK.inv()) or UI_MODE_NIGHT_YES false -> (uiMode and UI_MODE_NIGHT_MASK.inv()) or UI_MODE_NIGHT_NO } } } return LocalConfiguration.provides(new) } }在 iOS、桌面與 Web 平台上,您可以直接更改
LocalSystemTheme:kotlin@OptIn(InternalComposeUiApi::class) actual object LocalAppTheme { actual val current: Boolean @Composable get() = LocalSystemTheme.current == SystemTheme.Dark @Composable actual infix fun provides(value: Boolean?): ProvidedValue<*> { val new = when(value) { true -> SystemTheme.Dark false -> SystemTheme.Light null -> LocalSystemTheme.current } return LocalSystemTheme.provides(new) } }
密度
若要變更應用程式的解析度 Density,您可以使用所有平台都支援的通用 LocalDensity API:
var customAppDensity by mutableStateOf<Density?>(null)
object LocalAppDensity {
val current: Density
@Composable get() = LocalDensity.current
@Composable
infix fun provides(value: Density?): ProvidedValue<*> {
val new = value ?: LocalDensity.current
return LocalDensity.provides(new)
}
}
@Composable
fun AppEnvironment(content: @Composable () -> Unit) {
CompositionLocalProvider(
LocalAppDensity provides customAppDensity,
) {
key(customAppDensity) {
content()
}
}
}