Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1e04af6

Browse files
authoredApr 16, 2021
Migrate to rememberCoilPainter() (android#470)
* [Owl] Migrate to rememberCoilPainter() * [Crane] Migrate to rememberCoilPainter() * [Jetcaster] Migrate to rememberCoilPainter() * [Jetsurvey] Migrate to rememberCoilPainter() * [Jetsnack] Migrate to rememberCoilPainter()
1 parent 83460c2 commit 1e04af6

File tree

14 files changed

+81
-65
lines changed

14 files changed

+81
-65
lines changed
 

‎Crane/app/src/main/java/androidx/compose/samples/crane/base/ExploreSection.kt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import androidx.compose.ui.graphics.Color
4747
import androidx.compose.ui.layout.ContentScale
4848
import androidx.compose.ui.res.painterResource
4949
import androidx.compose.ui.unit.dp
50-
import com.google.accompanist.coil.CoilImage
50+
import com.google.accompanist.coil.rememberCoilPainter
51+
import com.google.accompanist.imageloading.ImageLoadState
5152

5253
@Composable
5354
fun ExploreSection(
@@ -93,21 +94,25 @@ private fun ExploreItem(
9394
.padding(top = 12.dp, bottom = 12.dp)
9495
) {
9596
ExploreImageContainer {
96-
CoilImage(
97-
data = item.imageUrl,
98-
fadeIn = true,
99-
contentScale = ContentScale.Crop,
100-
contentDescription = null,
101-
loading = {
102-
Box(Modifier.fillMaxSize()) {
103-
Image(
104-
modifier = Modifier.size(36.dp).align(Alignment.Center),
105-
painter = painterResource(id = R.drawable.ic_crane_logo),
106-
contentDescription = null
107-
)
108-
}
97+
Box {
98+
val painter = rememberCoilPainter(item.imageUrl, fadeIn = true)
99+
Image(
100+
painter = painter,
101+
contentDescription = null,
102+
contentScale = ContentScale.Crop,
103+
modifier = Modifier.fillMaxSize(),
104+
)
105+
106+
if (painter.loadState == ImageLoadState.Loading) {
107+
Image(
108+
painter = painterResource(id = R.drawable.ic_crane_logo),
109+
contentDescription = null,
110+
modifier = Modifier
111+
.size(36.dp)
112+
.align(Alignment.Center),
113+
)
109114
}
110-
)
115+
}
111116
}
112117
Spacer(Modifier.width(24.dp))
113118
Column {

‎Crane/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ subprojects {
4242

4343
if (!Libs.AndroidX.Compose.snapshot.isEmpty()) {
4444
maven { url Urls.composeSnapshotRepo }
45+
}
46+
47+
if (Libs.Accompanist.version.endsWith('SNAPSHOT')) {
4548
maven { url Urls.mavenCentralSnapshotRepo }
4649
}
4750
}

‎Crane/buildSrc/src/main/java/com/example/crane/buildsrc/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Libs {
3030
}
3131

3232
object Accompanist {
33-
private const val version = "0.7.1"
33+
const val version = "0.7.2-SNAPSHOT"
3434
const val coil = "com.google.accompanist:accompanist-coil:$version"
3535
}
3636

‎Jetcaster/app/src/main/java/com/example/jetcaster/ui/home/Home.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import com.example.jetcaster.util.constrastAgainst
7373
import com.example.jetcaster.util.quantityStringResource
7474
import com.example.jetcaster.util.rememberDominantColorState
7575
import com.example.jetcaster.util.verticalGradientScrim
76-
import com.google.accompanist.coil.CoilImage
76+
import com.google.accompanist.coil.rememberCoilPainter
7777
import com.google.accompanist.insets.statusBarsHeight
7878
import com.google.accompanist.pager.ExperimentalPagerApi
7979
import com.google.accompanist.pager.HorizontalPager
@@ -351,14 +351,13 @@ private fun FollowedPodcastCarouselItem(
351351
.aspectRatio(1f)
352352
) {
353353
if (podcastImageUrl != null) {
354-
CoilImage(
355-
data = podcastImageUrl,
354+
Image(
355+
painter = rememberCoilPainter(podcastImageUrl),
356356
contentDescription = null,
357357
contentScale = ContentScale.Crop,
358-
loading = { /* TODO do something better here */ },
359358
modifier = Modifier
360359
.fillMaxSize()
361-
.clip(MaterialTheme.shapes.medium)
360+
.clip(MaterialTheme.shapes.medium),
362361
)
363362
}
364363

‎Jetcaster/app/src/main/java/com/example/jetcaster/ui/home/category/PodcastCategory.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import com.example.jetcaster.ui.theme.JetcasterTheme
7474
import com.example.jetcaster.ui.theme.Keyline1
7575
import com.example.jetcaster.util.ToggleFollowPodcastIconButton
7676
import com.example.jetcaster.util.viewModelProviderFactoryOf
77-
import com.google.accompanist.coil.CoilImage
77+
import com.google.accompanist.coil.rememberCoilPainter
7878
import java.time.format.DateTimeFormatter
7979
import java.time.format.FormatStyle
8080

@@ -145,20 +145,18 @@ fun EpisodeListItem(
145145
)
146146

147147
if (podcast.imageUrl != null) {
148-
// If we have an image Url, we can show it using [CoilImage]
149-
CoilImage(
150-
data = podcast.imageUrl,
148+
// If we have an image Url, we can show it using Coil
149+
Image(
150+
painter = rememberCoilPainter(podcast.imageUrl, fadeIn = true),
151151
contentDescription = null,
152-
fadeIn = true,
153152
contentScale = ContentScale.Crop,
154-
loading = { /* TODO do something better here */ },
155153
modifier = Modifier
156154
.size(56.dp)
157155
.clip(MaterialTheme.shapes.medium)
158156
.constrainAs(image) {
159157
end.linkTo(parent.end, 16.dp)
160158
top.linkTo(parent.top, 16.dp)
161-
}
159+
},
162160
)
163161
} else {
164162
// If we don't have an image url, we need to make sure that the constraint reference
@@ -329,15 +327,13 @@ private fun TopPodcastRowItem(
329327
.align(Alignment.CenterHorizontally)
330328
) {
331329
if (podcastImageUrl != null) {
332-
CoilImage(
333-
data = podcastImageUrl,
330+
Image(
331+
painter = rememberCoilPainter(podcastImageUrl, fadeIn = true),
334332
contentDescription = null,
335-
fadeIn = true,
336333
contentScale = ContentScale.Crop,
337-
loading = { /* TODO do something better here */ },
338334
modifier = Modifier
339335
.fillMaxSize()
340-
.clip(MaterialTheme.shapes.medium)
336+
.clip(MaterialTheme.shapes.medium),
341337
)
342338
}
343339

‎Jetcaster/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ subprojects {
4242
// Jetpack Compose SNAPSHOTs
4343
if (!Libs.AndroidX.Compose.snapshot.isEmpty()) {
4444
maven { url "https://androidx.dev/snapshots/builds/${Libs.AndroidX.Compose.snapshot}/artifacts/repository/" }
45+
}
46+
if (Libs.Accompanist.version.endsWith('SNAPSHOT')) {
4547
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
4648
}
4749
}

‎Jetcaster/buildSrc/src/main/java/com/example/jetcaster/buildsrc/dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object Libs {
2727
const val material = "com.google.android.material:material:1.3.0"
2828

2929
object Accompanist {
30-
private const val version = "0.7.1"
30+
const val version = "0.7.2-SNAPSHOT"
3131
const val coil = "com.google.accompanist:accompanist-coil:$version"
3232
const val insets = "com.google.accompanist:accompanist-insets:$version"
3333
const val pager = "com.google.accompanist:accompanist-pager:$version"

‎Jetsnack/app/src/main/java/com/example/jetsnack/ui/components/Snacks.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.example.jetsnack.ui.components
1818

19+
import androidx.compose.foundation.Image
1920
import androidx.compose.foundation.clickable
2021
import androidx.compose.foundation.layout.Arrangement
2122
import androidx.compose.foundation.layout.Box
@@ -57,7 +58,7 @@ import com.example.jetsnack.model.Snack
5758
import com.example.jetsnack.model.SnackCollection
5859
import com.example.jetsnack.model.snacks
5960
import com.example.jetsnack.ui.theme.JetsnackTheme
60-
import com.google.accompanist.coil.CoilImage
61+
import com.google.accompanist.coil.rememberCoilPainter
6162

6263
private val HighlightCardWidth = 170.dp
6364
private val HighlightCardPadding = 16.dp
@@ -279,12 +280,14 @@ fun SnackImage(
279280
shape = CircleShape,
280281
modifier = modifier
281282
) {
282-
CoilImage(
283-
data = imageUrl,
283+
Image(
284+
painter = rememberCoilPainter(
285+
request = imageUrl,
286+
previewPlaceholder = R.drawable.placeholder,
287+
),
284288
contentDescription = contentDescription,
285-
previewPlaceholder = R.drawable.placeholder,
289+
modifier = Modifier.fillMaxSize(),
286290
contentScale = ContentScale.Crop,
287-
modifier = Modifier.fillMaxSize()
288291
)
289292
}
290293
}

‎Jetsnack/buildSrc/src/main/java/com/example/jetsnack/buildsrc/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object Libs {
2424
const val androidGradlePlugin = "com.android.tools.build:gradle:7.0.0-alpha14"
2525

2626
object Accompanist {
27-
const val version = "0.7.1"
27+
const val version = "0.7.2-SNAPSHOT"
2828
const val coil = "com.google.accompanist:accompanist-coil:$version"
2929
const val insets = "com.google.accompanist:accompanist-insets:$version"
3030
}

‎Jetsurvey/app/src/main/java/com/example/compose/jetsurvey/survey/SurveyQuestions.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import androidx.compose.ui.tooling.preview.Preview
6464
import androidx.compose.ui.unit.dp
6565
import com.example.compose.jetsurvey.R
6666
import com.example.compose.jetsurvey.theme.JetsurveyTheme
67-
import com.google.accompanist.coil.CoilImage
67+
import com.google.accompanist.coil.rememberCoilPainter
6868

6969
@Composable
7070
fun Question(
@@ -317,11 +317,10 @@ private fun PhotoQuestion(
317317
) {
318318
Column {
319319
if (answer != null && answer.result is SurveyActionResult.Photo) {
320-
CoilImage(
321-
data = answer.result.uri,
320+
Image(
321+
painter = rememberCoilPainter(answer.result.uri, fadeIn = true),
322+
contentDescription = null,
322323
modifier = Modifier.fillMaxSize(),
323-
fadeIn = true,
324-
contentDescription = null
325324
)
326325
} else {
327326
PhotoDefaultImage(modifier = Modifier.padding(horizontal = 86.dp, vertical = 74.dp))

‎Jetsurvey/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ subprojects {
4040

4141
if (Libs.AndroidX.Compose.version.endsWith('SNAPSHOT')) {
4242
maven { url Libs.AndroidX.Compose.snapshotUrl }
43+
}
44+
if (Libs.Accompanist.version.endsWith('SNAPSHOT')) {
4345
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
4446
}
4547
}

‎Jetsurvey/buildSrc/src/main/java/com/example/compose/jetsurvey/buildsrc/dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object Libs {
2929
const val material = "com.google.android.material:material:1.3.0"
3030

3131
object Accompanist {
32-
private const val version = "0.7.1"
32+
const val version = "0.7.2-SNAPSHOT"
3333
const val coil = "com.google.accompanist:accompanist-coil:$version"
3434
}
3535

‎Owl/app/src/main/java/com/example/owl/ui/utils/NetworkImage.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
package com.example.owl.ui.utils
1818

19+
import androidx.compose.foundation.Image
1920
import androidx.compose.foundation.background
21+
import androidx.compose.foundation.layout.Box
2022
import androidx.compose.foundation.layout.Spacer
21-
import androidx.compose.foundation.layout.fillMaxSize
2223
import androidx.compose.material.MaterialTheme
2324
import androidx.compose.runtime.Composable
2425
import androidx.compose.runtime.CompositionLocalProvider
@@ -34,12 +35,14 @@ import coil.request.ImageResult
3435
import coil.size.PixelSize
3536
import com.example.owl.R
3637
import com.example.owl.ui.theme.compositedOnSurface
37-
import com.google.accompanist.coil.CoilImage
3838
import com.google.accompanist.coil.LocalImageLoader
39+
import com.google.accompanist.coil.rememberCoilPainter
40+
import com.google.accompanist.imageloading.ImageLoadState
3941
import okhttp3.HttpUrl
4042

4143
/**
42-
* A wrapper around [CoilImage] setting a default [contentScale] and loading placeholder.
44+
* A wrapper around [Image] and [rememberCoilPainter], setting a
45+
* default [contentScale] and showing content while loading.
4346
*/
4447
@Composable
4548
fun NetworkImage(
@@ -49,22 +52,26 @@ fun NetworkImage(
4952
contentScale: ContentScale = ContentScale.Crop,
5053
placeholderColor: Color? = MaterialTheme.colors.compositedOnSurface(0.2f)
5154
) {
52-
CoilImage(
53-
data = url,
54-
modifier = modifier,
55-
previewPlaceholder = R.drawable.photo_architecture,
56-
contentDescription = contentDescription,
57-
contentScale = contentScale,
58-
loading = {
59-
if (placeholderColor != null) {
60-
Spacer(
61-
modifier = Modifier
62-
.fillMaxSize()
63-
.background(placeholderColor)
64-
)
65-
}
55+
Box(modifier) {
56+
val painter = rememberCoilPainter(
57+
request = url,
58+
previewPlaceholder = R.drawable.photo_architecture,
59+
)
60+
61+
Image(
62+
painter = painter,
63+
contentDescription = contentDescription,
64+
contentScale = contentScale,
65+
)
66+
67+
if (painter.loadState == ImageLoadState.Loading && placeholderColor != null) {
68+
Spacer(
69+
modifier = Modifier
70+
.matchParentSize()
71+
.background(placeholderColor)
72+
)
6673
}
67-
)
74+
}
6875
}
6976

7077
@Composable

‎Owl/buildSrc/src/main/java/com/example/owl/buildsrc/Dependencies.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object Libs {
2424
const val androidGradlePlugin = "com.android.tools.build:gradle:7.0.0-alpha14"
2525

2626
object Accompanist {
27-
const val version = "0.7.1"
27+
const val version = "0.7.2-SNAPSHOT"
2828
const val coil = "com.google.accompanist:accompanist-coil:$version"
2929
const val insets = "com.google.accompanist:accompanist-insets:$version"
3030
}

0 commit comments

Comments
 (0)
Failed to load comments.