Skip to content

Commit 3ac7470

Browse files
committed
Serve AsyncImage from the cache before fetching
1 parent d49f4a2 commit 3ac7470

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

  • Demo/swiftui/src/commonMain/kotlin/com/pureswift/swiftui

Demo/swiftui/src/commonMain/kotlin/com/pureswift/swiftui/Render.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,11 +1314,19 @@ private fun RenderAsyncImage(node: ViewNode) {
13141314
Text("[image]", modifier = node.composeModifiers())
13151315
return
13161316
}
1317-
var bitmap by remember(url) { mutableStateOf<ImageBitmap?>(null) }
1317+
// Seed from the cache synchronously: a URL already fetched renders on the
1318+
// first frame with no spinner. Only a miss touches the network.
1319+
var bitmap by remember(url) { mutableStateOf(ImageCache.get(url)) }
13181320
var failed by remember(url) { mutableStateOf(false) }
13191321
LaunchedEffect(url) {
1322+
if (bitmap != null) return@LaunchedEffect
13201323
val loaded = loadRemoteImage(url)
1321-
if (loaded != null) bitmap = loaded else failed = true
1324+
if (loaded != null) {
1325+
ImageCache.put(url, loaded)
1326+
bitmap = loaded
1327+
} else {
1328+
failed = true
1329+
}
13221330
}
13231331
val image = bitmap
13241332
when {

0 commit comments

Comments
 (0)