Skip to content

Commit b29d10b

Browse files
Jeremy Woodsjbw0033
authored andcommitted
Fix errors on result event sample
1 parent 1356254 commit b29d10b

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed

app/src/main/java/com/example/nav3recipes/results/event/ResultEventActivity.kt

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import androidx.compose.material3.Button
2626
import androidx.compose.material3.OutlinedTextField
2727
import androidx.compose.material3.Scaffold
2828
import androidx.compose.material3.Text
29+
import androidx.compose.runtime.CompositionLocalProvider
30+
import androidx.compose.runtime.getValue
31+
import androidx.compose.runtime.mutableStateOf
32+
import androidx.compose.runtime.setValue
2933
import androidx.compose.ui.Modifier
3034
import androidx.lifecycle.ViewModel
3135
import androidx.lifecycle.viewmodel.compose.viewModel
@@ -48,62 +52,62 @@ class ResultEventActivity : ComponentActivity() {
4852

4953
setContent {
5054
val resultStore = LocalResultStore.current
55+
CompositionLocalProvider(LocalResultStore.provides(resultStore)) {
56+
Scaffold { paddingValues ->
57+
58+
val backStack = rememberNavBackStack(Home)
59+
60+
NavDisplay(
61+
backStack = backStack,
62+
modifier = Modifier.padding(paddingValues),
63+
onBack = { backStack.removeLastOrNull() },
64+
entryProvider = { key ->
65+
when (key) {
66+
is Home -> NavEntry(key) {
67+
val viewModel = viewModel<HomeViewModel>(key = Home.toString())
68+
ResultEffect<Name?> { name ->
69+
viewModel.name = name
70+
}
5171

52-
Scaffold { paddingValues ->
53-
54-
val backStack = rememberNavBackStack(Home)
55-
56-
NavDisplay(
57-
backStack = backStack,
58-
modifier = Modifier.padding(paddingValues),
59-
onBack = { backStack.removeLastOrNull() },
60-
entryProvider = { key ->
61-
when (key) {
62-
is Home -> NavEntry(key) {
63-
val viewModel = viewModel<HomeViewModel>(key = Home.toString())
64-
ResultEffect<Name?>(resultStore) { name ->
65-
viewModel.name = name
66-
}
67-
68-
Column {
69-
Text("Welcome to Nav3")
70-
Button(onClick = {
71-
backStack.add(ResultPage())
72-
}) {
73-
Text("Click to navigate")
72+
Column {
73+
Text("Welcome to Nav3")
74+
Button(onClick = {
75+
backStack.add(ResultPage())
76+
}) {
77+
Text("Click to navigate")
78+
}
79+
Text("My returned name is ${viewModel.name}")
7480
}
75-
Text("My returned name is ${viewModel.name}")
7681
}
7782

78-
79-
}
80-
is ResultPage -> NavEntry(key) {
81-
Column {
82-
83-
val state = rememberTextFieldState()
84-
OutlinedTextField(
85-
state = state,
86-
label = { Text("Result to Return") }
87-
)
88-
Button(onClick = {
89-
resultStore.sendResult<Name>(result = state.text as String)
90-
backStack.removeLastOrNull()
91-
}) {
92-
Text("Return result")
83+
is ResultPage -> NavEntry(key) {
84+
Column {
85+
86+
val state = rememberTextFieldState()
87+
OutlinedTextField(
88+
state = state,
89+
label = { Text("Result to Return") }
90+
)
91+
Button(onClick = {
92+
resultStore.sendResult<Name>(result = state.text.toString())
93+
backStack.removeLastOrNull()
94+
}) {
95+
Text("Return result")
96+
}
9397
}
9498
}
99+
else -> NavEntry(key) { Text("Unknown route") }
95100
}
96-
else -> NavEntry(key) { Text("Unknown route") }
97101
}
98-
}
99-
)
102+
)
103+
}
100104
}
101105
}
102106
}
103107
}
104108

105109
class HomeViewModel : ViewModel() {
106-
var name: String? = null
110+
var name by mutableStateOf<String?>(null)
107111
}
108112

109113
typealias Name = String

app/src/main/java/com/example/nav3recipes/results/event/ResultStore.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import kotlinx.coroutines.flow.receiveAsFlow
1111

1212

1313
object LocalResultStore {
14-
private val LocalResultStore: ProvidableCompositionLocal<ResultStore?>
15-
get() {
16-
return compositionLocalOf { null }
17-
}
14+
private val LocalResultStore: ProvidableCompositionLocal<ResultStore?> =
15+
compositionLocalOf { null }
1816

1917
val current: ResultStore
2018
@Composable

app/src/main/java/com/example/nav3recipes/results/state/ResultStateActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ResultStateActivity : ComponentActivity() {
8585
)
8686

8787
Button(onClick = {
88-
result = state.text as String
88+
result = state.text.toString()
8989
backStack.removeLastOrNull()
9090
}) {
9191
Text("Return result")

0 commit comments

Comments
 (0)