Skip to content

Commit

Permalink
Add comments and fix custom seeking predictive back example.
Browse files Browse the repository at this point in the history
  • Loading branch information
riggaroo committed Dec 10, 2024
1 parent 3432b15 commit d7ba5e8
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.example.compose.snippets.animations.sharedelement

import android.util.Log
import androidx.activity.compose.PredictiveBackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -653,19 +654,24 @@ fun CustomPredictiveBackHandle() {

PredictiveBackHandler(seekableTransitionState.currentState is Screen.Details) { progress ->
try {
// Whilst a back gesture is in progress, backEvents will be fired for each progress
// update.
progress.collect { backEvent ->
// code for progress
// For each backEvent that comes in, we manually seekTo the reported back progress
try {
seekableTransitionState.seekTo(backEvent.progress, targetState = Screen.Home)
} catch (e: CancellationException) {
// ignore the cancellation
// seekTo may be cancelled as expected, if animateTo or subsequent seekTo calls
// before the current seekTo finishes, in this case, we ignore the cancellation.
}
}
// code for completion
// Once collection has completed, we are either fully in the target state, or need
// to progress towards the end.
seekableTransitionState.animateTo(seekableTransitionState.targetState)
} catch (e: CancellationException) {
// code for cancellation
seekableTransitionState.animateTo(seekableTransitionState.currentState)
// When the predictive back gesture is cancelled, we snap to the end state to ensure
// it completes its seeking animation back to the currentState
seekableTransitionState.snapTo(seekableTransitionState.currentState)
}
}
val coroutineScope = rememberCoroutineScope()
Expand Down

0 comments on commit d7ba5e8

Please sign in to comment.