@@ -26,6 +26,10 @@ import androidx.compose.material3.Button
26
26
import androidx.compose.material3.OutlinedTextField
27
27
import androidx.compose.material3.Scaffold
28
28
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
29
33
import androidx.compose.ui.Modifier
30
34
import androidx.lifecycle.ViewModel
31
35
import androidx.lifecycle.viewmodel.compose.viewModel
@@ -48,62 +52,62 @@ class ResultEventActivity : ComponentActivity() {
48
52
49
53
setContent {
50
54
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
+ }
51
71
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} " )
74
80
}
75
- Text (" My returned name is ${viewModel.name} " )
76
81
}
77
82
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
+ }
93
97
}
94
98
}
99
+ else -> NavEntry (key) { Text (" Unknown route" ) }
95
100
}
96
- else -> NavEntry (key) { Text (" Unknown route" ) }
97
101
}
98
- }
99
- )
102
+ )
103
+ }
100
104
}
101
105
}
102
106
}
103
107
}
104
108
105
109
class HomeViewModel : ViewModel () {
106
- var name: String? = null
110
+ var name by mutableStateOf< String ?>( null )
107
111
}
108
112
109
113
typealias Name = String
0 commit comments