Skip to content

Commit

Permalink
image create with one http instance
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Dec 1, 2024
1 parent c31e561 commit e9d3d2b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ImageLoader> {
error("No ImageLoader provided")
}

@Composable
internal fun createImageLoader(): ImageLoader = ImageLoader
.Builder(LocalPlatformContext.current)
.components {
add(KtorNetworkFetcherFactory(getKoin().get<HttpClient>()))
}
.build()

val imageProvidedValue: ProvidedValue<*>
@Composable
get() = ImageLoaderProvider provides createImageLoader()
2 changes: 1 addition & 1 deletion core/ui/kit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -18,7 +20,11 @@ fun AppTheme(
}
MaterialTheme(
colorScheme = colors,
content = content
content = {
CompositionLocalProvider(imageProvidedValue) {
content()
}
}
)
// todo kamel migration test
// CompositionLocalProvider(
Expand Down

0 comments on commit e9d3d2b

Please sign in to comment.