From 212dda40ca1968261d0aeb256a575400a18fe7d0 Mon Sep 17 00:00:00 2001 From: nrobi144 Date: Mon, 16 Mar 2026 10:39:43 +0200 Subject: [PATCH] =?UTF-8?q?feat(media):=20Phase=200+1=20=E2=80=94=20Coil3?= =?UTF-8?q?=20image=20loading=20+=20inline=20images=20in=20notes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DesktopImageLoaderSetup: Coil3 with explicit memory cache (25% heap, max 512MB), persistent disk cache (OS-appropriate paths), SVG decoder - DesktopBlurHashFetcher: blurhash placeholder images via Skia Bitmap - DesktopBase64Fetcher: base64 data: URI image decoding - SkiaGifDecoder: GIF first-frame rendering via Skia Codec - MediaAspectRatioCache: thread-safe LruCache for aspect ratios - NoteCard: inline AsyncImage for image URLs, stripped from text - Added coil-compose, coil-okhttp, coil-svg, vlcj, commons-imaging, androidx-collection deps to desktopApp Co-Authored-By: Claude Opus 4.6 --- desktopApp/build.gradle.kts | 12 +++ .../vitorpamplona/amethyst/desktop/Main.kt | 5 +- .../desktop/model/MediaAspectRatioCache.kt | 36 +++++++ .../service/images/DesktopBase64Fetcher.kt | 96 +++++++++++++++++ .../service/images/DesktopBlurHashFetcher.kt | 100 ++++++++++++++++++ .../service/images/DesktopImageLoaderSetup.kt | 94 ++++++++++++++++ .../desktop/service/images/SkiaGifDecoder.kt | 62 +++++++++++ .../amethyst/desktop/ui/note/NoteCard.kt | 65 ++++++++++-- gradle/libs.versions.toml | 4 + 9 files changed, 467 insertions(+), 7 deletions(-) create mode 100644 desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/model/MediaAspectRatioCache.kt create mode 100644 desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBase64Fetcher.kt create mode 100644 desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBlurHashFetcher.kt create mode 100644 desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopImageLoaderSetup.kt create mode 100644 desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/SkiaGifDecoder.kt diff --git a/desktopApp/build.gradle.kts b/desktopApp/build.gradle.kts index 1a728e171..ed294a9bd 100644 --- a/desktopApp/build.gradle.kts +++ b/desktopApp/build.gradle.kts @@ -46,8 +46,20 @@ dependencies { // JSON implementation(libs.jackson.module.kotlin) + // Image loading (Coil3 — explicit because commons uses implementation, not api) + implementation(libs.coil.compose) + implementation(libs.coil.okhttp) + implementation(libs.coil.svg) + + // Video playback + implementation(libs.vlcj) + + // EXIF stripping (lossless) + implementation(libs.commons.imaging) + // Collections implementation(libs.kotlinx.collections.immutable) + implementation(libs.androidx.collection) // SLF4J no-op — silence "No SLF4J providers" warnings from transitive deps implementation("org.slf4j:slf4j-nop:2.0.16") diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt index 30470359f..dc7e56d09 100644 --- a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/Main.kt @@ -74,6 +74,7 @@ import com.vitorpamplona.amethyst.desktop.model.DesktopDmRelayState import com.vitorpamplona.amethyst.desktop.model.DesktopIAccount import com.vitorpamplona.amethyst.desktop.network.DefaultRelays import com.vitorpamplona.amethyst.desktop.network.DesktopRelayConnectionManager +import com.vitorpamplona.amethyst.desktop.service.images.DesktopImageLoaderSetup import com.vitorpamplona.amethyst.desktop.subscriptions.DesktopRelaySubscriptionsCoordinator import com.vitorpamplona.amethyst.desktop.ui.ComposeNoteDialog import com.vitorpamplona.amethyst.desktop.ui.ConnectingRelaysScreen @@ -137,7 +138,8 @@ sealed class DesktopScreen { data object Settings : DesktopScreen() } -fun main() = +fun main() { + DesktopImageLoaderSetup.setup() application { val windowState = rememberWindowState( @@ -384,6 +386,7 @@ fun main() = ) } } +} @Composable fun App( diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/model/MediaAspectRatioCache.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/model/MediaAspectRatioCache.kt new file mode 100644 index 000000000..ffe54e243 --- /dev/null +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/model/MediaAspectRatioCache.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.desktop.model + +import androidx.collection.LruCache + +object MediaAspectRatioCache { + private val cache = LruCache(1000) + + fun get(url: String): Float? = cache[url] + + fun put( + url: String, + ratio: Float, + ) { + cache.put(url, ratio) + } +} diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBase64Fetcher.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBase64Fetcher.kt new file mode 100644 index 000000000..248903ad3 --- /dev/null +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBase64Fetcher.kt @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.desktop.service.images + +import androidx.compose.runtime.Stable +import coil3.ImageLoader +import coil3.Uri +import coil3.asImage +import coil3.decode.DataSource +import coil3.fetch.FetchResult +import coil3.fetch.Fetcher +import coil3.fetch.ImageFetchResult +import coil3.key.Keyer +import coil3.request.Options +import com.vitorpamplona.amethyst.commons.base64Image.toPlatformImage +import com.vitorpamplona.amethyst.commons.blurhash.toBufferedImage +import com.vitorpamplona.amethyst.commons.richtext.Base64Image +import com.vitorpamplona.quartz.nip01Core.core.toHexKey +import com.vitorpamplona.quartz.utils.sha256.sha256 +import org.jetbrains.skia.Bitmap +import org.jetbrains.skia.ColorAlphaType +import org.jetbrains.skia.ImageInfo +import java.awt.image.BufferedImage + +@Stable +class DesktopBase64Fetcher( + private val options: Options, + private val data: Uri, +) : Fetcher { + override suspend fun fetch(): FetchResult? = + runCatching { + val platformImage = Base64Image.toPlatformImage(data.toString()) + val bufferedImage = platformImage.toBufferedImage() + val bitmap = bufferedImageToSkiaBitmap(bufferedImage) + ImageFetchResult( + image = bitmap.asImage(true), + isSampled = false, + dataSource = DataSource.MEMORY, + ) + }.getOrNull() + + object Factory : Fetcher.Factory { + override fun create( + data: Uri, + options: Options, + imageLoader: ImageLoader, + ): Fetcher? = + if (data.scheme == "data") { + DesktopBase64Fetcher(options, data) + } else { + null + } + } + + object BKeyer : Keyer { + override fun key( + data: Uri, + options: Options, + ): String? = + if (data.scheme == "data") { + sha256(data.toString().toByteArray()).toHexKey() + } else { + null + } + } +} + +internal fun bufferedImageToSkiaBitmap(bi: BufferedImage): Bitmap { + val w = bi.width + val h = bi.height + val pixels = IntArray(w * h) + bi.getRGB(0, 0, w, h, pixels, 0, w) + val bitmap = Bitmap() + bitmap.allocPixels(ImageInfo.makeN32(w, h, ColorAlphaType.PREMUL)) + bitmap.installPixels(convertArgbToBgra(pixels)) + bitmap.setImmutable() + return bitmap +} diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBlurHashFetcher.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBlurHashFetcher.kt new file mode 100644 index 000000000..6a3081259 --- /dev/null +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopBlurHashFetcher.kt @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.desktop.service.images + +import androidx.compose.runtime.Stable +import coil3.ImageLoader +import coil3.asImage +import coil3.decode.DataSource +import coil3.fetch.FetchResult +import coil3.fetch.Fetcher +import coil3.fetch.ImageFetchResult +import coil3.key.Keyer +import coil3.request.Options +import com.vitorpamplona.amethyst.commons.blurhash.BlurHashDecoder +import com.vitorpamplona.amethyst.commons.blurhash.toBufferedImage +import org.jetbrains.skia.Bitmap +import org.jetbrains.skia.ColorAlphaType +import org.jetbrains.skia.ImageInfo + +data class BlurhashWrapper( + val blurhash: String, +) + +@Stable +class DesktopBlurHashFetcher( + private val options: Options, + private val data: BlurhashWrapper, +) : Fetcher { + override suspend fun fetch(): FetchResult? { + val hash = data.blurhash + val platformImage = BlurHashDecoder.decodeKeepAspectRatio(hash, 25) ?: return null + val bufferedImage = platformImage.toBufferedImage() + + val w = bufferedImage.width + val h = bufferedImage.height + val pixels = IntArray(w * h) + bufferedImage.getRGB(0, 0, w, h, pixels, 0, w) + + val bitmap = Bitmap() + bitmap.allocPixels(ImageInfo.makeN32(w, h, ColorAlphaType.PREMUL)) + bitmap.installPixels(convertArgbToBgra(pixels)) + bitmap.setImmutable() + + return ImageFetchResult( + image = bitmap.asImage(true), + isSampled = false, + dataSource = DataSource.MEMORY, + ) + } + + object Factory : Fetcher.Factory { + override fun create( + data: BlurhashWrapper, + options: Options, + imageLoader: ImageLoader, + ): Fetcher = DesktopBlurHashFetcher(options, data) + } + + object BKeyer : Keyer { + override fun key( + data: BlurhashWrapper, + options: Options, + ): String = data.blurhash + } +} + +internal fun convertArgbToBgra(pixels: IntArray): ByteArray { + val bytes = ByteArray(pixels.size * 4) + for (i in pixels.indices) { + val argb = pixels[i] + val a = (argb shr 24) and 0xFF + val r = (argb shr 16) and 0xFF + val g = (argb shr 8) and 0xFF + val b = argb and 0xFF + val offset = i * 4 + bytes[offset] = b.toByte() + bytes[offset + 1] = g.toByte() + bytes[offset + 2] = r.toByte() + bytes[offset + 3] = a.toByte() + } + return bytes +} diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopImageLoaderSetup.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopImageLoaderSetup.kt new file mode 100644 index 000000000..b3a32c416 --- /dev/null +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/DesktopImageLoaderSetup.kt @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.desktop.service.images + +import coil3.ImageLoader +import coil3.PlatformContext +import coil3.SingletonImageLoader +import coil3.annotation.DelicateCoilApi +import coil3.disk.DiskCache +import coil3.memory.MemoryCache +import coil3.size.Precision +import coil3.svg.SvgDecoder +import okio.Path.Companion.toOkioPath +import java.io.File + +object DesktopImageLoaderSetup { + @OptIn(DelicateCoilApi::class) + fun setup() { + SingletonImageLoader.setUnsafe(createImageLoader()) + } + + fun createImageLoader(): ImageLoader = + ImageLoader + .Builder(PlatformContext.INSTANCE) + .memoryCache { newMemoryCache() } + .diskCache { newDiskCache() } + .precision(Precision.INEXACT) + .components { + add(SvgDecoder.Factory()) + add(SkiaGifDecoder.Factory()) + add(DesktopBase64Fetcher.Factory) + add(DesktopBlurHashFetcher.Factory) + add(DesktopBase64Fetcher.BKeyer) + add(DesktopBlurHashFetcher.BKeyer) + }.build() + + private fun newMemoryCache(): MemoryCache { + val maxMemory = Runtime.getRuntime().maxMemory() + val cacheSize = (maxMemory * 0.25).toLong().coerceAtMost(512L * 1024 * 1024) + return MemoryCache + .Builder() + .maxSizeBytes(cacheSize) + .strongReferencesEnabled(true) + .build() + } + + private fun newDiskCache(): DiskCache = + DiskCache + .Builder() + .directory(cacheDir().resolve("AmethystDesktop/image_cache").toOkioPath()) + .maxSizeBytes(512L * 1024 * 1024) + .build() + + private fun cacheDir(): File { + val os = System.getProperty("os.name").lowercase() + return when { + "mac" in os -> { + File(System.getProperty("user.home"), "Library/Caches") + } + + "win" in os -> { + File( + System.getenv("LOCALAPPDATA") + ?: System.getProperty("user.home"), + ) + } + + else -> { + File( + System.getenv("XDG_CACHE_HOME") + ?: "${System.getProperty("user.home")}/.cache", + ) + } + } + } +} diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/SkiaGifDecoder.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/SkiaGifDecoder.kt new file mode 100644 index 000000000..185b0731a --- /dev/null +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/service/images/SkiaGifDecoder.kt @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.desktop.service.images + +import coil3.ImageLoader +import coil3.asImage +import coil3.decode.DecodeResult +import coil3.decode.Decoder +import coil3.decode.ImageSource +import coil3.fetch.SourceFetchResult +import coil3.request.Options +import org.jetbrains.skia.Bitmap +import org.jetbrains.skia.Codec +import org.jetbrains.skia.Data + +class SkiaGifDecoder( + private val source: ImageSource, +) : Decoder { + override suspend fun decode(): DecodeResult { + val bytes = source.source().use { it.readByteArray() } + val data = Data.makeFromBytes(bytes) + val codec = Codec.makeFromData(data) + val bitmap = Bitmap() + bitmap.allocN32Pixels(codec.width, codec.height) + codec.readPixels(bitmap, 0) + bitmap.setImmutable() + return DecodeResult( + image = bitmap.asImage(), + isSampled = false, + ) + } + + class Factory : Decoder.Factory { + override fun create( + result: SourceFetchResult, + options: Options, + imageLoader: ImageLoader, + ): Decoder? { + val mimeType = result.mimeType ?: return null + if (mimeType != "image/gif") return null + return SkiaGifDecoder(result.source) + } + } +} diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/ui/note/NoteCard.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/ui/note/NoteCard.kt index aca0f12d9..c096bcbc6 100644 --- a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/ui/note/NoteCard.kt +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/ui/note/NoteCard.kt @@ -27,8 +27,10 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Card import androidx.compose.material3.CardDefaults import androidx.compose.material3.HorizontalDivider @@ -38,12 +40,16 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.buildAnnotatedString import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.withStyle import androidx.compose.ui.unit.dp +import coil3.compose.AsyncImage +import com.vitorpamplona.amethyst.commons.richtext.RichTextParser import com.vitorpamplona.amethyst.commons.richtext.UrlParser import com.vitorpamplona.amethyst.commons.richtext.Urls import com.vitorpamplona.amethyst.commons.ui.components.UserAvatar @@ -73,6 +79,30 @@ fun NoteCard( onAuthorClick: ((String) -> Unit)? = null, ) { val urls = remember(note.content) { UrlParser().parseValidUrls(note.content) } + val imageUrls = + remember(urls) { + urls.withScheme.filter { RichTextParser.isImageUrl(it) } + } + val strippedContent = + remember(note.content, imageUrls) { + var text = note.content + for (url in imageUrls) { + text = text.replace(url, "").trim() + } + text + } + val strippedUrls = + remember(urls, imageUrls) { + val imageSet = imageUrls.toSet() + Urls( + withScheme = urls.withScheme - imageSet, + withoutScheme = urls.withoutScheme, + emails = urls.emails, + bech32s = urls.bech32s, + relayUrls = urls.relayUrls, + blossomUris = urls.blossomUris, + ) + } Card( modifier = modifier.fillMaxWidth(), @@ -125,11 +155,35 @@ fun NoteCard( Spacer(Modifier.height(8.dp)) - RichTextContent( - content = note.content, - urls = urls, - modifier = Modifier.fillMaxWidth(), - ) + if (strippedContent.isNotBlank()) { + RichTextContent( + content = strippedContent, + urls = strippedUrls, + modifier = Modifier.fillMaxWidth(), + ) + } + + // Inline images + if (imageUrls.isNotEmpty()) { + if (strippedContent.isNotBlank()) { + Spacer(Modifier.height(8.dp)) + } + for (url in imageUrls) { + AsyncImage( + model = url, + contentDescription = null, + modifier = + Modifier + .fillMaxWidth() + .heightIn(max = 400.dp) + .clip(RoundedCornerShape(8.dp)), + contentScale = ContentScale.FillWidth, + ) + if (url != imageUrls.last()) { + Spacer(Modifier.height(4.dp)) + } + } + } Spacer(Modifier.height(8.dp)) @@ -171,7 +225,6 @@ fun RichTextContent( val annotatedText = buildAnnotatedString { var lastIndex = 0 - // TODO: User the other urls. val sortedUrls = urls.withScheme.sortedBy { content.indexOf(it) } for (url in sortedUrls) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d6a717466..9e11c3afd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -60,6 +60,8 @@ unifiedpush = "3.0.10" vico-charts-compose = "3.0.3" zelory = "3.0.1" zoomable = "2.11.1" +vlcj = "4.8.3" +commonsImaging = "1.0.0-alpha5" zxing = "3.5.4" zxingAndroidEmbedded = "4.3.0" windowCoreAndroid = "1.5.1" @@ -121,6 +123,8 @@ coil-gif = { group = "io.coil-kt.coil3", name = "coil-gif", version.ref = "coil" coil-svg = { group = "io.coil-kt.coil3", name = "coil-svg", version.ref = "coil" } coil-okhttp = { group = "io.coil-kt.coil3", name = "coil-network-okhttp", version.ref = "coil" } coil-video = { group = "io.coil-kt.coil3", name = "coil-video", version.ref = "coil" } +commons-imaging = { group = "org.apache.commons", name = "commons-imaging", version.ref = "commonsImaging" } +vlcj = { group = "uk.co.caprica", name = "vlcj", version.ref = "vlcj" } dev-whyoleg-cryptography-provider-apple-optimal = { module = "dev.whyoleg.cryptography:cryptography-provider-optimal", version.ref = "devWhyolegCryptography" } drfonfon-geohash = { group = "com.github.drfonfon", name = "android-kotlin-geohash", version.ref = "androidKotlinGeohash" } firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.ref = "firebaseBom" }