Anyone have a SystemUiController fork that they li...
# compose-android
c
Anyone have a SystemUiController fork that they like? I can copy the code into my codebase, but i almost always rather use a community supported lib if possible. I think all I ever really need SystemUiController for is to switch system bar icons from light to dark and back again.
e
I ended up not using anything from the library. I used
activity.enableEdgeToEdge
from the androidx activity library and that covered my use cases for drawing under the status and nav bars + coloring them
s
Does
enableEdgeToEdge
not fill your requirements here?
a
All you need is this:
Copy code
val windowInsetsController: WindowInsetsControllerCompat?
    @Composable get() {
        val view = LocalView.current
        return remember(view) {
            view.context.findWindow()?.let { WindowCompat.getInsetsController(it, view) }
        }
    }

private tailrec fun Context.findWindow(): Window? =
    when (this) {
        is Activity -> window
        is ContextWrapper -> baseContext.findWindow()
        else -> null
    }
And use it like this:
Copy code
val controller = windowInsetsController
SideEffect {
    controller?.isAppearanceLightStatusBars = !darkTheme
}
c
Thanks everyone. I've been out of the compose game for like ~2 months or so due to some family stuff so I wasn't sure if there was something obvious I missed. let me try this out. I wonder if enableEdgeToEdge finally made it to stable!
Also, i wonder if theres room for a kmp compose lib for this. not sure what ios does for status bar colors, etc.
a
Unless I'm missing something,
enableEdgeToEdge
can only help you set up edge to edge, and you still need to change status bar appearance yourself if you are disabling activity recreation.
c
Hm. What does activity recreation have to do with status bar appearance?
a
If you are specifying
uiMode
in
android:configChanges
, the activity won’t be recreated when dark mode changes, and status bar icon color won’t change automatically.
🤯 1
today i learned 1
f
this is what the NIA app does, after setting edge to edge
Copy code
DisposableEffect(darkTheme) {
                enableEdgeToEdge(
                    statusBarStyle = SystemBarStyle.auto(
                        android.graphics.Color.TRANSPARENT,
                        android.graphics.Color.TRANSPARENT,
                    ) { darkTheme },
                    navigationBarStyle = SystemBarStyle.auto(
                        lightScrim,
                        darkScrim,
                    ) { darkTheme },
                )
                onDispose {}
            }
https://github.com/android/nowinandroid/blob/5f5bd91a686a414007c9ce3c7acb26d2b0bd2[…]ain/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt
1
a
Yeah you can use that if you want to also set navigation bar color, but if all you want is to set status bar icon color, my snippet is enough (and the
findWindow
part is not needed if you put it in an activity class like NIA does).
c
cant wait until we hit some api level where all of this is handled for us. lol
i forget when the auto color change for system bars was introduced 😄