From 855523d4e6602903917bb765663161c211f9c62f Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Sat, 11 May 2024 01:02:45 +0400 Subject: [PATCH] Migrate network images to coil3 --- catalog/build.gradle.kts | 1 - design/build.gradle.kts | 9 +++- .../sdui/design/component/AsyncImage.kt | 25 ---------- .../sdui/design/component/AsyncImage.kt | 13 ++++- .../sdui/design/component/AsyncImage.kt | 28 ----------- .../sdui/design/component/AsyncImage.js.kt | 47 ------------------- gradle/libs.versions.toml | 10 +++- sdui/build.gradle.kts | 6 --- 8 files changed, 28 insertions(+), 111 deletions(-) delete mode 100644 design/src/androidMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt delete mode 100644 design/src/desktopMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt delete mode 100644 design/src/jsMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.js.kt diff --git a/catalog/build.gradle.kts b/catalog/build.gradle.kts index c85a6eb..13edc69 100644 --- a/catalog/build.gradle.kts +++ b/catalog/build.gradle.kts @@ -54,7 +54,6 @@ kotlin { val androidMain by getting { dependencies { - implementation(libs.coil.compose) implementation(libs.androidx.compose.ui) implementation(libs.androidx.compose.ui.tooling) implementation(libs.androidx.compose.ui.tooling.preview) diff --git a/design/build.gradle.kts b/design/build.gradle.kts index 0a2c4f0..8377a0b 100644 --- a/design/build.gradle.kts +++ b/design/build.gradle.kts @@ -37,24 +37,31 @@ kotlin { implementation(compose.runtime) implementation(compose.foundation) implementation(compose.material3) + implementation(libs.coil) + implementation(libs.coil.compose) } } val androidMain by getting { dependencies { - implementation(libs.coil.compose) + implementation(libs.coil.network.okhttp) implementation(compose.uiTooling) } } val desktopMain by getting { resources.srcDir("src/commonRes") + dependencies { + implementation(libs.kotlinx.coroutines.swing) + implementation(libs.coil.network.okhttp) + } } val jsMain by getting { dependencies { @OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class) implementation(compose.components.resources) + implementation(libs.coil.network.ktor) implementation(libs.ktor.client.core) implementation(libs.ktor.client.js) } diff --git a/design/src/androidMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt b/design/src/androidMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt deleted file mode 100644 index c661b59..0000000 --- a/design/src/androidMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) 2023 Ahmed El-Helw and Abdulahi Osoble - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -package dev.helw.playground.sdui.design.component - -import androidx.compose.foundation.layout.size -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext -import coil.request.ImageRequest -import dev.helw.playground.sdui.design.core.SizeToken - -@Composable -actual fun AsyncImage(url: String, contentDescription: String, size: SizeToken, modifier: Modifier) { - coil.compose.AsyncImage( - model = ImageRequest.Builder(LocalContext.current) - .data(url) - .build(), - contentDescription = contentDescription, - modifier = modifier.size(size.underlyingSize) - ) -} diff --git a/design/src/commonMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt b/design/src/commonMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt index a9bd40d..f556c00 100644 --- a/design/src/commonMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt +++ b/design/src/commonMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt @@ -6,9 +6,20 @@ */ package dev.helw.playground.sdui.design.component +import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import coil3.compose.LocalPlatformContext +import coil3.request.ImageRequest import dev.helw.playground.sdui.design.core.SizeToken @Composable -expect fun AsyncImage(url: String, contentDescription: String, size: SizeToken, modifier: Modifier) +fun AsyncImage(url: String, contentDescription: String, size: SizeToken, modifier: Modifier) { + coil3.compose.AsyncImage( + model = ImageRequest.Builder(LocalPlatformContext.current) + .data(url) + .build(), + contentDescription = contentDescription, + modifier = modifier.size(size.underlyingSize) + ) +} \ No newline at end of file diff --git a/design/src/desktopMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt b/design/src/desktopMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt deleted file mode 100644 index 94d1622..0000000 --- a/design/src/desktopMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.kt +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2023 Ahmed El-Helw and Abdulahi Osoble - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -package dev.helw.playground.sdui.design.component - -import androidx.compose.foundation.Image -import androidx.compose.foundation.layout.size -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.painter.Painter -import androidx.compose.ui.graphics.toPainter -import dev.helw.playground.sdui.design.core.SizeToken -import java.net.URL -import javax.imageio.ImageIO - -@Composable -actual fun AsyncImage(url: String, contentDescription: String, size: SizeToken, modifier: Modifier) { - Image( - painter = loadImage(url), - contentDescription = contentDescription, - modifier = modifier.size(size.underlyingSize) - ) -} - -private fun loadImage(url: String): Painter = ImageIO.read(URL(url)).toPainter() diff --git a/design/src/jsMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.js.kt b/design/src/jsMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.js.kt deleted file mode 100644 index d678e4f..0000000 --- a/design/src/jsMain/kotlin/dev/helw/playground/sdui/design/component/AsyncImage.js.kt +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright (c) 2023 Ahmed El-Helw and Abdulahi Osoble - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ -package dev.helw.playground.sdui.design.component - -import androidx.compose.foundation.Image -import androidx.compose.foundation.layout.size -import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.produceState -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.graphics.toComposeImageBitmap -import dev.helw.playground.sdui.design.core.SizeToken -import io.ktor.client.HttpClient -import io.ktor.client.engine.js.JsClient -import io.ktor.client.request.get -import io.ktor.client.statement.readBytes -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.jetbrains.skia.Image - -private val httpClient = HttpClient(JsClient()) - -@Composable -actual fun AsyncImage(url: String, contentDescription: String, size: SizeToken, modifier: Modifier) { - val image: ImageBitmap? by produceState(null) { - value = withContext(Dispatchers.Default) { - val bytes = httpClient.get(url).readBytes() - imageBitmapFromBytes(bytes) - } - } - - if (image != null) { - Image(image!!, - contentDescription, - modifier = modifier.size(size.underlyingSize) - ) - } -} - -fun imageBitmapFromBytes(encodedImageData: ByteArray): ImageBitmap { - return Image.makeFromEncoded(encodedImageData).toComposeImageBitmap() -} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6087aee..58cb6a7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,8 @@ activity-compose = "1.9.0" agp = "8.4.0" androidx-material = "1.6.7" -coil-compose = "2.4.0" +coil = "3.0.0-alpha06" +coroutines = "1.8.1" foundation = "1.6.7" kotlin = "1.9.23" kotlinx-serialization = "1.6.3" @@ -25,6 +26,8 @@ androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling androidx-navigation-compose = "androidx.navigation:navigation-compose:2.7.7" +kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" } + ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" } ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" } @@ -32,7 +35,10 @@ ktor-client-contentNegotiation = { module = "io.ktor:ktor-client-content-negotia ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" } ktor-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" } -coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil-compose" } +coil = { module = "io.coil-kt.coil3:coil", version.ref = "coil" } +coil-compose = { module = "io.coil-kt.coil3:coil-compose", version.ref = "coil" } +coil-network-okhttp = { module = "io.coil-kt.coil3:coil-network-okhttp", version.ref = "coil" } +coil-network-ktor = { module = "io.coil-kt.coil3:coil-network-ktor", version.ref = "coil" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } diff --git a/sdui/build.gradle.kts b/sdui/build.gradle.kts index 61d74fd..000838e 100644 --- a/sdui/build.gradle.kts +++ b/sdui/build.gradle.kts @@ -41,12 +41,6 @@ kotlin { implementation(libs.kotlinx.serialization.json) } } - - val androidMain by getting { - dependencies { - implementation(libs.coil.compose) - } - } } }