-
Notifications
You must be signed in to change notification settings - Fork 277
add in predictive back snippets for NavHost and predictive back handler #388
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
81b4e8b
add in predictive back snippets for NavHost and predictive back handler
trambui09 ba6aaa4
add in expression
trambui09 63a7c17
fix build errors
trambui09 fbf7c6f
fix build error
trambui09 370231a
rename composables to be more descriptive and make it private
trambui09 bb1bd82
Merge branch 'main' into predictiveback
trambui09 deed718
fix build errors
trambui09 1527d9a
fix build errors
trambui09 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
152 changes: 152 additions & 0 deletions
152
...ppets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.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,152 @@ | ||
package com.example.compose.snippets.predictiveback | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.compose.animation.scaleOut | ||
import androidx.compose.ui.graphics.TransformOrigin | ||
import androidx.compose.animation.EnterTransition | ||
import android.os.SystemClock | ||
import androidx.activity.compose.PredictiveBackHandler | ||
import androidx.navigation.compose.NavHost | ||
import androidx.compose.animation.core.Animatable | ||
import androidx.navigation.compose.composable | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.icons.filled.Home | ||
import androidx.compose.material3.Surface | ||
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.geometry.Offset | ||
import androidx.compose.ui.input.pointer.util.VelocityTracker | ||
import androidx.compose.ui.platform.LocalDensity | ||
import kotlin.coroutines.cancellation.CancellationException | ||
import kotlinx.coroutines.launch | ||
|
||
|
||
@Composable | ||
fun MainNavigation( | ||
modifier: Modifier, | ||
) { | ||
val navController = rememberNavController() | ||
|
||
// [START android_compose_predictiveback_navhost] | ||
NavHost( | ||
navController = navController, | ||
startDestination = "home", | ||
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") { | ||
HomeScreen( | ||
modifier = modifier, | ||
navController = navController, | ||
) | ||
} | ||
composable("settings") { | ||
SettingsScreen( | ||
modifier = modifier, | ||
navController = navController, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun HomeScreen( | ||
modifier: Modifier = Modifier, navController: NavHostController | ||
) { | ||
|
||
} | ||
|
||
@Composable | ||
fun SettingsScreen( | ||
modifier: Modifier = Modifier, navController: NavHostController | ||
) { | ||
|
||
} | ||
|
||
@Composable | ||
fun HomeScreenDrawer() { | ||
trambui09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Surface( | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
var drawerState by remember { | ||
mutableStateOf(DrawerState.Closed) | ||
} | ||
var screenState by remember { | ||
mutableStateOf(Screen.Home) | ||
} | ||
|
||
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 | ||
} | ||
fun toggleDrawerState() { | ||
coroutineScope.launch { | ||
if (drawerState == DrawerState.Open) { | ||
closeDrawer() | ||
} else { | ||
openDrawer() | ||
} | ||
} | ||
} | ||
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 | ||
} | ||
|
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.
You can make these private and possible name it PredictiveBackOverrideExit?
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.
Yeah!