moko-resources
moko-resources copied to clipboard
Implement generation of Strings interface implementation with MR connection
In multi-module projects with injection of resources from umbrella module (like https://github.com/icerockdev/moko-template)
we have this separation:
feature module
:
class MyViewModel(
private val strings: Strings,
) : ViewModel() {
// ...
interface Strings {
val unknownError: StringResource
}
}
umbrella module (mpp-library
- contains resources
and generate MR
class:
val viewModel = MyViewModel(
strings = object: ListViewModel.Strings {
override val unknownError: StringResource
get() = MR.strings.unknown_error
}
)
and when we should pass many resources to feature module we do routine work with creating proxy anonymous object.
Current idea
Add annotations to moko-resources runtime library
annotation class MRmapping
annotation class MRname(val name: String)
(!) names should be improved
interface in feature module we mark with annotations
@MRmapping
interface Strings {
@MRname("unknown_error")
val unknownError: StringResource
}
in umbrella module we connect plugin generator
connect plugin to module, which have access to MR. plugin will generate for all annotated interfaces (from all modules, not only umbrella module) function:
private fun MR.mapping(): ListViewModel.Strings {
return object: ListViewModel.Strings {
override val unknownError: StringResource
get() = MR.strings.unknown_error
}
}
and we can use already generated proxy:
val viewModel = MyViewModel(strings = MR.mapping())
interfaces should work with all resource types
annotation processing - https://github.com/google/ksp/blob/master/docs/quickstart.md
can be useful - https://github.com/Tetraquark/KSP-Testing
By the way, you should consider generating the implementation which can switch resources in runtime during configuration changes, for example, as described at https://github.com/icerockdev/moko-resources/issues/201#issuecomment-922340943. cc @Nailik