Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

fix(downloads): show which dialog button the remote is on

Rick75e15425faec
diff --git a/feature/system/src/androidTest/java/com/streamvault/feature/system/DownloadsRemoteNavigationTest.kt b/feature/system/src/androidTest/java/com/streamvault/feature/system/DownloadsRemoteNavigationTest.kt
index 44550c83..24d6ced5 100644
--- a/feature/system/src/androidTest/java/com/streamvault/feature/system/DownloadsRemoteNavigationTest.kt
+++ b/feature/system/src/androidTest/java/com/streamvault/feature/system/DownloadsRemoteNavigationTest.kt
@@ -2,13 +2,20 @@ package com.streamvault.feature.system
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsFocused
+import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.hasClickAction
+import androidx.compose.ui.test.hasAnyAncestor
import androidx.compose.ui.test.hasText
+import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performKeyInput
@@ -65,6 +72,101 @@ class DownloadsRemoteNavigationTest {
assertThat(deleted).isEmpty()
}
+ @Test
+ fun focusedDeleteDialogButtonLooksDifferentFromUnfocused() {
+ showDownloadsWithDialog(completedDownload())
+ openDeleteDialog()
+
+ val confirm = composeRule.onNode(hasText("Delete") and hasClickAction() and hasAnyAncestor(isDialog()))
+ val unfocused = confirm.captureToImage().asAndroidBitmap()
+ press(Key.DirectionRight)
+ confirm.assertIsFocused()
+ val focused = confirm.captureToImage().asAndroidBitmap()
+
+ val share = clearlyDifferentPixelShare(unfocused, focused)
+ android.util.Log.i("PROBE", "clearly different pixel share = $share")
+ assertThat(share).isGreaterThan(0.05)
+ }
+
+ @Test
+ fun cancellingDeleteReturnsFocusToTheDeleteButton() {
+ showDownloadsWithDialog(completedDownload())
+ openDeleteDialog()
+
+ press(Key.Enter) // focus starts on Cancel
+ composeRule.waitUntil(2_000) { composeRule.onAllNodes(isDialog()).fetchSemanticsNodes().isEmpty() }
+
+ composeRule.onNode(hasText("Delete") and hasClickAction()).assertIsFocused()
+ }
+
+ @Test
+ fun confirmingDeleteKeepsFocusOnTheDownloadsScreen() {
+ showDownloadsWithDialog(completedDownload())
+ openDeleteDialog()
+
+ press(Key.DirectionRight)
+ press(Key.Enter)
+ composeRule.waitUntil(2_000) { downloads.isEmpty() }
+ composeRule.waitForIdle()
+
+ composeRule.onNode(hasText("Change download folder") and hasClickAction()).assertIsFocused()
+ }
+
+ private var downloads by mutableStateOf(emptyList<DownloadItem>())
+ private var confirmItem by mutableStateOf<DownloadItem?>(null)
+
+ /** Like the screen with its view model: Delete opens the dialog, confirming removes the download. */
+ private fun showDownloadsWithDialog(vararg items: DownloadItem) {
+ downloads = items.toList()
+ composeRule.setContent {
+ StreamVaultTheme {
+ MaterialTheme {
+ DownloadsContent(
+ uiState = DownloadsUiState(isLoading = false, downloads = downloads, deleteConfirmItem = confirmItem),
+ scaffold = scaffoldWithMenu,
+ onChangeFolder = {},
+ onOpen = { opened += it },
+ onResume = {},
+ onDelete = { confirmItem = it },
+ onConfirmDelete = {
+ val item = confirmItem
+ confirmItem = null
+ downloads = downloads - listOfNotNull(item).toSet()
+ },
+ onDismissDelete = { confirmItem = null },
+ )
+ }
+ }
+ }
+ }
+
+ private fun openDeleteDialog() {
+ focus("Delete")
+ press(Key.Enter)
+ composeRule.waitUntil(2_000) { composeRule.onAllNodes(isDialog()).fetchSemanticsNodes().isNotEmpty() }
+ // The dialog ignores presses for a moment after opening, so the OK that opened it can't confirm it.
+ composeRule.mainClock.advanceTimeBy(1_000)
+ composeRule.waitForIdle()
+ }
+
+ /** Share of pixels whose colour changed visibly (some channel by more than 40 of 255), not just a faint tint. */
+ private fun clearlyDifferentPixelShare(a: android.graphics.Bitmap, b: android.graphics.Bitmap): Double {
+ val w = minOf(a.width, b.width)
+ val h = minOf(a.height, b.height)
+ var different = 0
+ for (x in 0 until w) for (y in 0 until h) {
+ val p = a.getPixel(x, y)
+ val q = b.getPixel(x, y)
+ val delta = maxOf(
+ kotlin.math.abs((p shr 16 and 0xFF) - (q shr 16 and 0xFF)),
+ kotlin.math.abs((p shr 8 and 0xFF) - (q shr 8 and 0xFF)),
+ kotlin.math.abs((p and 0xFF) - (q and 0xFF)),
+ )
+ if (delta > 40) different++
+ }
+ return different.toDouble() / (w * h)
+ }
+
private fun showDownloads(vararg downloads: DownloadItem) {
composeRule.setContent {
StreamVaultTheme {
@@ -91,7 +193,9 @@ class DownloadsRemoteNavigationTest {
}
private fun press(key: Key) {
- composeRule.onRoot().performKeyInput { pressKey(key) }
+ val dialogs = composeRule.onAllNodes(isDialog()).fetchSemanticsNodes()
+ val target = if (dialogs.isNotEmpty()) composeRule.onNode(isDialog()) else composeRule.onRoot()
+ target.performKeyInput { pressKey(key) }
composeRule.waitForIdle()
}
@@ -102,6 +206,14 @@ class DownloadsRemoteNavigationTest {
Column(modifier = Modifier.fillMaxSize()) { content() }
}
+ /** Like the app: a top menu above the screen, where lost focus ends up. */
+ private val scaffoldWithMenu: SystemScaffoldContent = { _, _, _, _, _, content ->
+ Column(modifier = Modifier.fillMaxSize()) {
+ androidx.compose.material3.TextButton(onClick = {}) { androidx.compose.material3.Text("Menu Home") }
+ content()
+ }
+ }
+
private fun completedDownload() = DownloadItem(
id = "completed",
providerId = 7L,
diff --git a/feature/system/src/androidTest/java/com/streamvault/feature/system/presentation/downloads/DownloadsPresentationTest.kt b/feature/system/src/androidTest/java/com/streamvault/feature/system/presentation/downloads/DownloadsPresentationTest.kt
index 15daaf7a..ed487562 100644
--- a/feature/system/src/androidTest/java/com/streamvault/feature/system/presentation/downloads/DownloadsPresentationTest.kt
+++ b/feature/system/src/androidTest/java/com/streamvault/feature/system/presentation/downloads/DownloadsPresentationTest.kt
@@ -1,7 +1,13 @@
package com.streamvault.feature.system.presentation.downloads
import androidx.compose.foundation.layout.Column
+import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.test.assertIsDisplayed
+import androidx.compose.ui.test.performSemanticsAction
+import androidx.compose.ui.test.hasAnyAncestor
+import androidx.compose.ui.test.hasClickAction
+import androidx.compose.ui.test.hasText
+import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithText
@@ -117,12 +123,16 @@ class DownloadsPresentationTest {
composeRule.onNodeWithText("Completed").assertIsDisplayed()
composeRule.onNodeWithText("completed").performTouchInput { click() }
composeRule.onNodeWithText("Delete download").assertIsDisplayed()
- composeRule.onAllNodesWithText("Delete")[1].performClick()
+ // The dialog ignores presses for a moment after opening.
+ composeRule.mainClock.advanceTimeBy(1_000)
+ composeRule.onNode(hasText("Delete") and hasClickAction() and hasAnyAncestor(isDialog()))
+ .performSemanticsAction(SemanticsActions.OnClick)
assertThat(confirmed).isEqualTo(1)
assertThat(openedId).isEqualTo("completed")
composeRule.onNodeWithText("Delete download").assertIsDisplayed()
- composeRule.onNodeWithText("Cancel").performClick()
+ composeRule.onNode(hasText("Cancel") and hasClickAction())
+ .performSemanticsAction(SemanticsActions.OnClick)
assertThat(dismissed).isEqualTo(1)
}
diff --git a/feature/system/src/main/java/com/streamvault/feature/system/presentation/downloads/DownloadsScreen.kt b/feature/system/src/main/java/com/streamvault/feature/system/presentation/downloads/DownloadsScreen.kt
index cf015868..883222a9 100644
--- a/feature/system/src/main/java/com/streamvault/feature/system/presentation/downloads/DownloadsScreen.kt
+++ b/feature/system/src/main/java/com/streamvault/feature/system/presentation/downloads/DownloadsScreen.kt
@@ -21,9 +21,7 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.RoundedCornerShape
-import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
-import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
@@ -37,10 +35,14 @@ import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
+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.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
@@ -58,10 +60,13 @@ import coil3.compose.AsyncImage
import com.streamvault.feature.system.R
import com.streamvault.core.ui.image.rememberCrossfadeImageModel
import com.streamvault.core.ui.design.BrowseColors as AppColors
+import com.streamvault.core.ui.design.requestFocusSafely
import com.streamvault.core.navigation.AppDestination
import com.streamvault.domain.model.DownloadItem
import com.streamvault.domain.model.DownloadStatus
import com.streamvault.feature.system.api.SystemScaffoldContent
+import com.streamvault.core.ui.components.dialogs.PremiumDialog
+import com.streamvault.core.ui.components.dialogs.PremiumDialogFooterButton
@Composable
fun DownloadsScreen(
@@ -128,6 +133,18 @@ internal fun DownloadsContent(
onConfirmClearTorrentCache: () -> Unit = {},
onDismissClearTorrentCache: () -> Unit = {},
) {
+ // When a deleted download's card disappears, the remote's focus would otherwise jump out of the screen (to the
+ // top menu), so it's put on the screen's first button instead.
+ val folderButtonFocus = remember { FocusRequester() }
+ var deletingId by remember { mutableStateOf<String?>(null) }
+ LaunchedEffect(deletingId, uiState.downloads) {
+ val id = deletingId ?: return@LaunchedEffect
+ if (uiState.downloads.none { it.id == id }) {
+ deletingId = null
+ folderButtonFocus.requestFocusSafely(tag = "DownloadsScreen", target = "Downloads after delete")
+ }
+ }
+
scaffold(
AppDestination.Downloads,
stringResource(R.string.nav_downloads),
@@ -146,7 +163,7 @@ internal fun DownloadsContent(
.padding(horizontal = 16.dp, vertical = 8.dp),
horizontalAlignment = Alignment.End
) {
- Button(onClick = onChangeFolder) {
+ Button(onClick = onChangeFolder, modifier = Modifier.focusRequester(folderButtonFocus)) {
Text(text = stringResource(R.string.download_folder_change))
}
Text(
@@ -185,26 +202,22 @@ internal fun DownloadsContent(
uiState.deleteConfirmItem?.let { item ->
DeleteConfirmDialog(
item = item,
- onConfirm = onConfirmDelete,
+ onConfirm = {
+ deletingId = item.id
+ onConfirmDelete()
+ },
onDismiss = onDismissDelete
)
}
if (uiState.showClearTorrentCacheConfirm) {
- AlertDialog(
- onDismissRequest = onDismissClearTorrentCache,
- title = { Text(stringResource(R.string.torrent_cache_title)) },
- text = { Text(stringResource(R.string.torrent_cache_confirm_message)) },
- confirmButton = {
- TextButton(onClick = onConfirmClearTorrentCache) {
- Text(stringResource(R.string.torrent_cache_clear))
- }
- },
- dismissButton = {
- TextButton(onClick = onDismissClearTorrentCache) {
- Text(stringResource(R.string.settings_cancel))
- }
- }
+ ConfirmDialog(
+ title = stringResource(R.string.torrent_cache_title),
+ message = stringResource(R.string.torrent_cache_confirm_message),
+ confirmLabel = stringResource(R.string.torrent_cache_clear),
+ destructive = false,
+ onConfirm = onConfirmClearTorrentCache,
+ onDismiss = onDismissClearTorrentCache
)
}
}
@@ -560,31 +573,56 @@ private fun DeleteConfirmDialog(
onConfirm: () -> Unit,
onDismiss: () -> Unit
) {
- AlertDialog(
+ ConfirmDialog(
+ title = stringResource(R.string.downloads_delete_confirm_title),
+ message = stringResource(
+ R.string.downloads_delete_confirm_msg,
+ item.contentName.ifBlank { stringResource(R.string.downloads_item_title) }
+ ),
+ confirmLabel = stringResource(R.string.downloads_delete_confirm_delete),
+ destructive = true,
+ onConfirm = onConfirm,
+ onDismiss = onDismiss
+ )
+}
+
+/**
+ * The app's TV dialog: its buttons show clearly which one the remote is on, and it ignores presses for a moment after
+ * opening, so the OK that opened it can't also confirm it.
+ */
+@Composable
+private fun ConfirmDialog(
+ title: String,
+ message: String,
+ confirmLabel: String,
+ destructive: Boolean,
+ onConfirm: () -> Unit,
+ onDismiss: () -> Unit
+) {
+ PremiumDialog(
+ title = title,
onDismissRequest = onDismiss,
- title = {
- Text(text = stringResource(R.string.downloads_delete_confirm_title))
- },
- text = {
+ widthFraction = 0.44f,
+ heightFraction = null,
+ bodyHeightFraction = 0.22f,
+ content = {
Text(
- text = stringResource(
- R.string.downloads_delete_confirm_msg,
- item.contentName.ifBlank { stringResource(R.string.downloads_item_title) }
- )
+ text = message,
+ style = MaterialTheme.typography.bodyLarge,
+ color = AppColors.TextSecondary
)
},
- confirmButton = {
- Button(
+ footer = {
+ PremiumDialogFooterButton(
+ label = stringResource(R.string.settings_cancel),
+ onClick = onDismiss
+ )
+ PremiumDialogFooterButton(
+ label = confirmLabel,
onClick = onConfirm,
- colors = ButtonDefaults.buttonColors(containerColor = AppColors.Live)
- ) {
- Text(text = stringResource(R.string.downloads_delete_confirm_delete))
- }
- },
- dismissButton = {
- TextButton(onClick = onDismiss) {
- Text(text = stringResource(R.string.settings_cancel))
- }
+ destructive = destructive,
+ emphasized = true
+ )
}
)
}