-
Notifications
You must be signed in to change notification settings - Fork 98
feat(ui): implement predictive back gesture and migrate Key.Back to BackHandler #498
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
Merged
ProdigyV21
merged 6 commits into
ProdigyV21:main
from
Himanth-reddy:feat/predictive-back-gesture
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5aa0c1b
feat(motion): add smooth predictive back to SettingsScreen and Stream…
Himanth-reddy d65e5ff
fix: enable android:enableOnBackInvokedCallback=true for system predi…
Himanth-reddy 5df9fb5
fix(motion): keep main Settings page at 100% scale on sub-page entry
Himanth-reddy d50bb61
feat(ui): migrate Key.Back to BackHandler across app for predictive b…
Himanth-reddy dec43ea
Merge current main and complete predictive back migration
3e2d9f8
Merge latest main while preserving Live TV back handling
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
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
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
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
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
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
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
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
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
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
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
130 changes: 130 additions & 0 deletions
130
app/src/main/kotlin/com/arflix/tv/ui/motion/ArvioPredictiveBack.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,130 @@ | ||
| package com.arflix.tv.ui.motion | ||
|
|
||
| import androidx.activity.BackEventCompat | ||
| import androidx.activity.compose.PredictiveBackHandler | ||
| import androidx.compose.animation.core.Animatable | ||
| import androidx.compose.animation.core.CubicBezierEasing | ||
| import androidx.compose.animation.core.Easing | ||
| import androidx.compose.animation.core.tween | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.Stable | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableFloatStateOf | ||
| import androidx.compose.runtime.mutableIntStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.runtime.snapshotFlow | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.graphicsLayer | ||
| import androidx.compose.ui.unit.dp | ||
| import kotlinx.coroutines.launch | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
|
|
||
| /** | ||
| * M3 STANDARD_DECELERATE — PathInterpolator(0f, 0f, 0f, 1f). | ||
| * Deliberately NOT FastOutSlowInEasing, which is (0.4, 0, 0.2, 1) ease-in-out. | ||
| */ | ||
| val ArvioStandardDecelerate: Easing = CubicBezierEasing(0.05f, 0.7f, 0.1f, 1f) | ||
|
|
||
| private const val MinSurfaceScale = 0.90f // M3: surface scales to 90% | ||
| private val EdgeMargin = 8.dp // M3: 8dp from screen edges | ||
|
|
||
| /** Raw gesture state. `progress` is unmodified pointer progress; ease at consumption. */ | ||
| @Stable | ||
| class ArvioBackMotion internal constructor() { | ||
| var progress by mutableFloatStateOf(0f) | ||
| internal set | ||
| var swipeEdge by mutableIntStateOf(BackEventCompat.EDGE_LEFT) | ||
| internal set | ||
| var touchY by mutableFloatStateOf(Float.NaN) | ||
| internal set | ||
| val eased: Float get() = ArvioStandardDecelerate.transform(progress) | ||
| } | ||
|
|
||
| /** | ||
| * PredictiveBackHandler with real settle animations. | ||
| * | ||
| * Commit: animates the remaining travel to 1f *before* invoking [onCommit], so releasing | ||
| * at 80% is never a jump cut. Cancel: animates back to 0f. | ||
| * | ||
| * The settle is launched on [rememberCoroutineScope] because on cancel the handler's own | ||
| * coroutine is already cancelled — suspending in the catch block would throw immediately. | ||
| */ | ||
| @Composable | ||
| fun rememberArvioPredictiveBack( | ||
| enabled: Boolean, | ||
| commitDurationMs: Int = 280, | ||
| cancelDurationMs: Int = 240, | ||
| onCommit: () -> Unit, | ||
| ): ArvioBackMotion { | ||
| val motion = remember { ArvioBackMotion() } | ||
| val anim = remember { Animatable(0f) } | ||
| val scope = rememberCoroutineScope() | ||
| val commit by rememberUpdatedState(onCommit) | ||
|
|
||
| LaunchedEffect(anim) { | ||
| snapshotFlow { anim.value }.collect { motion.progress = it } | ||
| } | ||
|
|
||
| PredictiveBackHandler(enabled = enabled) { events -> | ||
| try { | ||
| events.collect { e -> | ||
| motion.swipeEdge = e.swipeEdge | ||
| motion.touchY = e.touchY | ||
| anim.snapTo(e.progress) | ||
| } | ||
| anim.animateTo(1f, tween(commitDurationMs, easing = ArvioStandardDecelerate)) | ||
| commit() | ||
| } catch (e: CancellationException) { | ||
| scope.launch { | ||
| anim.animateTo(0f, tween(cancelDurationMs, easing = ArvioStandardDecelerate)) | ||
| } | ||
| throw e | ||
| } | ||
| } | ||
| return motion | ||
| } | ||
|
|
||
| /** The leaving surface. Full M3 spec: 90% scale, edge-aware X-shift, touchY-weighted Y-shift. */ | ||
| fun Modifier.arvioBackSurface(motion: ArvioBackMotion): Modifier = graphicsLayer { | ||
| val p = motion.eased | ||
| if (p <= 0f) return@graphicsLayer | ||
|
|
||
| val s = 1f - (1f - MinSurfaceScale) * p | ||
| scaleX = s | ||
| scaleY = s | ||
|
|
||
| val margin = EdgeMargin.toPx() | ||
| val maxX = (size.width / 20f - margin).coerceAtLeast(0f) | ||
| val dir = if (motion.swipeEdge == BackEventCompat.EDGE_RIGHT) -1f else 1f | ||
| translationX = dir * p * maxX | ||
|
|
||
| val maxY = (size.height / 20f - margin).coerceAtLeast(0f) | ||
| val pivot = if (motion.touchY.isNaN()) 0f | ||
| else ((motion.touchY - size.height / 2f) / (size.height / 2f)).coerceIn(-1f, 1f) | ||
| translationY = p * pivot * maxY | ||
| } | ||
|
|
||
| /** The revealed surface underneath. Matches SettingsScreen.kt peeking behavior. */ | ||
| fun Modifier.arvioBackPeek(motion: ArvioBackMotion, active: Boolean): Modifier = graphicsLayer { | ||
| if (!active) return@graphicsLayer | ||
| val p = motion.eased | ||
| if (p <= 0f) return@graphicsLayer | ||
| val s = 0.96f + p * 0.04f | ||
| scaleX = s | ||
| scaleY = s | ||
| alpha = 0.6f + p * 0.4f | ||
| } | ||
|
|
||
| /** Modal dismiss: shrink + fade in place. No positional shift — a modal closes, it doesn't traverse. */ | ||
| fun Modifier.arvioBackModal(motion: ArvioBackMotion): Modifier = graphicsLayer { | ||
| val p = motion.eased | ||
| if (p <= 0f) return@graphicsLayer | ||
| val s = 1f - (1f - MinSurfaceScale) * p | ||
| scaleX = s | ||
| scaleY = s | ||
| alpha = 1f - p * 0.3f | ||
| } |
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
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
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.
With this application-wide flag enabled on API 33+ / targetSdk 36, system Back is delivered through the OnBackInvoked/OnBackPressed path instead of the Compose key-event path, but a repo-wide check still shows screens and modals that only handle
Key.BackinonKeyEvent/onPreviewKeyEventand have noBackHandlerin those files, e.g.app/src/main/kotlin/com/arflix/tv/ui/components/TextInputModal.kt:142andapp/src/main/kotlin/com/arflix/tv/ui/screens/plugin/PluginScreen.kt:130. In those contexts Back will no longer dismiss/navigate as intended and can fall through to the activity/root back behavior, so either keep this flag off or migrate the remainingKey.Backhandlers toBackHandlerbefore enabling it globally.Useful? React with 👍 / 👎.