Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Merge branch 'main' into dt/nav-safe-args (AnimatedPane broken)

Don Turnerf711e69cd2ae
diff --cc app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/interests2pane/InterestsListDetailScreen.kt
index 1f2bbf6df,ada4e49d1..d1ead05dd
--- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/interests2pane/InterestsListDetailScreen.kt
+++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/interests2pane/InterestsListDetailScreen.kt
@@@ -24,28 -26,44 +26,36 @@@ import androidx.compose.material3.adapt
import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldNavigator
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
+ import androidx.compose.runtime.key
+ import androidx.compose.runtime.mutableStateOf
+ import androidx.compose.runtime.remember
+ import androidx.compose.runtime.saveable.Saver
+ import androidx.compose.runtime.saveable.rememberSaveable
+ import androidx.compose.runtime.setValue
+import androidx.compose.runtime.setValue
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
-import androidx.navigation.navArgument
import com.google.samples.apps.nowinandroid.feature.interests.InterestsRoute
-import com.google.samples.apps.nowinandroid.feature.interests.navigation.INTERESTS_ROUTE
-import com.google.samples.apps.nowinandroid.feature.interests.navigation.TOPIC_ID_ARG
+import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsDestination
import com.google.samples.apps.nowinandroid.feature.topic.TopicDetailPlaceholder
-import com.google.samples.apps.nowinandroid.feature.topic.navigation.TOPIC_ROUTE
-import com.google.samples.apps.nowinandroid.feature.topic.navigation.createTopicRoute
++import com.google.samples.apps.nowinandroid.feature.topic.navigation.TopicDestination
import com.google.samples.apps.nowinandroid.feature.topic.navigation.navigateToTopic
import com.google.samples.apps.nowinandroid.feature.topic.navigation.topicScreen
+ import java.util.UUID
+import kotlinx.serialization.Serializable
+
+@Serializable object TopicPlaceholderDestination
-private const val DETAIL_PANE_NAVHOST_ROUTE = "detail_pane_route"
+@Serializable object DetailPaneNavHostDestination
fun NavGraphBuilder.interestsListDetailScreen() {
- composable(
- route = INTERESTS_ROUTE,
- arguments = listOf(
- navArgument(TOPIC_ID_ARG) {
- type = NavType.StringType
- defaultValue = null
- nullable = true
- },
- ),
- ) {
+ composable<InterestsDestination> {
InterestsListDetailScreen()
}
}
@@@ -72,13 -97,31 +89,34 @@@ internal fun InterestsListDetailScreen
listDetailNavigator.navigateBack()
}
- val nestedNavController = rememberNavController()
+ var nestedNavHostStartDestination by remember {
- mutableStateOf(selectedTopicId?.let(::createTopicRoute) ?: TOPIC_ROUTE)
++ val destination = selectedTopicId?.let { TopicDestination(id = it) } ?: TopicPlaceholderDestination
++ mutableStateOf(destination)
+ }
+ var nestedNavKey by rememberSaveable(
+ stateSaver = Saver({ it.toString() }, UUID::fromString),
+ ) {
+ mutableStateOf(UUID.randomUUID())
+ }
+ val nestedNavController = key(nestedNavKey) {
+ rememberNavController()
+ }
fun onTopicClickShowDetailPane(topicId: String) {
onTopicClick(topicId)
- nestedNavController.navigateToTopic(topicId) {
- popUpTo<DetailPaneNavHostDestination>()
- }
- if (listDetailNavigator.isDetailPaneVisible()) {
++
++ // TODO (merge): Fix this
++ //if (listDetailNavigator.isDetailPaneVisible()) {
+ // If the detail pane was visible, then use the nestedNavController navigate call
+ // directly
+ nestedNavController.navigateToTopic(topicId) {
- popUpTo(DETAIL_PANE_NAVHOST_ROUTE)
++ popUpTo<DetailPaneNavHostDestination>()
+ }
- } else {
++ /*} else {
+ // Otherwise, recreate the NavHost entirely, and start at the new destination
- nestedNavHostStartDestination = createTopicRoute(topicId)
++ nestedNavHostStartDestination = TopicDestination(id = topicId)
+ nestedNavKey = UUID.randomUUID()
- }
++ }*/
listDetailNavigator.navigateTo(ListDetailPaneScaffoldRole.Detail)
}
@@@ -86,34 -129,34 +124,43 @@@
value = listDetailNavigator.scaffoldValue,
directive = listDetailNavigator.scaffoldDirective,
listPane = {
- InterestsRoute(
- onTopicClick = ::onTopicClickShowDetailPane,
- highlightSelectedTopic = listDetailNavigator.isDetailPaneVisible(),
- )
- },
- detailPane = {
- NavHost(
- navController = nestedNavController,
- startDestination = TopicPlaceholderDestination,
- route = DetailPaneNavHostDestination::class,
- ) {
- topicScreen(
- showBackButton = !listDetailNavigator.isListPaneVisible(),
- onBackClick = listDetailNavigator::navigateBack,
- AnimatedPane {
++ // TODO (merge): Fix this
++ //AnimatedPane {
+ InterestsRoute(
onTopicClick = ::onTopicClickShowDetailPane,
+ highlightSelectedTopic = listDetailNavigator.isDetailPaneVisible(),
)
- composable<TopicPlaceholderDestination> {
- TopicDetailPlaceholder()
- }
-- }
++ //}
+ },
+ detailPane = {
- AnimatedPane {
- key(nestedNavKey) {
++ // TODO (merge): Fix this
++ //AnimatedPane {
++ // key(nestedNavKey) {
+ NavHost(
+ navController = nestedNavController,
+ startDestination = nestedNavHostStartDestination,
- route = DETAIL_PANE_NAVHOST_ROUTE,
++ route = DetailPaneNavHostDestination::class,
+ ) {
+ topicScreen(
+ showBackButton = !listDetailNavigator.isListPaneVisible(),
+ onBackClick = listDetailNavigator::navigateBack,
+ onTopicClick = ::onTopicClickShowDetailPane,
+ )
- composable(route = TOPIC_ROUTE) {
++ composable<TopicPlaceholderDestination> {
+ TopicDetailPlaceholder()
+ }
+ }
- }
- }
++ // }
++ //}
},
)
++ // TODO (merge): Remove
+ LaunchedEffect(Unit) {
+ if (selectedTopicId != null) {
+ // Initial topic ID was provided when navigating to Interests, so show its details.
+ onTopicClickShowDetailPane(selectedTopicId)
+ }
+ }
}
@OptIn(ExperimentalMaterial3AdaptiveApi::class)