Hi everyone :wave: I'm trying to inject a dependen...
# koin
n
Hi everyone ๐Ÿ‘‹ I'm trying to inject a dependency into my composable this way:
Copy code
CompositionLocalProvider(LocalSdkExport provides koinInject())
My SdkExport is injected dynamically at start using:
Copy code
singleSdkExport { sdkExport }
I have a missing definition error when
compileSafety
is enabled, and it seems I can't add a
@Provided
in a composable variable. What is the best way to handle this case ? Thanks
In this particular case, I do have a way to send the
sdkExport
to my Composable method (which is the MainViewController used from the SwiftUI application in a CMP project) but I hoped that I could reuse my previously injected value
a
Afaik, Its a bad practice to use CompositionLocalProvider for injecting dependencies.
I think koinInject is a composable already and you can use it directly in your composable.
Copy code
// Top-level / screen composable โ€” Koin boundary
@Composable
fun MyScreen(
    sdkExport: SdkExport = koinInject()
) {
    MyContent(sdkExport)
}
n
Sorry, I should have explain, it's just the compile safety which produces the error. At runtime, this is working if I disable the compile safety. My SdkExport provides me a UIController I can embed into my Compose view (which is why I need to inject it using
CompositionLocalProvider
). It also has some methods that are called in some ViewModels which explains why I need to inject it using Koin too
a
Still don't understand why would you need do to this through the CompositionLocalProvider. But if your sdk is defined in a diferent module than root, try to disable the compileSafety on that module, leaving the root module compile safe, that's an acceptable tradeoff i think.
n
It's a CMP application. My koin injection is only available in the
:shared
module. I need to use an external SDK which is a Video Feed View which does not have a CMP library. I created an
:external-video-player
module which has the following signature in commonMain :
Copy code
@Composable
public expect fun VideoFeed(
    modifier: Modifier = Modifier,
    id: String,
)
On iosMain, I have:
Copy code
public val LocalVideoFeedExport: ProvidableCompositionLocal<VideoFeedExport> = staticCompositionLocalOf {
    error("VideoFeedExport not initialized")
}

public interface VideoFeedExport {
    public fun createVideoFeedView(): UIView
    // and other methods for the SDK management called in the ViewModel
}

@Composable
public actual fun VideoFeed(
    modifier: Modifier,
    id: String,
) {
    val export = LocalVideoFeedExport.current

    UIKitView(
        modifier = modifier.fillMaxSize(),
        factory = {
            export.createVideoFeedView()
        },
    )
}
That allows me to simply implement this interface in my iosApp target by adding the VideoFeedSDK
a
Maybe try something like this (wrote it from the hip ๐Ÿ™‚, did not compile but rather a general idea).
Copy code
commmon/
@Composable
public expect fun VideoFeed(
    modifier: Modifier = Modifier,
    id: String,
)

iosMain/
public interface VideoFeedExport {
    public fun createVideoFeedView(): UIView
    // and other methods for the SDK management called in the ViewModel
}

@Composable
public actual fun VideoFeed(
    modifier: Modifier,
    id: String,
) {
    val export = koinInject<VideoFeedExport>()

    UIKitView(
        modifier = modifier.fillMaxSize(),
        factory = {
            export.createVideoFeedView()
        },
    )
}

somewhere in iosMain/

@Factory or @Single
fun provideVideoFeed(videoSdk:VideoSdk) = VideoFeedExportImpl(videoSdk)

also in common of the ^^(same) module need to have a 
@Module
@ComponentScan("your.package")
@Configuration
VideoFeedWhatever
n
The thing is the
VideoFeed
stuff is not in my `shared`module, it's in a module without Koin (it's not even local to my project because we share this video feed library across multiple projects)
So I can't use the
koinInject
util method in the iosMain
VideoFeed
method
a
๐Ÿ™‚ that's tricky
n
I was looking for something like
koinInjectProvided()
which allows us to specify that the injected value should not be considered by the compile safety flag
a
I guess I would try to make a common interface VideoFeedExport with createVideoFeedView(): Any which will be passed to the common composable and then cast it inside the actual implementation to the UIView. It's a bit hackish though
or instead of Any have an expect NativeView
and in ios actual actual NativeView: UIView
less hackish
n
Yeah I see, but in my opinion it should be doable the same way we can add
@Provided
in a class, function or value parameter.
a
true
the problem could lie in the mix of apis dsl + and annotations
like this is a dsl api single<SdkExport> { sdkExport }
though not sure
could be that the annotation do not see this declaration ^^
n
Yes that's how the Koin compile safety must work. If a dependency is injected using DSL, it cannot fully ensure the compiler that a dependency was successfully provided
Thanks for the help and the discussion anyway ๐Ÿ™‚
๐Ÿ‘ 1
as a side note, when the SPM import will be deployed with pure-swift library support (a man can dream), I will not need this injection anyway ๐Ÿ˜„
a
looks pretty, not so far away though kotlin = "2.4.0-Beta1"
n
It works but it only handles swift libraries with objective c export, which is sadly not the case for most of them ๐Ÿ˜ž
a
you can do it now as well, but proabbaly would need some intermediary module wrapper which has koin as well, and adds that dep throgh annotation, not so pretty