diff --git a/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/AppImage.kt b/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/AppImage.kt index 6f4a4f5..4d0a1c6 100644 --- a/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/AppImage.kt +++ b/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/AppImage.kt @@ -3,29 +3,9 @@ package com.stslex.wizard.core.ui.image import androidx.compose.foundation.Image import androidx.compose.foundation.layout.BoxScope import androidx.compose.runtime.Composable -import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale -import coil3.ImageLoader -import coil3.compose.LocalPlatformContext import coil3.compose.rememberAsyncImagePainter -import coil3.network.ktor3.KtorNetworkFetcherFactory -import io.ktor.client.HttpClient -import io.ktor.client.engine.cio.CIO - -// todo refactor -private var imageLoader: ImageLoader? = null - -@Composable -internal fun rememberImageLoader(): ImageLoader { - val platformContext = LocalPlatformContext.current - return remember { - imageLoader ?: ImageLoader - .Builder(platformContext) - .components { add(KtorNetworkFetcherFactory(HttpClient(CIO))) } - .build() - } -} @Composable fun AppImage( @@ -36,10 +16,9 @@ fun AppImage( onLoading: @Composable (BoxScope.(Float) -> Unit)? = null, onFailure: @Composable (BoxScope.(Throwable) -> Unit)? = null, ) { - val imageLoader = rememberImageLoader() val painter = rememberAsyncImagePainter( model = url, - imageLoader = imageLoader, + imageLoader = ImageLoaderProvider.current, ) // todo add states Image( diff --git a/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/ImageLoaderTheme.kt b/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/ImageLoaderTheme.kt new file mode 100644 index 0000000..39f11d3 --- /dev/null +++ b/core/ui/image/src/commonMain/kotlin/com/stslex/wizard/core/ui/image/ImageLoaderTheme.kt @@ -0,0 +1,26 @@ +package com.stslex.wizard.core.ui.image + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.ProvidedValue +import androidx.compose.runtime.staticCompositionLocalOf +import coil3.ImageLoader +import coil3.compose.LocalPlatformContext +import coil3.network.ktor3.KtorNetworkFetcherFactory +import io.ktor.client.HttpClient +import org.koin.compose.getKoin + +internal val ImageLoaderProvider = staticCompositionLocalOf { + error("No ImageLoader provided") +} + +@Composable +internal fun createImageLoader(): ImageLoader = ImageLoader + .Builder(LocalPlatformContext.current) + .components { + add(KtorNetworkFetcherFactory(getKoin().get())) + } + .build() + +val imageProvidedValue: ProvidedValue<*> + @Composable + get() = ImageLoaderProvider provides createImageLoader() diff --git a/core/ui/kit/build.gradle.kts b/core/ui/kit/build.gradle.kts index 0bd5213..6fdc2fa 100644 --- a/core/ui/kit/build.gradle.kts +++ b/core/ui/kit/build.gradle.kts @@ -6,7 +6,7 @@ kotlin { sourceSets { commonMain.dependencies { implementation(project(":core:core")) - implementation(project(":core:network:api")) + implementation(project(":core:ui:image")) implementation(libs.kotlinx.datetime) } } diff --git a/core/ui/kit/src/commonMain/kotlin/com/stslex/wizard/core/ui/kit/theme/AppTheme.kt b/core/ui/kit/src/commonMain/kotlin/com/stslex/wizard/core/ui/kit/theme/AppTheme.kt index 41fcbc8..95333fb 100644 --- a/core/ui/kit/src/commonMain/kotlin/com/stslex/wizard/core/ui/kit/theme/AppTheme.kt +++ b/core/ui/kit/src/commonMain/kotlin/com/stslex/wizard/core/ui/kit/theme/AppTheme.kt @@ -5,6 +5,8 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import com.stslex.wizard.core.ui.image.imageProvidedValue @Composable fun AppTheme( @@ -18,7 +20,11 @@ fun AppTheme( } MaterialTheme( colorScheme = colors, - content = content + content = { + CompositionLocalProvider(imageProvidedValue) { + content() + } + } ) // todo kamel migration test // CompositionLocalProvider(