Skip to content
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

🤖 Sync main to latest #417

Merged
merged 31 commits into from
Dec 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
31b4de2
Added two snippets for showcasing how to do Masking and Clipping in C…
riggaroo Oct 2, 2024
d1e297b
🤖 Update Dependencies (#366)
compose-devrel-github-bot Oct 3, 2024
0c9024b
Add Material Carousel (#363)
riggaroo Oct 3, 2024
68fef6e
Basic menu examples (#371)
jakeroseman Oct 11, 2024
fbfd07d
Filter chip dropdown menu (#375)
JolandaVerhoef Oct 11, 2024
f67b849
Add example of date picker textfield opening picker dialog on click (…
JolandaVerhoef Oct 11, 2024
a0b94c0
Add auto advance pager snippets (#377)
jakeroseman Oct 11, 2024
591dfa5
Tooltip component examples (#373)
jakeroseman Oct 11, 2024
4baf477
Add pull to refresh snippets (#378)
JolandaVerhoef Oct 11, 2024
5545f37
Remove LaunchedEffect workaround (#379)
jakeroseman Oct 14, 2024
90b8500
Navigation drawer examples (#380)
jakeroseman Oct 14, 2024
e9116f5
Snippets for keyboard input (#368)
chikoski Oct 16, 2024
2af26d9
Snippet for animated sorted list with add/remove buttons. (#381)
jakeroseman Oct 16, 2024
514653c
Add basic segmented button examples (#383)
jakeroseman Oct 24, 2024
5ccd4a8
Adding views snippets module with Generated Preview samples (#384)
secondsun Oct 28, 2024
195bd61
Adding top bar multi selection examples (#387)
jakeroseman Oct 29, 2024
d081491
Include AppScaffold in navigation Snippet (#385)
kul3r4 Oct 31, 2024
81c1a2a
Animate image size on scroll (#390)
jakeroseman Nov 5, 2024
1da22a0
Add basic HTML text styling example (#389)
jakeroseman Nov 5, 2024
f595a0d
🤖 Update Dependencies (#391)
compose-devrel-github-bot Nov 6, 2024
f4e4987
add in predictive back snippets for NavHost and predictive back handl…
trambui09 Nov 21, 2024
515b08e
Add region tags to auto advance pager examples (#398)
jakeroseman Nov 27, 2024
fb654a9
Init Wear Tiles snippets (#400)
ithinkihaveacat Nov 27, 2024
c699e70
Create misc module, add broadcast receiver snippets (#397)
JolandaVerhoef Nov 28, 2024
6f61cb5
Add live search with FilterTextView example (#402)
jakeroseman Dec 4, 2024
0d07b08
Add single option radio button example (#401)
jakeroseman Dec 4, 2024
c79a414
update to use type safe routes (#399)
trambui09 Dec 6, 2024
273d399
Migrated relevant MotionCompose snippets into snippets (#411)
riggaroo Dec 9, 2024
82573fd
Moving location permissions snippets to repo (#413)
jakeroseman Dec 9, 2024
6dc5a0a
Use VerticalDivider in IntrinsicSnippets (#369)
DavidMendozaMartinez Dec 10, 2024
eaeca60
Merge remote-tracking branch 'origin/main' into latest
compose-devrel-github-bot Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add in predictive back snippets for NavHost and predictive back handl…
…er (#394)

* add in predictive back snippets

* Apply Spotless

* add in basic predictivebackhandler composable example

* Apply Spotless

---------

Co-authored-by: trambui09 <trambui09@users.noreply.github.com>
  • Loading branch information
trambui09 and trambui09 authored Nov 21, 2024
commit f4e49876ac3fad90376323fa23b6991581d547c7
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* 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.BackEventCompat
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.background
import androidx.compose.foundation.layout.Box
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.mutableFloatStateOf
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.Color
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
import kotlinx.coroutines.flow.Flow

@Composable
private fun PredictiveBackOverrideExit(
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
private fun HomeScreen(
modifier: Modifier = Modifier,
navController: NavHostController
) {
}

@Composable
private fun SettingsScreen(
modifier: Modifier = Modifier,
navController: NavHostController
) {
}

@Composable
private fun PredictiveBackHandlerBasicExample() {

var boxScale by remember { mutableFloatStateOf(1F) }

Box(
modifier = Modifier
.fillMaxSize(boxScale)
.background(Color.Blue)
)

// [START android_compose_predictivebackhandler_basic]
PredictiveBackHandler(true) { progress: Flow<BackEventCompat> ->
// code for gesture back started
try {
progress.collect { backEvent ->
// code for progress
boxScale = 1F - (1F * backEvent.progress)
}
// code for completion
} catch (e: CancellationException) {
// code for cancellation
boxScale = 1F
}
}
// [END android_compose_predictivebackhandler_basic]
}

@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_manualprogress]
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_manualprogress]
}
}

private enum class DrawerState {
Open,
Closed
}

private val DrawerWidth = 300.dp