-
Notifications
You must be signed in to change notification settings - Fork 180
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
add in predictive back snippets for NavHost and predictive back handler #394
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright 2024 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.compose.snippets.predictiveback | ||
|
||
import android.os.SystemClock | ||
import androidx.activity.compose.PredictiveBackHandler | ||
import androidx.compose.animation.EnterTransition | ||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.scaleOut | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.TransformOrigin | ||
import androidx.compose.ui.input.pointer.util.VelocityTracker | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import androidx.navigation.compose.composable | ||
import androidx.navigation.compose.rememberNavController | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
@Composable | ||
private fun PredictiveBackOverrideExit( | ||
modifier: Modifier, | ||
) { | ||
val navController = rememberNavController() | ||
|
||
// [START android_compose_predictiveback_navhost] | ||
NavHost( | ||
navController = navController, | ||
startDestination = "home", | ||
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. This should use type safe routes. To do this declare:
before the function. Then to use it do 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. Ahhh, thanks for the tip of changing them to safe routes, Don! |
||
popExitTransition = { | ||
scaleOut( | ||
targetScale = 0.9f, | ||
transformOrigin = TransformOrigin(pivotFractionX = 0.5f, pivotFractionY = 0.5f) | ||
) | ||
}, | ||
popEnterTransition = { | ||
EnterTransition.None | ||
}, | ||
modifier = modifier, | ||
) | ||
// [END android_compose_predictiveback_navhost] | ||
{ | ||
composable("home") { | ||
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.
|
||
HomeScreen( | ||
modifier = modifier, | ||
navController = navController, | ||
) | ||
} | ||
composable("settings") { | ||
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.
|
||
SettingsScreen( | ||
modifier = modifier, | ||
navController = navController, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun HomeScreen( | ||
modifier: Modifier = Modifier, | ||
navController: NavHostController | ||
) { | ||
} | ||
|
||
@Composable | ||
private fun SettingsScreen( | ||
modifier: Modifier = Modifier, | ||
navController: NavHostController | ||
) { | ||
} | ||
|
||
@Composable | ||
private fun PredictiveBackHandlerManualProgress() { | ||
|
||
Surface( | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
var drawerState by remember { | ||
mutableStateOf(DrawerState.Closed) | ||
} | ||
|
||
val translationX = remember { | ||
Animatable(0f) | ||
} | ||
|
||
val drawerWidth = with(LocalDensity.current) { | ||
DrawerWidth.toPx() | ||
} | ||
translationX.updateBounds(0f, drawerWidth) | ||
|
||
val coroutineScope = rememberCoroutineScope() | ||
|
||
suspend fun closeDrawer(velocity: Float = 0f) { | ||
translationX.animateTo(targetValue = 0f, initialVelocity = velocity) | ||
drawerState = DrawerState.Closed | ||
} | ||
suspend fun openDrawer(velocity: Float = 0f) { | ||
translationX.animateTo(targetValue = drawerWidth, initialVelocity = velocity) | ||
drawerState = DrawerState.Open | ||
} | ||
|
||
val velocityTracker = remember { | ||
VelocityTracker() | ||
} | ||
|
||
// [START android_compose_predictivebackhandler] | ||
PredictiveBackHandler(drawerState == DrawerState.Open) { progress -> | ||
try { | ||
progress.collect { backEvent -> | ||
val targetSize = (drawerWidth - (drawerWidth * backEvent.progress)) | ||
translationX.snapTo(targetSize) | ||
velocityTracker.addPosition( | ||
SystemClock.uptimeMillis(), | ||
Offset(backEvent.touchX, backEvent.touchY) | ||
) | ||
} | ||
closeDrawer(velocityTracker.calculateVelocity().x) | ||
} catch (e: CancellationException) { | ||
openDrawer(velocityTracker.calculateVelocity().x) | ||
} | ||
velocityTracker.resetTracking() | ||
} | ||
// [END android_compose_predictivebackhandler] | ||
} | ||
} | ||
|
||
private enum class DrawerState { | ||
Open, | ||
Closed | ||
} | ||
|
||
private val DrawerWidth = 300.dp |
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.
Not sure of the context in which this is being used but I think it's important to mention that
NavHost
supports predictive back out of the box. There's nothing extra required, aside from defining the animations you want to use between destinations.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.
Yes, we do mention NavHost supports PB out of the box. This snippets shows a custom in-app animations example for PB