Skip to content

Instantly share code, notes, and snippets.

@LloydBlv
Created February 3, 2024 03:06
Show Gist options
  • Save LloydBlv/60ab86be3c62c92716d4b60ebdc378a7 to your computer and use it in GitHub Desktop.
Save LloydBlv/60ab86be3c62c92716d4b60ebdc378a7 to your computer and use it in GitHub Desktop.
@Stable
sealed interface StringResource {
fun resolve(context: Context): String
data class Text(val text: String): StringResource {
override fun resolve(context: Context): String {
return text
}
}
data class ResId(@StringRes val stringId: Int): StringResource {
override fun resolve(context: Context): String {
return context.getString(stringId)
}
}
data class ResIdWithParams(@StringRes val stringId: Int, val params: List<Any>): StringResource {
override fun resolve(context: Context): String {
return context.getString(stringId, *params.toTypedArray())
}
}
@Composable
fun resolve(): String {
return when (this) {
is ResId -> stringResource(id = stringId)
is ResIdWithParams -> stringResource(id = stringId, *params.toTypedArray())
is Text -> text
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment