@@ -18,6 +18,7 @@ package com.google.samples.apps.nowinandroid.ui.interests2pane
18
18
19
19
import androidx.activity.compose.BackHandler
20
20
import androidx.annotation.Keep
21
+ import androidx.compose.animation.AnimatedContent
21
22
import androidx.compose.foundation.interaction.MutableInteractionSource
22
23
import androidx.compose.foundation.layout.Box
23
24
import androidx.compose.material3.LocalMinimumInteractiveComponentSize
@@ -50,13 +51,17 @@ import androidx.compose.ui.layout.layout
50
51
import androidx.compose.ui.unit.dp
51
52
import androidx.hilt.navigation.compose.hiltViewModel
52
53
import androidx.lifecycle.compose.collectAsStateWithLifecycle
54
+ import androidx.lifecycle.viewmodel.viewModelFactory
53
55
import androidx.navigation.NavGraphBuilder
54
56
import androidx.navigation.compose.NavHost
55
57
import androidx.navigation.compose.composable
56
58
import androidx.navigation.compose.rememberNavController
57
59
import com.google.samples.apps.nowinandroid.feature.interests.InterestsRoute
58
60
import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsRoute
59
61
import com.google.samples.apps.nowinandroid.feature.topic.TopicDetailPlaceholder
62
+ import com.google.samples.apps.nowinandroid.feature.topic.TopicScreen
63
+ import com.google.samples.apps.nowinandroid.feature.topic.TopicViewModel
64
+ import com.google.samples.apps.nowinandroid.feature.topic.TopicViewModel_Factory
60
65
import com.google.samples.apps.nowinandroid.feature.topic.navigation.TopicRoute
61
66
import com.google.samples.apps.nowinandroid.feature.topic.navigation.navigateToTopic
62
67
import com.google.samples.apps.nowinandroid.feature.topic.navigation.topicScreen
@@ -126,28 +131,10 @@ internal fun InterestsListDetailScreen(
126
131
val route = selectedTopicId?.let { TopicRoute (id = it) } ? : TopicPlaceholderRoute
127
132
mutableStateOf(route)
128
133
}
129
- var nestedNavKey by rememberSaveable(
130
- stateSaver = Saver ({ it.toString() }, UUID ::fromString),
131
- ) {
132
- mutableStateOf(UUID .randomUUID())
133
- }
134
- val nestedNavController = key(nestedNavKey) {
135
- rememberNavController()
136
- }
137
134
138
135
fun onTopicClickShowDetailPane (topicId : String ) {
139
136
onTopicClick(topicId)
140
- if (listDetailNavigator.isDetailPaneVisible()) {
141
- // If the detail pane was visible, then use the nestedNavController navigate call
142
- // directly
143
- nestedNavController.navigateToTopic(topicId) {
144
- popUpTo<DetailPaneNavHostRoute >()
145
- }
146
- } else {
147
- // Otherwise, recreate the NavHost entirely, and start at the new destination
148
- nestedNavHostStartRoute = TopicRoute (id = topicId)
149
- nestedNavKey = UUID .randomUUID()
150
- }
137
+ nestedNavHostStartRoute = TopicRoute (id = topicId)
151
138
coroutineScope.launch {
152
139
listDetailNavigator.navigateTo(ListDetailPaneScaffoldRole .Detail )
153
140
}
@@ -207,22 +194,25 @@ internal fun InterestsListDetailScreen(
207
194
}
208
195
},
209
196
) {
210
- key(nestedNavKey) {
211
- NavHost (
212
- navController = nestedNavController,
213
- startDestination = nestedNavHostStartRoute,
214
- route = DetailPaneNavHostRoute ::class ,
215
- ) {
216
- topicScreen(
217
- showBackButton = ! listDetailNavigator.isListPaneVisible(),
218
- onBackClick = {
219
- coroutineScope.launch {
220
- listDetailNavigator.navigateBack()
197
+ AnimatedContent (nestedNavHostStartRoute) { route ->
198
+ when (route) {
199
+ is TopicRoute -> {
200
+ TopicScreen (
201
+ showBackButton = ! listDetailNavigator.isListPaneVisible(),
202
+ onBackClick = {
203
+ coroutineScope.launch {
204
+ listDetailNavigator.navigateBack()
205
+ }
206
+ },
207
+ onTopicClick = ::onTopicClickShowDetailPane,
208
+ viewModel = hiltViewModel<TopicViewModel , TopicViewModel .Factory >(
209
+ key = route.id,
210
+ ) { factory ->
211
+ factory.create(route.id)
221
212
}
222
- },
223
- onTopicClick = ::onTopicClickShowDetailPane,
224
- )
225
- composable<TopicPlaceholderRoute > {
213
+ )
214
+ }
215
+ is TopicPlaceholderRoute -> {
226
216
TopicDetailPlaceholder ()
227
217
}
228
218
}
0 commit comments