Yokan / rebuild
Course 1 8624561

16-coil-image.md

.codex/skills/compose-kotlin-agent-skills/references/16-coil-image.md

documentation / design asset203 lines
1---2name: coil-image3description: >4  Coil 3 image loading in Compose — AsyncImage, SubcomposeAsyncImage, cache config,5  network dependency, AsyncImagePainter StateFlow, LocalAsyncImageModelEqualityDelegate.6  Use when loading images in Jetpack Compose.7version: "2.2.0"8updated: "2026-05-21"9---1011# Coil 3 — Compose Image Loading (2026)1213## Pinned versions (2026) — both required1415```kotlin16implementation("io.coil-kt.coil3:coil-compose:3.4.0")17implementation("io.coil-kt.coil3:coil-network-okhttp:3.4.0")  // REQUIRED separate artifact18```1920`coil-network-okhttp` is **not** transitive — network images fail silently without it.2122---2324## WRONG / RIGHT 1 — Missing network dependency2526```kotlin27// WRONG — only coil-compose; URLs never load28dependencies {29    implementation("io.coil-kt.coil3:coil-compose:3.4.0")30}3132// RIGHT — explicit network engine33dependencies {34    implementation("io.coil-kt.coil3:coil-compose:3.4.0")35    implementation("io.coil-kt.coil3:coil-network-okhttp:3.4.0")36}37```3839---4041## WRONG / RIGHT 2 — AsyncImage without placeholder/error4243```kotlin44// WRONG — layout jump + no error state45@Composable46fun Avatar(url: String) {47    AsyncImage(48        model = url,49        contentDescription = null,50        modifier = Modifier.size(48.dp)51    )52}5354// RIGHT — SubcomposeAsyncImage for loading/error slots55@Composable56fun Avatar(url: String, modifier: Modifier = Modifier) {57    SubcomposeAsyncImage(58        model = url,59        contentDescription = stringResource(R.string.avatar_content_description),60        modifier = modifier61            .size(48.dp)62            .clip(CircleShape),63        loading = {64            CircularProgressIndicator(65                modifier = Modifier.size(24.dp),66                strokeWidth = 2.dp67            )68        },69        error = {70            Icon(71                imageVector = Icons.Default.Person,72                contentDescription = null,73                modifier = Modifier.size(32.dp)74            )75        }76    )77}78```7980---8182## WRONG / RIGHT 3 — Reading AsyncImagePainter.state via .value (2026 breaking change)8384```kotlin85// WRONG — .value is not observable; UI won't recompose on state change86@Composable87fun ImageWithOverlay(model: Any) {88    val painter = rememberAsyncImagePainter(model = model)89    val state = painter.state.value  // BANNED in Coil 3.4+90    if (state is AsyncImagePainter.State.Success) {91        Image(painter = painter, contentDescription = null)92    }93}9495// RIGHT — observe StateFlow96@Composable97fun ImageWithOverlay(model: Any) {98    val painter = rememberAsyncImagePainter(model = model)99    val state by painter.state.collectAsState()100    when (state) {101        is AsyncImagePainter.State.Loading -> LoadingPlaceholder()102        is AsyncImagePainter.State.Success -> Image(103            painter = painter,104            contentDescription = stringResource(R.string.image_loaded)105        )106        is AsyncImagePainter.State.Error -> ErrorPlaceholder()107        AsyncImagePainter.State.Empty -> Unit108    }109}110```111112---113114## WRONG / RIGHT 4 — EqualityDelegate as AsyncImage parameter (removed in 3.x)115116```kotlin117// WRONG — EqualityDelegate parameter removed from AsyncImage118AsyncImage(119    model = url,120    contentDescription = null,121    equalityDelegate = DefaultModelEqualityDelegate()  // compile error122)123124// RIGHT — provide via CompositionLocal125@Composable126fun AppImageHost(content: @Composable () -> Unit) {127    CompositionLocalProvider(128        LocalAsyncImageModelEqualityDelegate provides DefaultModelEqualityDelegate()129    ) {130        content()131    }132}133134@Composable135fun ProductImage(url: String) {136    AsyncImage(137        model = url,138        contentDescription = stringResource(R.string.product_image)139    )140}141```142143---144145## WRONG / RIGHT 5 — Default ImageLoader per screen (cache thrash)146147```kotlin148// WRONG — new loader every composition149@Composable150fun Banner(url: String) {151    val loader = ImageLoader.Builder(LocalContext.current).build()152    AsyncImage(model = url, imageLoader = loader, contentDescription = null)153}154155// RIGHT — singleton via Coil.setImageLoader or Hilt @Singleton156@Module157@InstallIn(SingletonComponent::class)158object CoilModule {159    @Provides160    @Singleton161    fun imageLoader(@ApplicationContext context: Context): ImageLoader =162        ImageLoader.Builder(context)163            .memoryCache {164                MemoryCache.Builder()165                    .maxSizePercent(context, 0.25)166                    .build()167            }168            .diskCache {169                DiskCache.Builder()170                    .directory(context.cacheDir.resolve("image_cache"))171                    .maxSizeBytes(50L * 1024 * 1024)172                    .build()173            }174            .build()175}176177@Composable178fun Banner(url: String, loader: ImageLoader = LocalContext.current.imageLoader) {179    AsyncImage(180        model = url,181        imageLoader = loader,182        contentDescription = stringResource(R.string.banner_image)183    )184}185```186187---188189## AsyncImage vs SubcomposeAsyncImage190191| | AsyncImage | SubcomposeAsyncImage |192|---|---|---|193| API | Simple one-liner | Slot-based loading/error |194| Use when | Icons, known-size assets | Hero images, need skeleton UI |195| Recomposition | Crossfade built-in | Full control per state |196197## Checklist198199- [ ] `coil-network-okhttp:3.4.0` on classpath200- [ ] `contentDescription` set (or explicitly decorative)201- [ ] `painter.state.collectAsState()` not `.value`202- [ ] `LocalAsyncImageModelEqualityDelegate` for custom equality203- [ ] Singleton `ImageLoader` with memory + disk cache limits204

Search chapters, reference and source filenames.