-
Notifications
You must be signed in to change notification settings - Fork 346
NavEventSnippets as per the Doc #800
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
Open
praneethatchana
wants to merge
10
commits into
main
Choose a base branch
from
NavEventSnippets
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.
+398
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9fdaf7
NavEventSnippets as per the Doc
praneethatchana 3475882
Apply Spotless
praneethatchana 393a1ee
NavEventSnippets Added the New class for the Animation.
praneethatchana 4d988df
Apply Spotless
praneethatchana e3cdedb
Resolved the GIT comments
praneethatchana dfb6511
Resolved the TODO comment in the Nav3Snippets
praneethatchana 5dbef5a
Resolved the GIT comment in the Nav3Snippets
praneethatchana 73e9bcf
There are two additional improvements made to the snippet.
praneethatchana e9b97c8
There are two additional improvements made to the snippet.
praneethatchana 2f7f315
There are the additional improvements made to the snippet as per the …
praneethatchana 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
133 changes: 133 additions & 0 deletions
133
compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/Nav3Snippets.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,133 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| @file:Suppress("unused", "UNUSED_VARIABLE") | ||
|
|
||
| package com.example.compose.snippets.predictiveback | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import android.view.MotionEvent.EDGE_LEFT | ||
| import android.view.MotionEvent.EDGE_RIGHT | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.animation.core.animateFloatAsState | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.offset | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.scale | ||
| import androidx.compose.ui.platform.LocalConfiguration | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.navigationevent.NavigationEventInfo | ||
| import androidx.navigationevent.NavigationEventTransitionState | ||
| import androidx.navigationevent.compose.NavigationEventState | ||
| import androidx.navigationevent.compose.rememberNavigationEventState | ||
|
|
||
|
|
||
| // [START android_compose_predictiveback_navevent_animation] | ||
| object Routes { | ||
| const val SCREEN_A = "Screen A" | ||
| const val SCREEN_B = "Screen B" | ||
| } | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| setContent { | ||
| var state by remember { mutableStateOf(Routes.SCREEN_A) } | ||
| val backEventState = rememberNavigationEventState<NavigationEventInfo>(currentInfo = NavigationEventInfo.None) | ||
|
|
||
| when (state) { | ||
| Routes.SCREEN_A -> { | ||
| ScreenA { state = Routes.SCREEN_B } | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| else -> { | ||
| if (backEventState.transitionState is NavigationEventTransitionState.InProgress) { | ||
| ScreenA { } | ||
| } | ||
| ScreenB( | ||
| backEventState = backEventState, | ||
| onBackCompleted = { state = Routes.SCREEN_A } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @SuppressLint("ConfigurationScreenWidthHeight") | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Composable | ||
| fun ScreenB( | ||
| backEventState: NavigationEventState<NavigationEventInfo>, | ||
| onBackCompleted: () -> Unit = {}, | ||
| ) { | ||
| val transitionState = backEventState.transitionState | ||
| val latestEvent = | ||
| (transitionState as? NavigationEventTransitionState.InProgress) | ||
| ?.latestEvent | ||
| val backProgress = latestEvent?.progress ?: 0f | ||
| val swipeEdge = latestEvent?.swipeEdge ?: 0 | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (transitionState is NavigationEventTransitionState.InProgress) { | ||
| Log.d("BackGesture", "Progress: ${transitionState.latestEvent.progress}") | ||
| } else if (transitionState is NavigationEventTransitionState.Idle) { | ||
| Log.d("BackGesture", "Idle") | ||
| } | ||
|
|
||
| val animatedScale by animateFloatAsState( | ||
| targetValue = 1f - (backProgress * 0.1f), | ||
| label = "ScaleAnimation" | ||
| ) | ||
|
|
||
| val configuration = LocalConfiguration.current | ||
| val maxShift = remember(configuration) { | ||
| (configuration.screenWidthDp / 20f) - 8 | ||
| } | ||
|
|
||
| val offsetX = when (swipeEdge) { | ||
| EDGE_LEFT -> (backProgress * maxShift).dp | ||
| EDGE_RIGHT -> (-backProgress * maxShift).dp | ||
| else -> 0.dp | ||
| } | ||
|
|
||
| NavigationBackHandler( | ||
| state = backEventState, | ||
| onBackCompleted = onBackCompleted, | ||
| isBackEnabled = true | ||
| ) | ||
| Box( | ||
| modifier = Modifier | ||
| .offset(x = offsetX) | ||
| .scale(animatedScale) | ||
| ){ | ||
| // Rest of UI | ||
| } | ||
| } | ||
| // [END android_compose_predictiveback_navevent_animation] | ||
|
|
||
| @Composable | ||
| fun ScreenA(onNavigate: () -> Unit) { | ||
| // Basic ScreenA implementation for snippet | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused by the naming of this file since there's nothing in here that's actually using Navigation 3. Would it make sense to move these snippets into
NavEventSnippets.ktas well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I have moved these Snippets into NavEventSnippets.kt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you delete this file as well? It looks like the build is failing due to the duplication between the two files https://github.com/android/snippets/actions/runs/23192468917/job/67391917984?pr=800