From 405909fe675b033a2e5a6cb91c47d85e558c1a58 Mon Sep 17 00:00:00 2001 From: Sasikanth Miriyampalli Date: Sat, 16 Sep 2023 21:10:52 +0530 Subject: [PATCH] Use proper keys for image state in `IOSImageLoader#rememberImageLoaderState` This was causing issues were a different image was loading for few seconds before correct image is loaded --- .../rss/reader/components/IOSImageLoader.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/components/IOSImageLoader.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/components/IOSImageLoader.kt index 57fa58c92..88f0ceb93 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/components/IOSImageLoader.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/components/IOSImageLoader.kt @@ -18,8 +18,10 @@ package dev.sasikanth.rss.reader.components import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.State -import androidx.compose.runtime.produceState +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.asComposeImageBitmap import dev.sasikanth.rss.reader.di.scopes.AppScope @@ -62,15 +64,20 @@ internal fun rememberImageLoaderState(url: String?): State { ImageLoaderState.Loading } val imageLoader = LocalImageLoader.current + val result = remember(url, imageLoader) { mutableStateOf(initialState) } - return produceState(initialState, url) { - value = + LaunchedEffect(url) { + val imageLoaderState = try { ImageLoaderState.Loaded(imageLoader?.getImage(url!!, size = null)!!) } catch (e: Exception) { ImageLoaderState.Error } + + result.value = imageLoaderState } + + return result } internal sealed interface ImageLoaderState {