fixup! ref: add MXCImage

This commit is contained in:
2025-06-05 21:51:04 +03:00
parent de205b739f
commit c40b13a7ea
@@ -5,7 +5,6 @@
package ru.risdeveau.pixeldragon.ui.item
import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
@@ -28,20 +27,18 @@ import ru.risdeveau.pixeldragon.api.mxcToUrl
import ru.risdeveau.pixeldragon.token
import splitties.init.appCtx
@SuppressLint("StateFlowValueCalledInComposition")
enum class ImageLoadState {
Loading, Success, Error
}
@Composable
fun MXCImage(
mxcUrl: String,
modifier: Modifier = Modifier,
contentDescription: String = ""
) {
data class ImageLoadState(
val isLoading: Boolean = false,
val isSuccess: Boolean = false,
val error: Throwable? = null
)
val loadState = remember { mutableStateOf(ImageLoadState()) }
val loadState = remember { mutableStateOf(ImageLoadState.Loading) }
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(appCtx)
.data(mxcToUrl(mxcUrl))
@@ -54,10 +51,9 @@ fun MXCImage(
.build(),
onState = { state ->
loadState.value = when (state) {
is AsyncImagePainter.State.Loading -> ImageLoadState(isLoading = true)
is AsyncImagePainter.State.Success -> ImageLoadState(isSuccess = true)
is AsyncImagePainter.State.Error -> ImageLoadState(error = state.result.throwable)
else -> ImageLoadState()
is AsyncImagePainter.State.Loading -> ImageLoadState.Loading
is AsyncImagePainter.State.Success -> ImageLoadState.Success
else -> ImageLoadState.Error
}
}
)
@@ -73,10 +69,10 @@ fun MXCImage(
modifier = Modifier.fillMaxSize()
)
if (loadState.value.isLoading) {
CircularProgressIndicator()
} else if (loadState.value.error != null) {
Icon(Icons.Outlined.Warning, "Error")
when (loadState.value) {
ImageLoadState.Loading -> CircularProgressIndicator()
ImageLoadState.Error -> Icon(Icons.Outlined.Warning, "Error")
ImageLoadState.Success -> Unit
}
}
}