feat(player): refactor back navigation handling in TvPlayerScreen

This commit is contained in:
2026-07-13 20:27:05 +02:00
parent 79066b2ce5
commit ced9ca7b9d

View File

@@ -2,7 +2,6 @@ package hu.bbara.purefin.ui.screen.player
import android.app.Activity import android.app.Activity
import android.view.WindowManager import android.view.WindowManager
import androidx.activity.compose.BackHandler
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeIn
@@ -183,6 +182,14 @@ fun TvPlayerScreen(
trackPanelType = null trackPanelType = null
} }
} }
fun handleBack() {
when {
trackPanelType != null -> closeTrackPanel()
isPlaylistExpanded -> closePlaylist()
controlsVisible -> hideControls()
else -> onBack()
}
}
val showSkipIntroButton = !controlsVisible val showSkipIntroButton = !controlsVisible
@@ -230,37 +237,28 @@ fun TvPlayerScreen(
SubtitleView.DEFAULT_BOTTOM_PADDING_FRACTION SubtitleView.DEFAULT_BOTTOM_PADDING_FRACTION
} }
BackHandler(enabled = true) {
when {
trackPanelType != null -> closeTrackPanel()
isPlaylistExpanded -> {
closePlaylist()
}
controlsVisible -> {
hideControls()
}
else -> onBack()
}
}
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize()
.background(Color.Black) .background(Color.Black)
.focusRequester(rootFocusRequester) .focusRequester(rootFocusRequester)
.onKeyEvent { event -> .onKeyEvent { event ->
if (event.key == Key.Back) {
if (event.type == KeyEventType.KeyDown) {
handleBack()
}
true
} else {
val handled = handleTvPlayerRootKeyEvent( val handled = handleTvPlayerRootKeyEvent(
event = event, event = event,
controlsVisible = controlsVisible, controlsVisible = controlsVisible,
popupVisible = showSkipIntroButton || showNextEpisodeOverlay, popupVisible = showSkipIntroButton || showNextEpisodeOverlay,
onShowControls = ::showControls, onShowControls = ::showControls,
onTogglePlayback = { onTogglePlayback = {
// This is a hack to trigger the ValueChangeTimedVisibility to show the hidden resume/stop feedback.
resumeStopFeedbackCounter++ resumeStopFeedbackCounter++
viewModel.togglePlayPause() viewModel.togglePlayPause()
}, },
onSeekRelative = { onSeekRelative = {
// This is a hack to trigger the ValueChangeTimedVisibility to show the hidden seek timeline.
hiddenSeekCounter++ hiddenSeekCounter++
viewModel.seekBy(it) viewModel.seekBy(it)
}, },
@@ -270,6 +268,7 @@ fun TvPlayerScreen(
} }
handled handled
} }
}
.focusable() .focusable()
) { ) {
AndroidView( AndroidView(
@@ -279,6 +278,8 @@ fun TvPlayerScreen(
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FIT
player = viewModel.player player = viewModel.player
subtitleView?.setBottomPaddingFraction(subtitleBottomPaddingFraction) subtitleView?.setBottomPaddingFraction(subtitleBottomPaddingFraction)
isFocusable = false
isFocusableInTouchMode = false
} }
}, },
update = { update = {