package tv.tivilux.nativeplayer.ui.components import android.content.ClipData import android.content.ClipboardManager import android.content.Context import android.content.Intent import androidx.compose.foundation.background import androidx.compose.foundation.focusable import androidx.compose.ui.focus.onFocusChanged import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.platform.LocalContext 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 tv.tivilux.nativeplayer.BuildConfig import tv.tivilux.nativeplayer.data.LiveStreamTrace import tv.tivilux.nativeplayer.ui.theme.* @Composable fun StreamDiagnosticsDialog(onDismiss: () -> Unit) { val context = LocalContext.current var snapshot by remember { mutableStateOf(LiveStreamTrace.snapshot()) } var recording by remember { mutableStateOf(LiveStreamTrace.isRecording()) } var message by remember { mutableStateOf(null) } val firstFocus = remember { FocusRequester() } fun refresh() { snapshot = LiveStreamTrace.snapshot() recording = LiveStreamTrace.isRecording() } fun report() = "TiviLux ${BuildConfig.VERSION_NAME} / ${BuildConfig.VERSION_CODE}\n$snapshot" Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) { Column(Modifier.fillMaxWidth(.94f).fillMaxHeight(.94f).background(LuxPanel).padding(18.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { Text("Stream Diagnostics", color = LuxGold, fontSize = 22.sp) Text("Start → play A&E → switch to Catchy Comedy → wait 60 seconds → return here. Avoid HOME; it closes the app and clears this log.", color = LuxText, fontSize = 12.sp) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { LuxButton(if (recording) "STOP CAPTURE" else "START NEW CAPTURE", { if (recording) LiveStreamTrace.stop() else LiveStreamTrace.start() refresh() message = if (recording) "Recording. Press CLOSE and return to Live TV." else "Stopped. You can copy, share or photograph the results." }, Modifier.weight(1f).focusRequester(firstFocus)) LuxButton("REFRESH", { refresh() }, Modifier.weight(1f)) LuxButton("CLOSE", onDismiss, Modifier.weight(1f)) } Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { LuxButton("COPY LOG", { val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager clipboard?.setPrimaryClip(ClipData.newPlainText("TiviLux Stream Diagnostics", report())) message = if (clipboard != null) "Copied to this device's clipboard." else "Clipboard unavailable; photograph the results." }, Modifier.weight(1f)) LuxButton("SHARE LOG", { val send = Intent(Intent.ACTION_SEND).apply { type = "text/plain" putExtra(Intent.EXTRA_SUBJECT, "TiviLux stream diagnostic") putExtra(Intent.EXTRA_TEXT, report()) } message = runCatching { context.startActivity(Intent.createChooser(send, "Share diagnostic log")) "Share opened." }.getOrElse { "No sharing app available. Photograph the summary or copy the log." } }, Modifier.weight(1f)) LuxButton("CLEAR LOG", { LiveStreamTrace.clear(); refresh(); message = "Log cleared." }, Modifier.weight(1f)) } message?.let { Text(it, color = LuxGold, fontSize = 12.sp) } LazyColumn(Modifier.weight(1f).fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(3.dp)) { itemsIndexed(snapshot.lines()) { _, line -> // Focusable rows let TV remotes scroll through the whole report. androidx.compose.foundation.layout.Box(Modifier.fillMaxWidth().then(Modifier.diagnosticRowFocus())) { Text(line.ifEmpty { " " }, color = LuxText, fontSize = 12.sp) } } } } LaunchedEffect(Unit) { firstFocus.requestFocus() } } } @Composable private fun Modifier.diagnosticRowFocus(): Modifier { var focused by remember { mutableStateOf(false) } return this .background(if (focused) LuxLine else androidx.compose.ui.graphics.Color.Transparent) .onFocusChanged { focused = it.isFocused } .focusable() }