moko-resources
moko-resources copied to clipboard
Add common type for resource providers
Sometimes it can be useful to get resources from common code. For such situations, a special type can be provided which will have no meaning in the common code, but which needs to be passed from the platform code to the localized() method.
Example:
// common code
expect class ResourceProvider
// android code
actual typealias ResourceProvider = Context
actual class ResourceStringDesc(@StringRes private val id: Int) : StringDesc {
override fun localized(provider: ResourceProvider): String = provider.getString(id)
}
So then you can use it for common ui:
// android code
class MyActivity : AppCompatActivity() {
override fun onCreate(savedState: Bundle?) {
setContent {
CommonUi(resourceProvider = this)
}
}
}
// common ui code, but you still have access to resources
@Composable
fun CommonUi(resourceProvider: ResourceProvider) {
MR.strings.app_name.desc().localized(resourceProvider)
}
Or something else