Complete changes that must be done available here
That if
snippet exists just because of the Component Preview within Android Studio - where there's never an available Activity to attach to! (When you're actually running an Application your view
won't be in edit mode - thus actually running the inner statement only in real scenarios).
Since it's logically only executed within real application we can do some casts to retrieve the current window
by assuming that the view.context
is an Activity
. If that's an Activity you can access the currentWindow
property and use that as the window
parameter for the recommended approach.
So we end up with the following code - with some extra refactoring to reduce code duplication - that casts the current view's context to an Activity and does the appropriate settings:
@Composable
fun YourAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true,
content: @Composable () -> Unit
) {
val colorScheme = pickColorScheme(dynamicColor, darkTheme)
val view = LocalView.current
if (!view.isInEditMode) {
/* getting the current window by tapping into the Activity */
val currentWindow = (view.context as? Activity)?.window
?: throw Exception("Not in an activity - unable to get Window reference")
SideEffect {
/* the default code did the same cast here - might as well use our new variable! */
currentWindow.statusBarColor = colorScheme.primary.toArgb()
/* accessing the insets controller to change appearance of the status bar, with 100% less deprecation warnings */
WindowCompat.getInsetsController(currentWindow, view).isAppearanceLightStatusBars =
darkTheme
}
}
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
}