-
Notifications
You must be signed in to change notification settings - Fork 362
Add snippets for Wear Navigation 3 #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ithinkihaveacat
wants to merge
1
commit into
android:main
Choose a base branch
from
ithinkihaveacat:wear-nav3-snippets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
wear/src/main/java/com/example/wear/snippets/m3/navigation/Navigation3.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,110 @@ | ||||||||||
| /* | ||||||||||
| * Copyright 2026 The Android Open Source Project | ||||||||||
| * | ||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||
| * You may obtain a copy of the License at | ||||||||||
| * | ||||||||||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| * | ||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| * See the License for the specific language governing permissions and | ||||||||||
| * limitations under the License. | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| package com.example.wear.snippets.m3.navigation | ||||||||||
|
|
||||||||||
| import androidx.compose.runtime.Composable | ||||||||||
| import androidx.lifecycle.ViewModel | ||||||||||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||||||||||
| import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator | ||||||||||
| import androidx.navigation3.runtime.NavKey | ||||||||||
| import androidx.navigation3.runtime.entryProvider | ||||||||||
| import androidx.navigation3.runtime.rememberNavBackStack | ||||||||||
| import androidx.navigation3.runtime.rememberSaveableStateHolderNavEntryDecorator | ||||||||||
| import androidx.navigation3.ui.NavDisplay | ||||||||||
| import androidx.wear.compose.navigation3.rememberSwipeDismissableSceneStrategy | ||||||||||
| import kotlinx.serialization.Serializable | ||||||||||
|
|
||||||||||
| // [START android_wear_nav3_destinations] | ||||||||||
| @Serializable | ||||||||||
| sealed interface Screen : NavKey { | ||||||||||
| @Serializable | ||||||||||
| data object Home : Screen | ||||||||||
|
|
||||||||||
| @Serializable | ||||||||||
| data class Details(val itemId: String) : Screen | ||||||||||
| } | ||||||||||
| // [END android_wear_nav3_destinations] | ||||||||||
|
|
||||||||||
| // [START android_wear_nav3_setup] | ||||||||||
| @Composable | ||||||||||
| fun WearApp() { | ||||||||||
| // 1. Create the persistent back stack starting at the Home screen | ||||||||||
| val backStack = rememberNavBackStack(Screen.Home) | ||||||||||
|
|
||||||||||
| // 2. Initialize the Wear OS swipe-to-dismiss strategy | ||||||||||
| val strategy = rememberSwipeDismissableSceneStrategy<NavKey>() | ||||||||||
|
|
||||||||||
| // 3. Render the NavDisplay | ||||||||||
| NavDisplay( | ||||||||||
| backStack = backStack, | ||||||||||
| sceneStrategies = listOf(strategy), | ||||||||||
|
ithinkihaveacat marked this conversation as resolved.
|
||||||||||
| entryProvider = entryProvider { | ||||||||||
| // 4. Map keys to Composables | ||||||||||
| entry<Screen.Home> { | ||||||||||
| HomeScreen( | ||||||||||
| onNavigateToDetails = { id -> backStack.add(Screen.Details(id)) } | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| entry<Screen.Details> { key -> | ||||||||||
| DetailsScreen( | ||||||||||
| itemId = key.itemId, | ||||||||||
| onBack = { backStack.removeLast() } | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| // [END android_wear_nav3_setup] | ||||||||||
|
|
||||||||||
| @Composable | ||||||||||
| fun WearAppWithViewModel() { | ||||||||||
| val backStack = rememberNavBackStack(Screen.Home) | ||||||||||
| val strategy = rememberSwipeDismissableSceneStrategy<NavKey>() | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||
|
|
||||||||||
| // [START android_wear_nav3_viewmodel] | ||||||||||
| NavDisplay( | ||||||||||
| backStack = backStack, | ||||||||||
| sceneStrategies = listOf(strategy), | ||||||||||
| entryDecorators = listOf( | ||||||||||
| rememberSaveableStateHolderNavEntryDecorator<NavKey>(), | ||||||||||
| rememberViewModelStoreNavEntryDecorator<NavKey>() | ||||||||||
| ), | ||||||||||
| entryProvider = entryProvider { | ||||||||||
| entry<Screen.Home> { | ||||||||||
| // Any viewModel() requested here will be scoped to this NavEntry | ||||||||||
| val viewModel: HomeViewModel = viewModel() | ||||||||||
| HomeScreen(onNavigateToDetails = {}) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| // [END android_wear_nav3_viewmodel] | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Mock implementations to make it compile | ||||||||||
| @Composable | ||||||||||
| fun HomeScreen(onNavigateToDetails: (String) -> Unit) { | ||||||||||
| // Mock | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Composable | ||||||||||
| fun DetailsScreen(itemId: String, onBack: () -> Unit) { | ||||||||||
| // Mock | ||||||||||
| } | ||||||||||
|
|
||||||||||
| class HomeViewModel : ViewModel() { | ||||||||||
| // Mock | ||||||||||
| } | ||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.