From 2afaddb21088bd171dd4f56e0c77384de2d9c3a3 Mon Sep 17 00:00:00 2001 From: Victor Ude Date: Sat, 19 Oct 2024 18:18:55 -0500 Subject: [PATCH] Update compose-multiplatform-resources-usage.md Suggesting `produceState` for string resource loading within a composition with an example of use. --- .../compose-multiplatform-resources-usage.md | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/topics/compose/compose-multiplatform-resources-usage.md b/topics/compose/compose-multiplatform-resources-usage.md index b89df540..5b1bcce5 100644 --- a/topics/compose/compose-multiplatform-resources-usage.md +++ b/topics/compose/compose-multiplatform-resources-usage.md @@ -101,18 +101,37 @@ To get string resources as a `String`, use the following code: ```kotlin @Composable -fun stringResource(resource: StringResource): String {...} +fun stringResource( + resource: StringResource, + default: String = "", +): State = + produceState(initialValue = default) { + value = getString(resource = resource) + } @Composable -fun stringResource(resource: StringResource, vararg formatArgs: Any): String {...} +fun stringResource( + resource: StringResource, + vararg formatArgs: Any, + default: String = "", +): State = + produceState(initialValue = default) { + value = getString( + resource = resource, + formatArgs = formatArgs, + ) + } ``` For example: ```kotlin -Text(stringResource(Res.string.app_name)) +var appName by stringResource(Res.string.app_name) +Text(text = appName) ``` +This example uses `produceState` within `stringResource` to load the string from resources. The `appName` variable is then updated on recomposition. +