Created
February 3, 2024 03:06
-
-
Save LloydBlv/60ab86be3c62c92716d4b60ebdc378a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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