Skip to content

Commit

Permalink
Merge pull request #633 from skydoves/improve/remember-states
Browse files Browse the repository at this point in the history
Remember the internal image states
  • Loading branch information
skydoves authored Jan 3, 2025
2 parents 08b7e17 + 425b7aa commit e4736e1
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 59 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 @@ -40,10 +40,6 @@ internal fun Project.configureAndroidCompose(
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}

packaging {
resources {
excludes.add("/META-INF/{AL2.0,LGPL2.1}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,14 @@ public fun CoilImage(
imageOptions = imageOptions,
modifier = modifier,
) ImageRequest@{ imageState ->
when (
val coilImageState = imageState.toCoilImageState().apply {

val coilImageState: CoilImageState = remember(imageState) {
imageState.toCoilImageState().apply {
onImageStateChanged.invoke(this)
}
) {
}

when (coilImageState) {
is CoilImageState.None -> Unit

is CoilImageState.Loading -> {
Expand Down
14 changes: 6 additions & 8 deletions coil3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import com.github.skydoves.landscapist.Configuration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
id("landscapist.library.compose.multiplatformWasm")
Expand Down Expand Up @@ -115,14 +117,10 @@ baselineProfile {
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
"-Xexplicit-api=strict",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=com.skydoves.landscapist.InternalLandscapistApi",
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.addAll(
"-opt-in=coil3.annotation.ExperimentalCoilApi",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ public fun CoilImage(
imageOptions = imageOptions,
modifier = modifier,
) ImageRequest@{ imageState ->
when (
val coilImageState = imageState.toCoilImageState().apply {

val coilImageState: CoilImageState = remember(imageState) {
imageState.toCoilImageState().apply {
onImageStateChanged.invoke(this)
}
) {
}

when (coilImageState) {
is CoilImageState.None -> Unit

is CoilImageState.Loading -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ public fun FrescoImage(
imageRequest = StableHolder(imageRequest.invoke()),
modifier = modifier,
) ImageRequest@{ imageState ->
when (
val frescoImageState = imageState.toFrescoImageState().apply {

val state: FrescoImageState = imageState.toFrescoImageState()
val frescoImageState = remember(state) {
state.apply {
onImageStateChanged.invoke(this)
}
) {
}

when (frescoImageState) {
is FrescoImageState.None -> Unit

is FrescoImageState.Loading -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalInspectionMode
Expand Down Expand Up @@ -149,11 +151,14 @@ public fun GlideImage(
clearTarget = clearTarget,
modifier = modifier,
) ImageRequest@{ imageState ->
when (
val glideImageState = imageState.toGlideImageState(

val glideImageState: GlideImageState = remember(imageState) {
imageState.toGlideImageState(
glideRequestType = glideRequestType,
).apply { onImageStateChanged.invoke(this) }
) {
}

when (glideImageState) {
is GlideImageState.None -> Unit

is GlideImageState.Loading -> {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dokka = "2.0.0"
jvmTarget = "11"
nexusPlugin = "0.30.0"
kotlinxCoroutines = "1.9.0"
kotlinBinaryCompatibility = "0.16.3"
kotlinBinaryCompatibility = "0.17.0"
androidGradlePlugin = "8.7.3"
androidxActivity = "1.9.3"
androidxMaterial = "1.12.0"
Expand Down
10 changes: 6 additions & 4 deletions landscapist-animation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import com.github.skydoves.landscapist.Configuration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
id("landscapist.library.compose.multiplatformWasm")
Expand Down Expand Up @@ -86,10 +88,10 @@ baselineProfile {
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.addAll(
"-Xexplicit-api=strict",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
Expand Down
10 changes: 6 additions & 4 deletions landscapist-palette/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import com.github.skydoves.landscapist.Configuration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
id("landscapist.library.compose.multiplatform")
Expand Down Expand Up @@ -81,10 +83,10 @@ baselineProfile {
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.addAll(
"-Xexplicit-api=strict",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
Expand Down
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)
}
10 changes: 6 additions & 4 deletions landscapist-placeholder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import com.github.skydoves.landscapist.Configuration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
id("landscapist.library.compose.multiplatformWasm")
Expand Down Expand Up @@ -86,10 +88,10 @@ baselineProfile {
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.addAll(
"-Xexplicit-api=strict",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
Expand Down
15 changes: 6 additions & 9 deletions landscapist/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import com.github.skydoves.landscapist.Configuration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
id("landscapist.library.compose.multiplatformWasm")
Expand Down Expand Up @@ -83,14 +85,9 @@ baselineProfile {
}
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs += listOf(
"-Xexplicit-api=strict",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=com.skydoves.landscapist.InternalLandscapistApi",
)
tasks.withType<KotlinJvmCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public fun <T : Any> ImageLoad(
var state by remember(recomposeKey, imageOptions) {
mutableStateOf<ImageLoadState>(ImageLoadState.None)
}
LaunchedEffect(recomposeKey, imageOptions) {
executeImageLoading(
executeImageRequest,
).collect {

LaunchedEffect(key1 = recomposeKey, key2 = imageOptions) {
executeImageLoading(executeImageRequest).collect {
state = it
}
}

BoxWithConstraints(
modifier = modifier.imageSemantics(imageOptions),
propagateMinConstraints = true,
Expand All @@ -85,7 +85,7 @@ public fun <T : Any> ImageLoad(
}
}

private suspend fun executeImageLoading(
private fun executeImageLoading(
executeImageRequest: suspend () -> Flow<ImageLoadState>,
) = flow {
// execute imager loading
Expand Down

0 comments on commit e4736e1

Please sign in to comment.