-
-
Notifications
You must be signed in to change notification settings - Fork 26
fix: use Android-owned Configuration.uiMode for SYSTEM dark mode (#677) #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ed/src/androidMain/kotlin/com/devil/phoenixproject/ui/theme/PlatformSystemDark.android.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.devil.phoenixproject.ui.theme | ||
|
|
||
| import android.content.res.Configuration | ||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.LifecycleEventObserver | ||
| import androidx.lifecycle.compose.LocalLifecycleOwner | ||
| import co.touchlab.kermit.Logger | ||
|
|
||
| private val log = Logger.withTag("PlatformSystemDark") | ||
|
|
||
| /** | ||
| * Android-owned lifecycle-safe system appearance source. | ||
| * | ||
| * Seeds from `Configuration.uiMode` on first composition and refreshes on every | ||
| * `ON_RESUME` event so lock/unlock, display-mode changes, and other configuration | ||
| * transitions are captured. Logs a mismatch when the Compose | ||
| * `isSystemInDarkTheme()` signal disagrees with the Android-owned value — this | ||
| * telemetry is the first diagnostic that would have caught issue #677. | ||
| */ | ||
| @Composable | ||
| actual fun rememberPlatformSystemDark(): Boolean { | ||
| val context = LocalContext.current | ||
| val lifecycleOwner = LocalLifecycleOwner.current | ||
|
|
||
| fun readUiModeDark(): Boolean { | ||
| val nightMask = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK | ||
| return nightMask == Configuration.UI_MODE_NIGHT_YES | ||
| } | ||
|
|
||
| var isDark by remember { mutableStateOf(readUiModeDark()) } | ||
|
|
||
| // Capture the Compose system-dark signal during composition and bridge its latest | ||
| // value into the long-lived lifecycle observer for each resume diagnostic. | ||
| val composeSignal = isSystemInDarkTheme() | ||
| val currentComposeSignal by rememberUpdatedState(composeSignal) | ||
|
|
||
| DisposableEffect(lifecycleOwner) { | ||
| val observer = LifecycleEventObserver { _, event -> | ||
| if (event == Lifecycle.Event.ON_RESUME) { | ||
| val refreshed = readUiModeDark() | ||
| if (refreshed != isDark) { | ||
| log.i { "System dark mode changed on resume: $isDark -> $refreshed" } | ||
| } | ||
| isDark = refreshed | ||
|
|
||
| // Diagnostic: log mismatch between Android-owned value and the latest | ||
| // recomposed Compose signal. `rememberUpdatedState` keeps this observer | ||
| // current without re-registering it on every recomposition. | ||
| if (currentComposeSignal != refreshed) { | ||
| log.w { | ||
| "MISMATCH: Configuration.uiMode says dark=$refreshed " + | ||
| "but Compose isSystemInDarkTheme() says dark=$currentComposeSignal" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| lifecycleOwner.lifecycle.addObserver(observer) | ||
| onDispose { | ||
| lifecycleOwner.lifecycle.removeObserver(observer) | ||
| } | ||
| } | ||
|
|
||
| return isDark | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
shared/src/commonMain/kotlin/com/devil/phoenixproject/ui/theme/PlatformSystemDark.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.devil.phoenixproject.ui.theme | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
|
|
||
| /** | ||
| * Returns the platform-owned system dark-mode state, refreshed on lifecycle resume. | ||
| * | ||
| * On Android this reads `Configuration.uiMode` (the OS-owned source of truth) and | ||
| * is refreshed on `ON_RESUME` so lock/unlock and other configuration changes are | ||
| * captured without relying on the transient Compose `isSystemInDarkTheme()` signal. | ||
| * | ||
| * On iOS this delegates to `isSystemInDarkTheme()` which is already lifecycle-stable. | ||
| * | ||
| * Use this instead of `isSystemInDarkTheme()` when resolving `ThemeMode.SYSTEM`. | ||
| */ | ||
| @Composable | ||
| expect fun rememberPlatformSystemDark(): Boolean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
shared/src/iosMain/kotlin/com/devil/phoenixproject/ui/theme/PlatformSystemDark.ios.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.devil.phoenixproject.ui.theme | ||
|
|
||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.runtime.Composable | ||
|
|
||
| /** | ||
| * iOS: delegate to the platform `isSystemInDarkTheme()` signal. | ||
| * iOS does not have the same transient-signal issue as Android's Compose bridge; | ||
| * the SwiftUI/UIViewController lifecycle keeps the appearance stable. | ||
| */ | ||
| @Composable | ||
| actual fun rememberPlatformSystemDark(): Boolean = isSystemInDarkTheme() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.