I have a `StringResourceContext` interface (essent...
# compose
m
I have a
StringResourceContext
interface (essentially the subset of Android
Context
functions like
getString
) provided through DI (koin) throughout my app and am wondering whether it makes sense to also have
LocalStringResourceContext
for access in composable. Apparently, since the instance of this interface never changes, it doesn’t make sense and I can just use
koinInject()
instead. However,
koinInject()
is not a
@ReadOnlyComposable
and so cannot be used in
@ReadOnlyComposable
functions. (Code in thread)
Copy code
fun interface StringGetter {

    operator fun invoke(context: StringResourceContext): String

    @Composable
    @ReadOnlyComposable
    operator fun invoke() = invoke(LocalStringResourceContext.current)
instead would have to be:
Copy code
fun interface StringGetter {

    operator fun invoke(context: StringResourceContext): String

    @Composable
    @ReadOnlyComposable
    operator fun invoke() = invoke(LocalKoinScope.current.getKoin().get())
or just remove
@ReadOnlyComposable
and I’m not sure the performance impact of that:
Copy code
fun interface StringGetter {

    operator fun invoke(context: StringResourceContext): String

    @Composable
    operator fun invoke() = invoke(koinInject())
As a side-note, in certain parts of my code, calling the argumentless
StringGetter.invoke()
results in this runtime error (and I can’t see any pattern for why this might be happening):
Copy code
java.lang.AbstractMethodError: abstract method "java.lang.String StringGetter.invoke(androidx.compose.runtime.Composer, int)"
	at FooBarDialogKt.FooBarDialog(FooBarDialog.kt:49)
	at FooBarDialogKt.FooBarDialog(FooBarDialog.kt:23)
	at Foo.FooDialog(Foo.kt:93)
	at Foo.FooDialog$lambda$3(Unknown Source:6)
	at Foo.$r8$lambda$e3QgwvEpizXt98xiUXZPA6WHqz4(Unknown Source:0)
	at Foo$$ExternalSyntheticLambda0.invoke(D8$$SyntheticClass:0)
	at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:192)