jvmMain.com.mikepenz.markdown.coil2.ImagePainterProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multiplatform-markdown-renderer-coil2-jvm Show documentation
Show all versions of multiplatform-markdown-renderer-coil2-jvm Show documentation
Kotlin Multiplatform Markdown Renderer. (Android, Desktop, ...) powered by Compose Multiplatform
package com.mikepenz.markdown.coil2
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toComposeImageBitmap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.skia.Image
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
@Composable
internal actual fun imagePainter(url: String): Painter? {
return fetchImage(url)?.let { BitmapPainter(it) }
}
@Composable
internal actual fun painterIntrinsicSize(painter: Painter): Size {
return painter.intrinsicSize
}
@Composable
fun fetchImage(url: String): ImageBitmap? {
var image by remember(url) { mutableStateOf(null) }
LaunchedEffect(url) {
image = loadPicture(url)
}
return image
}
suspend fun loadPicture(url: String): ImageBitmap? = withContext(Dispatchers.IO) {
return@withContext runCatching {
val connection: HttpURLConnection = URL(url).openConnection() as HttpURLConnection
connection.connectTimeout = 5000
connection.connect()
val input: InputStream = connection.inputStream
Image.makeFromEncoded(input.readBytes()).toComposeImageBitmap()
}.getOrNull()
}