Skip to content
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

Font is not updated to the new font resource #4863

Closed
PhilipDukhov opened this issue May 24, 2024 · 0 comments · Fixed by #4864
Closed

Font is not updated to the new font resource #4863

PhilipDukhov opened this issue May 24, 2024 · 0 comments · Fixed by #4864
Assignees
Labels
bug Something isn't working resources

Comments

@PhilipDukhov
Copy link
Contributor

PhilipDukhov commented May 24, 2024

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

  • Android

Versions

  • Libraries:
    • Compose Multiplatform version: 1.6.10
  • Kotlin version: 2.0.0
  • OS architecture (x86 or arm64): arm64
  • Device (model or simulator for iOS issues): Android emulator

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.

@PhilipDukhov PhilipDukhov added bug Something isn't working submitted labels May 24, 2024
@terrakok terrakok self-assigned this May 27, 2024
terrakok pushed a commit that referenced this issue May 27, 2024
Fixes #4863

Before this update, when a new `resource` value was passed to
`org.jetbrains.compose.resources.Font` composable, it kept the original
value.

Test sample code. `Res.font` here is autogenerated from
`commonMain/composeResources/font/` folder content.
```kt
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 })
}
```

## Release Notes
### Fixes - Resources
- Fix a cached font if the resource acessor was changed
terrakok pushed a commit that referenced this issue May 28, 2024
Fixes #4863

Before this update, when a new `resource` value was passed to
`org.jetbrains.compose.resources.Font` composable, it kept the original
value.

Test sample code. `Res.font` here is autogenerated from
`commonMain/composeResources/font/` folder content.
```kt
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 })
}
```

## Release Notes
### Fixes - Resources
- Fix a cached font if the resource acessor was changed

(cherry picked from commit 0485f03)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resources
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants