JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.24k stars 1.11k forks source link

Font is not updated to the new font resource #4863

Closed PhilipDukhov closed 1 month ago

PhilipDukhov commented 1 month ago

Describe the bug org.jetbrains.compose.resources.Font composable is ignorant to updated resource value.

This is Android only issue, but it's not reproduced on regular compose - it's related to using shared resources from common part.

Affected platforms

Versions

To Reproduce Steps to reproduce the behavior:

  1. Put fonts in commonMain/composeResources/font/ folder, so Res.font is autogenerated.
  2. Run this code snippet:
    var flag by remember {
        mutableStateOf(false)
    }
    Column {
        Text(
            "hey",
            fontFamily = FontFamily(Font(if (flag) Res.font.HelveticaNeueMedium else Res.font.COMICSANS, FontWeight.Normal))
        )
        Switch(checked = flag, onCheckedChange = { flag = it })
    }
  3. Click on Switch

Expected behavior Text font is changed

Additional context As you can see in the source code of org.jetbrains.compose.resources.Font composable, it remembers result of getResourceItemByEnvironment and doesn't update it in case new resource value is passed.

On other platforms it's not an issue because resource value is passed into rememberResourceState.

Suggested fix is passing resource to remember:

val path = remember(environment, resource) { resource.getResourceItemByEnvironment(environment).path }

Related stackoverflow question.