Skip to content

Commit 19a5003

Browse files
committed
demo(useReducer): Type-safe Action type
1 parent ead8670 commit 19a5003

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ dependencies {
5656
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
5757
androidTestImplementation(libs.compose.test.junit4)
5858
debugImplementation(libs.compose.test.manifest)
59+
debugImplementation(libs.compose.tooling.debug)
5960
}

app/src/main/java/me/pavi2410/useCompose/app/screens/ReducerExample.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
package me.pavi2410.useCompose.app.screens
22

33
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.padding
45
import androidx.compose.material.Button
56
import androidx.compose.material.Text
67
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.tooling.preview.Preview
10+
import androidx.compose.ui.unit.dp
711
import me.pavi2410.useCompose.react.useReducer
812

913
data class MyState(val count: Int)
10-
data class MyAction(val type: String)
14+
sealed interface MyAction {
15+
object Increment : MyAction
16+
object Decrement : MyAction
17+
}
1118

1219
val initialState = MyState(0)
1320

21+
@Preview(showBackground = true)
1422
@Composable
1523
fun ReducerExample() {
16-
Column {
24+
Column(Modifier.padding(16.dp)) {
1725
val (state, dispatch) = useReducer<MyState, MyAction>(initialState) { state, action ->
18-
when (action.type) {
19-
"increment" -> state.copy(count = state.count + 1)
20-
"decrement" -> state.copy(count = state.count - 1)
21-
else -> throw Error()
26+
when (action) {
27+
MyAction.Increment -> state.copy(count = state.count + 1)
28+
MyAction.Decrement -> state.copy(count = state.count - 1)
2229
}
2330
}
2431

2532
Text("Count: ${state.count}")
2633
Button(onClick = {
27-
dispatch(MyAction("increment"))
34+
dispatch(MyAction.Increment)
2835
}) {
2936
Text("+")
3037
}
3138
Button(onClick = {
32-
dispatch(MyAction("decrement"))
39+
dispatch(MyAction.Decrement)
3340
}) {
3441
Text("-")
3542
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencyResolutionManagement {
1717
version("compose", "1.1.0")
1818
library("compose-ui", "androidx.compose.ui", "ui").versionRef("compose")
1919
library("compose-material", "androidx.compose.material", "material").versionRef("compose")
20+
library("compose-tooling-debug", "androidx.compose.ui", "ui-tooling").versionRef("compose")
2021
library("compose-tooling-preview", "androidx.compose.ui", "ui-tooling-preview").versionRef("compose")
2122
library("compose-test-junit4", "androidx.compose.ui", "ui-test-junit4").versionRef("compose")
2223
library("compose-test-manifest", "androidx.compose.ui", "ui-test-manifest").versionRef("compose")

0 commit comments

Comments
 (0)