Skip to content

Commit fed08c3

Browse files
committed
Persist zoom, pan state across config changes
1 parent 700be9f commit fed08c3

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

minesweeper-ui/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id("com.android.library")
33
kotlin("android")
4+
id("kotlin-parcelize")
45
}
56

67
android {

minesweeper-ui/src/main/java/com/jayasuryat/minesweeperui/component/ZoomableContent.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ import androidx.compose.runtime.Stable
2222
import androidx.compose.runtime.mutableStateOf
2323
import androidx.compose.runtime.remember
2424
import androidx.compose.ui.Modifier
25-
import androidx.compose.ui.graphics.graphicsLayer
2625
import androidx.compose.ui.input.pointer.pointerInput
26+
import com.jayasuryat.minesweeperui.grid.ZoomPanState
2727
import kotlin.math.absoluteValue
2828

2929
@Composable
3030
internal fun ZoomableContent(
3131
modifier: Modifier = Modifier,
32-
content: @Composable (zoomModifier: Modifier) -> Unit,
32+
defaultState: ZoomPanState,
33+
content: @Composable (zoomPanState: ZoomPanState) -> Unit,
3334
) {
3435

35-
val scale = remember { mutableStateOf(1f) }
36-
val translateX = remember { mutableStateOf(0f) }
37-
val translateY = remember { mutableStateOf(0f) }
36+
val scale = remember { mutableStateOf(defaultState.scale) }
37+
val translateX = remember { mutableStateOf(defaultState.translationX) }
38+
val translateY = remember { mutableStateOf(defaultState.translationY) }
3839

3940
Box(
4041
modifier = modifier
@@ -68,13 +69,11 @@ internal fun ZoomableContent(
6869
) {
6970

7071
content(
71-
zoomModifier = Modifier
72-
.graphicsLayer(
73-
scaleX = scale.value,
74-
scaleY = scale.value,
75-
translationX = translateX.value,
76-
translationY = translateY.value,
77-
)
72+
ZoomPanState(
73+
scale = scale.value,
74+
translationX = translateX.value,
75+
translationY = translateY.value,
76+
)
7877
)
7978
}
8079
}

minesweeper-ui/src/main/java/com/jayasuryat/minesweeperui/grid/Minefield.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
package com.jayasuryat.minesweeperui.grid
1717

1818
import androidx.compose.runtime.Composable
19+
import androidx.compose.runtime.mutableStateOf
20+
import androidx.compose.runtime.remember
21+
import androidx.compose.runtime.saveable.rememberSaveable
1922
import androidx.compose.ui.Modifier
23+
import androidx.compose.ui.graphics.graphicsLayer
2024
import com.jayasuryat.minesweeperui.action.CellInteractionListener
2125
import com.jayasuryat.minesweeperui.component.ZoomableContent
2226
import com.jayasuryat.util.LogCompositions
@@ -30,10 +34,30 @@ public fun Minefield(
3034

3135
LogCompositions(name = "Minefield")
3236

33-
ZoomableContent { zoomModifier: Modifier ->
37+
val savableState = rememberSaveable {
38+
mutableStateOf(
39+
ZoomPanState(
40+
scale = 1f,
41+
translationX = 0f,
42+
translationY = 0f,
43+
)
44+
)
45+
}
46+
val zoomPanState = remember { savableState.value }
47+
48+
ZoomableContent(
49+
defaultState = zoomPanState,
50+
) { zoomState: ZoomPanState ->
51+
52+
savableState.value = zoomState
3453

3554
MineGrid(
36-
modifier = modifier.then(zoomModifier),
55+
modifier = modifier.then(Modifier.graphicsLayer {
56+
scaleX = zoomState.scale
57+
scaleY = zoomState.scale
58+
translationX = zoomState.translationX
59+
translationY = zoomState.translationY
60+
}),
3761
gridInfo = gridInfo,
3862
actionListener = actionListener,
3963
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jayasuryat.minesweeperui.grid
2+
3+
import android.os.Parcelable
4+
import androidx.compose.runtime.Immutable
5+
import kotlinx.parcelize.Parcelize
6+
7+
@Parcelize
8+
@Immutable
9+
internal data class ZoomPanState(
10+
val scale: Float,
11+
val translationX: Float,
12+
val translationY: Float,
13+
) : Parcelable

0 commit comments

Comments
 (0)