Skip to content

Commit

Permalink
Use SubcomposeAsyncImage for loading and error placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcoding810 committed Dec 30, 2024
1 parent 3250ec7 commit 947cab0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/paulcoding/hviewer/model/SiteModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.paulcoding.hviewer.R

data class SiteConfig(
val baseUrl: String = "",
Expand All @@ -26,6 +28,7 @@ data class SiteConfig(
.size(size)
.clip(RoundedCornerShape(clip)),
contentScale = ContentScale.Crop,
placeholder = painterResource(R.drawable.ic_launcher_foreground)
)
}
}
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/com/paulcoding/hviewer/ui/component/HImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import coil3.compose.AsyncImage
import coil3.compose.SubcomposeAsyncImage
import coil3.request.ImageRequest
import coil3.request.crossfade
import com.paulcoding.hviewer.R

@Composable
fun HImage(url: String, modifier: Modifier = Modifier) {
AsyncImage(
SubcomposeAsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(url)
.crossfade(200)
.build(),
contentDescription = url,
placeholder = painterResource(R.drawable.ic_launcher_foreground),
error = painterResource(R.drawable.ic_launcher_foreground),
loading = { HLoading() },
error = { HEmpty() },
modifier = modifier.fillMaxWidth(),
contentScale = ContentScale.FillWidth,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -20,6 +21,7 @@ fun HLoading() {
.height(64.dp)
.padding(16.dp)
.align(Alignment.Center),
trackColor = MaterialTheme.colorScheme.primary,
)
}
}

0 comments on commit 947cab0

Please sign in to comment.