Skip to content

Commit

Permalink
Improve the palette sample to avoid recompositions
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jan 3, 2025
1 parent 3a22212 commit 425b7aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ fun DisneyPosters(
PosterItem(poster, vm)
}
}
SelectedPoster(poster)

var palette by rememberPaletteState()

SelectedPoster(poster = poster, onPaletteUpdated = { palette = it })

palette?.let {
PosterInformation(poster = poster, it)
}
}
}

Expand All @@ -131,9 +138,8 @@ private fun PosterItem(
@Composable
private fun SelectedPoster(
poster: Poster,
onPaletteUpdated: (Palette) -> Unit,
) {
var palette by rememberPaletteState(null)

CoilImage(
imageModel = { poster.image },
modifier = Modifier.aspectRatio(0.8f),
Expand All @@ -149,11 +155,17 @@ private fun SelectedPoster(
),
)
+CircularRevealPlugin()
+PalettePlugin { palette = it }
+PalettePlugin { onPaletteUpdated.invoke(it) }
},
previewPlaceholder = painterResource(id = R.drawable.poster),
)
}

@Composable
private fun PosterInformation(
poster: Poster,
palette: Palette,
) {
ColorPalettes(palette)

Text(
Expand Down Expand Up @@ -190,7 +202,7 @@ private fun SelectedPoster(
}

@Composable
private fun ColorPalettes(palette: Palette?) {
private fun ColorPalettes(palette: Palette) {
val colorList: List<Int> = palette.paletteColorList()

LazyRow(
Expand Down Expand Up @@ -224,7 +236,7 @@ private fun SelectedPosterPreview() {
.fillMaxSize()
.verticalScroll(rememberScrollState()),
) {
SelectedPoster(poster = MockUtil.getMockPoster())
SelectedPoster(poster = MockUtil.getMockPoster()) {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.kmpalette.palette.graphics.Palette
*/
@Composable
public fun rememberPaletteState(
value: Palette?,
value: Palette? = null,
policy: SnapshotMutationPolicy<Palette?> = structuralEqualityPolicy(),
): MutableState<Palette?> = remember { mutableStateOf(value = value, policy = policy) }
): MutableState<Palette?> = remember(key1 = value) {
mutableStateOf(value = value, policy = policy)
}

0 comments on commit 425b7aa

Please sign in to comment.