package tv.tivilux.nativeplayer.ui.components import androidx.activity.compose.BackHandler import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.focusGroup import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusProperties import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleEventObserver import kotlinx.coroutines.delay import tv.tivilux.nativeplayer.data.AppUpdateInfo import tv.tivilux.nativeplayer.ui.theme.LuxGold import tv.tivilux.nativeplayer.ui.theme.LuxLine import tv.tivilux.nativeplayer.ui.theme.LuxMuted import tv.tivilux.nativeplayer.ui.theme.LuxPanel import tv.tivilux.nativeplayer.ui.theme.LuxText /** * True modal TV update dialog. * * 0.4.1 intentionally renders this in a Compose Dialog instead of as a Box layered * over the Home screen. A normal overlay still shares the same focus tree with the * screen behind it, so Android TV's spatial focus search could continue moving Home * tiles even though the update card was visible. A Dialog owns its own focus window, * which keeps D-pad navigation completely inside UPDATE NOW / LATER. */ @Composable fun UpdateOverlay( update: AppUpdateInfo, downloading: Boolean, progress: Int, status: String?, error: String?, onUpdate: () -> Unit, onLater: () -> Unit, onResume: () -> Unit, ) { val updateFocus = remember { FocusRequester() } val laterFocus = remember { FocusRequester() } val lifecycleOwner = LocalContext.current as? LifecycleOwner Dialog( onDismissRequest = { if (!update.required && !downloading) onLater() }, properties = DialogProperties( dismissOnBackPress = false, dismissOnClickOutside = false, usePlatformDefaultWidth = false, ), ) { val focusManager = LocalFocusManager.current // Re-capture focus whenever a new update card appears or the download returns // from Android's install-permission screen. Clearing first is important on TV: // it prevents a previously focused Home tile from remaining the active target. LaunchedEffect(update.versionCode, downloading, error) { if (!downloading) { delay(140) focusManager.clearFocus(force = true) runCatching { updateFocus.requestFocus() } } } DisposableEffect(lifecycleOwner) { if (lifecycleOwner != null) { val observer = LifecycleEventObserver { _, event -> if (event == Lifecycle.Event.ON_RESUME) onResume() } lifecycleOwner.lifecycle.addObserver(observer) onDispose { lifecycleOwner.lifecycle.removeObserver(observer) } } else { onDispose { } } } BackHandler { if (!update.required && !downloading) onLater() } Box( Modifier .fillMaxSize() .background(Color.Black.copy(alpha = .82f)), contentAlignment = Alignment.Center, ) { Column( Modifier .widthIn(min = 560.dp, max = 720.dp) .focusGroup() .background(Color(0xFF111820), RoundedCornerShape(22.dp)) .border(1.dp, LuxLine, RoundedCornerShape(22.dp)) .padding(horizontal = 30.dp, vertical = 26.dp), verticalArrangement = Arrangement.spacedBy(12.dp), ) { Text( if (update.required) "TIVILUX UPDATE REQUIRED" else "TIVILUX UPDATE AVAILABLE", color = LuxGold, fontSize = 11.sp, fontWeight = FontWeight.ExtraBold, letterSpacing = 1.6.sp, ) Text( "Version ${update.versionName}", color = LuxText, fontSize = 30.sp, fontWeight = FontWeight.SemiBold, ) Text( if (update.required) "This build must be updated before continuing." else "Install the newest TiviLux build without deleting the app or signing in again.", color = LuxMuted, fontSize = 12.sp, ) if (update.notes.isNotEmpty()) { Column( Modifier .fillMaxWidth() .background(LuxPanel, RoundedCornerShape(14.dp)) .border(1.dp, LuxLine, RoundedCornerShape(14.dp)) .padding(14.dp), verticalArrangement = Arrangement.spacedBy(5.dp), ) { update.notes.take(6).forEach { note -> Text("• $note", color = LuxText, fontSize = 11.sp) } } } if (downloading) { Column(verticalArrangement = Arrangement.spacedBy(6.dp)) { LinearProgressIndicator( progress = { progress.coerceIn(0, 100) / 100f }, modifier = Modifier.fillMaxWidth().height(5.dp), color = LuxGold, trackColor = Color.White.copy(alpha = .12f), ) Text(status ?: "Downloading update…", color = LuxMuted, fontSize = 10.sp) Text("$progress%", color = LuxGold, fontSize = 11.sp, fontWeight = FontWeight.Bold) } } else { status?.let { Text(it, color = LuxMuted, fontSize = 10.sp) } } error?.let { Text(it, color = Color(0xFFFFA4AC), fontSize = 10.sp, fontWeight = FontWeight.SemiBold) } Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) { LuxButton( text = if (downloading) "DOWNLOADING…" else "UPDATE NOW", onClick = onUpdate, modifier = Modifier .width(190.dp) .focusRequester(updateFocus) .focusProperties { if (!update.required) right = laterFocus }, primary = true, enabled = !downloading, ) if (!update.required) { LuxButton( text = "LATER", onClick = onLater, modifier = Modifier .width(140.dp) .focusRequester(laterFocus) .focusProperties { left = updateFocus }, enabled = !downloading, ) } } Text( "D-PAD to choose • OK to select • Your TiviLux data stays in place", color = LuxMuted.copy(alpha = .75f), fontSize = 9.sp, ) } } } }