package com.cottondb.android.ui.media.video import android.view.SurfaceView import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.semantics import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.unit.dp import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.compose.LocalLifecycleOwner import com.cottondb.android.design.CottonTextStyles import com.cottondb.android.generated.CottonPalette import com.cottondb.android.media.LocalMediaFile /** Creates and releases a route-local player; no player outlives this pane's composition. */ @Composable private fun rememberAndroidVideoPlayerController(): AndroidVideoPlayerController { val context = LocalContext.current val controller = remember(context) { AndroidVideoPlayerController(context) } DisposableEffect(controller) { onDispose { controller.dispose() } } return controller } /** * A local-file-only film pane. It owns its route-local controller and releases it on disposal. * Its caller has already resolved or deliberately fetched the file; it has no click-to-fetch behavior. */ @Composable fun VideoPlayerPane( file: LocalMediaFile, modifier: Modifier = Modifier, ) { val controller = rememberAndroidVideoPlayerController() val lifecycle = LocalLifecycleOwner.current.lifecycle val state by controller.state.collectAsState() LaunchedEffect(controller, file) { controller.bind(file) } DisposableEffect(controller, lifecycle) { val observer = LifecycleEventObserver { _, event -> if (event == Lifecycle.Event.ON_STOP || event == Lifecycle.Event.ON_DESTROY) { controller.onAppBackgrounded() } } lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } Box(modifier = modifier, contentAlignment = Alignment.Center) { AndroidView( factory = { context -> SurfaceView(context).also { surface -> controller.attach(surface.holder) } }, update = {}, onRelease = { surface -> controller.detach(surface.holder) }, modifier = Modifier.fillMaxSize().semantics { contentDescription = "Video player" }, ) VideoPlaybackOverlay(state, controller) { controller.bind(file) } } } @Composable private fun VideoPlaybackOverlay(state: VideoPlaybackState, controller: AndroidVideoPlayerController, retry: () -> Unit) { val label = when (state) { VideoPlaybackState.Idle -> null VideoPlaybackState.Preparing -> "Opening video…" is VideoPlaybackState.Paused -> "Play video" is VideoPlaybackState.Playing -> "Pause video" is VideoPlaybackState.Completed -> "Play video again" is VideoPlaybackState.Failed -> state.message } if (label != null) { val playable = state is VideoPlaybackState.Paused || state is VideoPlaybackState.Playing || state is VideoPlaybackState.Completed if (playable) { TextButton( onClick = controller::toggle, modifier = Modifier.background(CottonPalette.ground, RoundedCornerShape(24.dp)) .semantics { contentDescription = label }, ) { Text(label, style = CottonTextStyles.caption, color = CottonPalette.ink) } } else androidx.compose.foundation.layout.Column( modifier = Modifier.background(CottonPalette.ground, RoundedCornerShape(12.dp)).padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Text( text = label, modifier = Modifier.semantics { contentDescription = label }, style = CottonTextStyles.caption, color = CottonPalette.ink, ) if (state is VideoPlaybackState.Failed) TextButton(onClick = retry) { Text("Try again") } } } }