icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development

Home Page:https://moko.icerock.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convenient Jetpack compose call

ln-12 opened this issue · comments

commented

In Jetpack compose, you can do something like text = stringResource(id = R.string.email) inside a composable which uses ContextAmbient inside. For now, with this plugin, the code would look something like text = getMyString().toString(context = ContextAmbient.current.applicationContext). Moreover, I would have to write a function for each string resource as I can't access them like in R.string.something. Is there a more convenient way of doing it?

hi @ln-12 ! your sample getMyString().toString(context = ContextAmbient.current.applicationContext) use StringDesc, but for your case text = stringResource(id = R.string.email) you need StringResource.
in MR class generated by plugin you have MR.strings with StringResource fields - for android in StringResource you have field resourceId. so you can just call text = stringResource(id = MR.strings.email.resourceId)

for better support of StringDesc in Compose i think we should add later support module where work with ContextAmbient will be inbox

commented

Ah cool, the hint text = stringResource(id = MR.strings.email.resourceId) did the trick. That's much better, thanks. Is this stated in the readme? If not I think it would be a great idea to mention this method for the use with compose :)