moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

Add common type for resource providers

Open y9san9 opened this issue 3 years ago • 3 comments

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

y9san9 avatar Mar 01 '22 03:03 y9san9