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,45 +237,37 @@ 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 ->
val handled = handleTvPlayerRootKeyEvent( if (event.key == Key.Back) {
event = event, if (event.type == KeyEventType.KeyDown) {
controlsVisible = controlsVisible, handleBack()
popupVisible = showSkipIntroButton || showNextEpisodeOverlay, }
onShowControls = ::showControls, true
onTogglePlayback = { } else {
// This is a hack to trigger the ValueChangeTimedVisibility to show the hidden resume/stop feedback. val handled = handleTvPlayerRootKeyEvent(
resumeStopFeedbackCounter++ event = event,
viewModel.togglePlayPause() controlsVisible = controlsVisible,
}, popupVisible = showSkipIntroButton || showNextEpisodeOverlay,
onSeekRelative = { onShowControls = ::showControls,
// This is a hack to trigger the ValueChangeTimedVisibility to show the hidden seek timeline. onTogglePlayback = {
hiddenSeekCounter++ resumeStopFeedbackCounter++
viewModel.seekBy(it) viewModel.togglePlayPause()
}, },
) onSeekRelative = {
if (event.type == KeyEventType.KeyDown) { hiddenSeekCounter++
hideControlsWithTimeout() viewModel.seekBy(it)
},
)
if (event.type == KeyEventType.KeyDown) {
hideControlsWithTimeout()
}
handled
} }
handled
} }
.focusable() .focusable()
) { ) {
@@ -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 = {