5

I'm using the Accompanist systemUiController library and I'm setting darkIcons=false

while in Light Theme but the effect doesn't apply on devices with android 11. It seems to work on devices with android 10 for example.

This is the code with which I'm trying to set the colors.

 val systemUiController = rememberSystemUiController()
 systemUiController.setStatusBarColor(color=statusBarColor,darkIcons=false) 

where statusBarColor is a darker color which represents the reason I want white foreground/icons

While in Dark Theme it works to set darkIcons to both true and false and the effect applies accordingly

This is the status bar on LightTheme with darkIcons=false and darkIcons=true

This is the status bar on DarkTheme with darkIcons=false enter image description here

This is the status bar on DarkTheme with darkIcons=true enter image description here

For reference this is my whole Theme.kt

private val LightBase = ASBTheme(
    material = lightColors(
        background = FigmaPrimaryWhite,
        onBackground = FigmaTextBlack,
        surface = FigmaPrimaryWhite,
        onSurface = FigmaTextBlack,
        primary = FigmaSecondaryAvastBlue,
        error = FigmaStatusPink,
    ),
    textColorLabel = FigmaSecondaryAvastPurple,
    colorAccent = FigmaPrimaryGreen,
    ... //bunch of custom colors
)

private val DarkBase = ASBTheme(
    material = darkColors(
        background = FigmaPrimaryBlackBg,
        onBackground = FigmaTextWhite,
        surface = FigmaSecondaryAvastBlueDark,
        onSurface = FigmaTextWhite,
        primary = FigmaSecondaryBlackDark,
        error = FigmaStatusPink
    ),
        textColorLabel = FigmaSecondaryAvastPurpleBright,
        colorAccent = FigmaStatusGreen,
        ... //bunch of custom colors
)

private val LocalAsbTheme = staticCompositionLocalOf { LightBase }
var navBarColor: Color? = null
var statusBarColor: Color? = null

@Composable
fun ASBTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkBase
    } else {
        LightBase
    }
    if (darkTheme) {
        navBarColor = DarkBase.background
        statusBarColor = DarkBase.primary
    } else {
        navBarColor = LightBase.background
        statusBarColor = LightBase.primary
    }

    SetBarsTheme(statusBarColor!!, navBarColor!!)

    CompositionLocalProvider(
        LocalAsbTheme provides colors,
    ) {
        MaterialTheme(
            colors = colors.material,
            content = content,
        )
    }
}

val MaterialTheme.asbTheme: ASBTheme
    @Composable
    @ReadOnlyComposable
    get() = LocalAsbTheme.current

SetBarsTheme() is where I'm trying to set the status bar colors depending on the lifecycle event so that the colors would maintain after onPause() / onStop(). I also tried to set the colors outside of this logic and the bug still persists.

@Composable
fun SetBarsTheme(
    statusBarColor: Color,
    navigationBarColor: Color,
    darkIcons:Boolean=false,
) {
    val lifecycleOwner = LocalLifecycleOwner.current
    val systemUiController = rememberSystemUiController()

    DisposableEffect(lifecycleOwner) {
        // Create an observer that triggers our remembered callbacks
        // for sending analytics events
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_RESUME) {
               systemUiController.setStatusBarColor(color=statusBarColor,darkIcons)
                systemUiController.setNavigationBarColor(color=navigationBarColor)
            }
        }

        // Add the observer to the lifecycle
        lifecycleOwner.lifecycle.addObserver(observer)

        // When the effect leaves the Composition, remove the observer
        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
        }
    }
}
Tiberiu Paduraru
  • 313
  • 3
  • 17
  • Did you find solution? I have the same problem. – Arsenius Jan 27 '22 at 03:33
  • 1
    @tim, there's a [fixed bug](https://github.com/google/accompanist/issues/949), make sure you're using the latest Accompanist version. If you still able to reproduce it, you should [report it](https://github.com/google/accompanist/issues/new?assignees=alexvanyo&labels=&template=system-ui-controller-bug-report.md&title=%5BSystem+UI+Controller%5D+), including used device/dependencies versions. – Phil Dukhov Mar 15 '22 at 04:58
  • @PhilipDukhov probably right. I haven't been able to test it due to compileSdk not being 31 yet. Post the answer if you want the bounty – Tim Mar 18 '22 at 14:44

1 Answers1

2

There's a fixed bug, make sure you're using the latest Accompanist version.

If you still able to reproduce it, you should report it, including used device/dependencies versions.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
  • It does indeed fix the issue, however I had my own dependency on androidx.core and I had to bump that to 1.8.0-alpha04 as well for it to work. – Tim Mar 31 '22 at 10:10