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

Implement generation of Strings interface implementation with MR connection

Open Alex009 opened this issue 4 years ago • 4 comments

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.

Alex009 avatar Nov 26 '20 13:11 Alex009

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

Alex009 avatar Nov 26 '20 13:11 Alex009

annotation processing - https://github.com/google/ksp/blob/master/docs/quickstart.md

Alex009 avatar Feb 11 '21 01:02 Alex009

can be useful - https://github.com/Tetraquark/KSP-Testing

Alex009 avatar Feb 25 '21 12:02 Alex009

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

r4zzz4k avatar Nov 20 '21 14:11 r4zzz4k