fun WearApp()

in ComposeAdvanced/app/src/main/java/com/example/android/wearable/composeadvanced/presentation/MainActivity.kt [72:169]


fun WearApp(watchRepository: WatchRepository) {
    WearAppTheme {
        // The state for the ScalingLazyColumn in other screen is hoisted to this level, so
        // the Scaffold can properly place the position indicator (also known as the
        // scroll indicator). We use it for various other things (like hiding the time when
        // the user is scrolling).
        //
        // This current solution works for one scrolling type Composable, that is, we only
        // have one Composable capable of scrolling.
        val scalingLazyListState: ScalingLazyListState = rememberScalingLazyListState()

        val swipeDismissableNavController = rememberSwipeDismissableNavController()

        // Allows user to disable the text before the time and hide the vignette.
        var showProceedingTextBeforeTime by rememberSaveable { mutableStateOf(false) }
        var vignetteVisible by rememberSaveable { mutableStateOf(true) }

        Scaffold(
            // Scaffold places time at top of screen to follow Material Design guidelines.
            timeText = {
                CustomTimeText(
                    visible = !scalingLazyListState.isScrollInProgress,
                    showLeadingText = showProceedingTextBeforeTime,
                    leadingText = stringResource(R.string.leading_time_text)
                )
            },
            vignette = {
                CustomVignette(
                    visible = vignetteVisible,
                    vignettePosition = VignettePosition.TopAndBottom
                )
            },
            positionIndicator = {
                CustomPositionIndicator(
                    visible = scalingLazyListState.isScrollInProgress,
                    scalingLazyListState = scalingLazyListState
                )
            }
        ) {

            /*
             * Wear OS's version of NavHost supports swipe-to-dismiss (similar to back
             * gesture on mobile). Otherwise, the code looks very similar.
             */
            SwipeDismissableNavHost(
                navController = swipeDismissableNavController,
                startDestination = Screen.Landing.route,
                modifier = Modifier.background(MaterialTheme.colors.background)
            ) {

                // Main Window
                composable(Screen.Landing.route) {
                    LandingScreen(
                        onClickWatchList = {
                            swipeDismissableNavController.navigate(Screen.WatchList.route)
                        },
                        proceedingTimeTextEnabled = showProceedingTextBeforeTime,
                        onClickProceedingTimeText = {
                            showProceedingTextBeforeTime = !showProceedingTextBeforeTime
                        }
                    )
                }

                composable(Screen.WatchList.route) {
                    WatchListScreen(
                        scalingLazyListState = scalingLazyListState,
                        watchRepository = watchRepository,
                        showVignette = vignetteVisible,
                        onClickVignetteToggle = { showVignette ->
                            vignetteVisible = showVignette
                        },
                        onClickWatch = { id ->
                            swipeDismissableNavController.navigate(
                                route = Screen.WatchDetail.route + "/" + id,
                            )
                        }
                    )
                }

                composable(
                    route = Screen.WatchDetail.route + "/{$WATCH_ID_NAV_ARGUMENT}",
                    arguments = listOf(
                        navArgument(WATCH_ID_NAV_ARGUMENT) {
                            type = NavType.IntType
                        }
                    )
                ) { navBackStackEntry ->
                    val watchId =
                        navBackStackEntry.arguments?.getInt(WATCH_ID_NAV_ARGUMENT)!!
                    WatchDetailScreen(
                        id = watchId,
                        watchRepository = watchRepository
                    )
                }
            }
        }
    }
}