StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.
Kotlin iptv-player
fix(downloads): let the remote move between download cards
Rick308f24556612
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.ktindex 24d6ced5..eefc14d8 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@@ -16,6 +16,7 @@ import androidx.compose.ui.test.hasClickActionimport androidx.compose.ui.test.hasAnyAncestorimport androidx.compose.ui.test.hasTextimport androidx.compose.ui.test.isDialog+import androidx.compose.ui.test.isFocusedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performKeyInput@@ -28,6 +29,7 @@ import com.streamvault.core.ui.theme.StreamVaultThemeimport com.streamvault.domain.model.DownloadContentTypeimport com.streamvault.domain.model.DownloadItemimport com.streamvault.domain.model.DownloadStatus+import com.streamvault.domain.model.TorrentCacheStatusimport com.streamvault.feature.system.api.SystemScaffoldContentimport com.streamvault.feature.system.presentation.downloads.DownloadsContentimport com.streamvault.feature.system.presentation.downloads.DownloadsUiState@@ -112,6 +114,47 @@ class DownloadsRemoteNavigationTest {composeRule.onNode(hasText("Change download folder") and hasClickAction()).assertIsFocused()}+ @Test+ fun rightFromACompletedCardReachesTheNextCardsButtons() {+ showDownloads(completedDownload(), failedDownload())++ focus("Delete", index = 0)+ press(Key.DirectionRight)++ composeRule.onNode(hasText("Resume") and hasClickAction()).assertIsFocused()+ }++ @Test+ fun buttonsOfCardsInARowLineUp() {+ showDownloads(completedDownload(), failedDownload())++ val deletes = composeRule.onAllNodes(hasText("Delete") and hasClickAction()).fetchSemanticsNodes()++ assertThat(deletes.map { it.boundsInRoot.top }.distinct()).hasSize(1)+ }++ @Test+ fun downReachesTheNextRowOfCards() {+ showDownloads(*Array(4) { completedDownload().copy(id = "completed-$it", contentName = "Movie $it") })++ focus("Delete", index = 0)+ press(Key.DirectionDown)++ val focused = composeRule.onAllNodes(hasText("Delete") and hasClickAction()).fetchSemanticsNodes()+ .indexOfFirst { it.config.getOrElse(androidx.compose.ui.semantics.SemanticsProperties.Focused) { false } }+ assertThat(focused).isEqualTo(3)+ }++ @Test+ fun downFromClearCacheReachesACard() {+ showDownloads(completedDownload(), failedDownload(), cacheStatus = TorrentCacheStatus(0, 8_000_000_000, 20_000_000_000, 0))++ focus("Clear cache")+ press(Key.DirectionDown)++ composeRule.onNode(hasText("Delete") and hasClickAction() and isFocused()).assertExists()+ }+private var downloads by mutableStateOf(emptyList<DownloadItem>())private var confirmItem by mutableStateOf<DownloadItem?>(null)@@ -167,13 +210,17 @@ class DownloadsRemoteNavigationTest {return different.toDouble() / (w * h)}- private fun showDownloads(vararg downloads: DownloadItem) {+ private fun showDownloads(vararg downloads: DownloadItem, cacheStatus: TorrentCacheStatus? = null) {composeRule.setContent {StreamVaultTheme {MaterialTheme {DownloadsContent(- uiState = DownloadsUiState(isLoading = false, downloads = downloads.toList()),- scaffold = testScaffold,+ uiState = DownloadsUiState(+ isLoading = false,+ downloads = downloads.toList(),+ torrentCacheStatus = cacheStatus+ ),+ scaffold = scaffoldWithMenu,onChangeFolder = {},onOpen = { opened += it },onResume = {},@@ -186,8 +233,9 @@ class DownloadsRemoteNavigationTest {}}- private fun focus(text: String) {- composeRule.onNode(hasText(text) and hasClickAction())+ private fun focus(text: String, index: Int? = null) {+ val matcher = hasText(text) and hasClickAction()+ (if (index == null) composeRule.onNode(matcher) else composeRule.onAllNodes(matcher)[index]).performSemanticsAction(SemanticsActions.RequestFocus)composeRule.waitForIdle()}@@ -214,6 +262,17 @@ class DownloadsRemoteNavigationTest {}}+ private fun failedDownload() = DownloadItem(+ id = "failed",+ providerId = 7L,+ contentType = DownloadContentType.MOVIE,+ contentId = 43L,+ contentName = "Always Fails",+ streamUrl = "https://example.test/failed.mp4",+ status = DownloadStatus.FAILED,+ failureReason = "HTTP 503",+ )+private fun completedDownload() = DownloadItem(id = "completed",providerId = 7L,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.ktindex 883222a9..16334d77 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@@ -5,7 +5,7 @@ import com.streamvault.core.ui.design.BrowseShapesimport androidx.activity.compose.rememberLauncherForActivityResultimport androidx.activity.result.contract.ActivityResultContractsimport androidx.compose.foundation.background-import androidx.compose.foundation.clickable+import androidx.compose.foundation.gestures.detectTapGesturesimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Column@@ -17,9 +17,13 @@ import androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.size-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.layout.BoxWithConstraints+import androidx.compose.foundation.layout.IntrinsicSize+import androidx.compose.foundation.layout.fillMaxHeight+import androidx.compose.foundation.lazy.LazyColumn+import androidx.compose.foundation.lazy.LazyListState+import androidx.compose.foundation.lazy.items+import androidx.compose.foundation.lazy.rememberLazyListStateimport androidx.compose.foundation.shape.RoundedCornerShapeimport androidx.compose.material3.Buttonimport androidx.compose.material3.CircularProgressIndicator@@ -41,16 +45,19 @@ import androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.focus.FocusRequester-import androidx.compose.ui.focus.focusPropertiesimport androidx.compose.ui.focus.focusRequesterimport androidx.compose.ui.graphics.Color+import androidx.compose.ui.input.pointer.pointerInputimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.LocalConfigurationimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.res.stringResource+import androidx.compose.ui.semantics.onClick+import androidx.compose.ui.semantics.semanticsimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.text.style.TextOverflowimport androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.Dpimport androidx.hilt.lifecycle.viewmodel.compose.hiltViewModelimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport androidx.lifecycle.Lifecycle@@ -136,11 +143,13 @@ internal fun DownloadsContent(// 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() }+ val gridState = rememberLazyListState()var deletingId by remember { mutableStateOf<String?>(null) }LaunchedEffect(deletingId, uiState.downloads) {val id = deletingId ?: return@LaunchedEffectif (uiState.downloads.none { it.id == id }) {deletingId = null+ gridState.scrollToItem(0)folderButtonFocus.requestFocusSafely(tag = "DownloadsScreen", target = "Downloads after delete")}}@@ -152,15 +161,14 @@ internal fun DownloadsContent(true,false) {- Column(modifier = Modifier.fillMaxSize()) {- val downloadFolderLabel = uiState.storageConfig.displayName- ?: uiState.storageConfig.treeUri- ?: stringResource(R.string.download_folder_default)-+ val downloadFolderLabel = uiState.storageConfig.displayName+ ?: uiState.storageConfig.treeUri+ ?: stringResource(R.string.download_folder_default)+ val header: @Composable (Dp) -> Unit = { horizontalPadding ->Column(modifier = Modifier.fillMaxWidth()- .padding(horizontal = 16.dp, vertical = 8.dp),+ .padding(horizontal = horizontalPadding, vertical = 8.dp),horizontalAlignment = Alignment.End) {Button(onClick = onChangeFolder, modifier = Modifier.focusRequester(folderButtonFocus)) {@@ -182,20 +190,31 @@ internal fun DownloadsContent(TorrentCacheCard(status = uiState.torrentCacheStatus,isClearing = uiState.isClearingTorrentCache,- onClear = onClearTorrentCache+ onClear = onClearTorrentCache,+ horizontalPadding = horizontalPadding)+ }- when {- uiState.isLoading -> DownloadsLoadingState(Modifier.weight(1f))- uiState.downloads.isEmpty() -> DownloadsEmptyState(Modifier.weight(1f))- else -> DownloadsGrid(- downloads = uiState.downloads,- onOpenClick = onOpen,- onResumeClick = onResume,- onDeleteClick = onDelete,- modifier = Modifier.weight(1f)- )+ when {+ uiState.isLoading -> Column(modifier = Modifier.fillMaxSize()) {+ header(16.dp)+ DownloadsLoadingState(Modifier.weight(1f))+ }+ uiState.downloads.isEmpty() -> Column(modifier = Modifier.fillMaxSize()) {+ header(16.dp)+ DownloadsEmptyState(Modifier.weight(1f))}+ // The header scrolls with the cards: a card is taller than the space left below a fixed header on a TV,+ // so its buttons would sit off screen where the remote can't reach them.+ else -> DownloadsGrid(+ downloads = uiState.downloads,+ state = gridState,+ header = { header(0.dp) },+ onOpenClick = onOpen,+ onResumeClick = onResume,+ onDeleteClick = onDelete,+ modifier = Modifier.fillMaxSize()+ )}}@@ -226,13 +245,14 @@ internal fun DownloadsContent(private fun TorrentCacheCard(status: com.streamvault.domain.model.TorrentCacheStatus?,isClearing: Boolean,- onClear: () -> Unit+ onClear: () -> Unit,+ horizontalPadding: Dp = 16.dp) {val context = LocalContext.currentSurface(modifier = Modifier.fillMaxWidth()- .padding(horizontal = 16.dp, vertical = 4.dp),+ .padding(horizontal = horizontalPadding, vertical = 4.dp),color = AppColors.Surface,shape = BrowseShapes.Control) {@@ -345,31 +365,53 @@ private fun DownloadsEmptyState(modifier: Modifier = Modifier) {@Composableprivate fun DownloadsGrid(downloads: List<DownloadItem>,+ state: LazyListState,+ header: @Composable () -> Unit,onOpenClick: (DownloadItem) -> Unit,onResumeClick: (DownloadItem) -> Unit,onDeleteClick: (DownloadItem) -> Unit,modifier: Modifier = Modifier) {- val columns = if (LocalConfiguration.current.screenWidthDp < 700) {- GridCells.Adaptive(180.dp)- } else {- GridCells.Adaptive(250.dp)- }-- LazyVerticalGrid(- columns = columns,- modifier = modifier.fillMaxSize(),- contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),- horizontalArrangement = Arrangement.spacedBy(12.dp),- verticalArrangement = Arrangement.spacedBy(12.dp)- ) {- items(downloads, key = { it.id }) { download ->- DownloadCard(- download = download,- onOpenClick = { onOpenClick(download) },- onResumeClick = { onResumeClick(download) },- onDeleteClick = { onDeleteClick(download) }- )+ val minCardWidth = if (LocalConfiguration.current.screenWidthDp < 700) 180.dp else 250.dp+ val spacing = 12.dp++ BoxWithConstraints(modifier = modifier.fillMaxSize()) {+ // As many columns as GridCells.Adaptive(minCardWidth) would give.+ val columns = ((maxWidth - 32.dp + spacing) / (minCardWidth + spacing)).toInt().coerceAtLeast(1)+ LazyColumn(+ state = state,+ modifier = Modifier.fillMaxSize(),+ contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),+ verticalArrangement = Arrangement.spacedBy(spacing)+ ) {+ item(key = "header") {+ header()+ }+ items(downloads.chunked(columns), key = { row -> row.first().id }) { row ->+ // The cards of a row share one height, so their buttons line up and Up/Down move straight between+ // rows instead of sideways to whichever card's buttons happen to sit lower.+ Row(+ modifier = Modifier+ .fillMaxWidth()+ .height(IntrinsicSize.Min),+ horizontalArrangement = Arrangement.spacedBy(spacing)+ ) {+ row.forEach { download ->+ DownloadCard(+ download = download,+ onOpenClick = { onOpenClick(download) },+ onResumeClick = { onResumeClick(download) },+ onDeleteClick = { onDeleteClick(download) },+ modifier = Modifier+ .weight(1f)+ .fillMaxHeight()+ )+ }+ repeat(columns - row.size) {+ Spacer(modifier = Modifier.weight(1f))+ }+ }+ }}}}@@ -379,26 +421,37 @@ private fun DownloadCard(download: DownloadItem,onOpenClick: () -> Unit,onResumeClick: () -> Unit,- onDeleteClick: () -> Unit+ onDeleteClick: () -> Unit,+ modifier: Modifier = Modifier) {val progress = download.totalBytes?.takeIf { it > 0L }?.let { total ->(download.bytesWritten.toFloat() / total.toFloat()).coerceIn(0f, 1f)} ?: 0fSurface(- // Tapping the card still opens it, but the remote skips the card: a focused card never lets focus reach its- // buttons, so on TV the card's Play and Delete buttons are the way in.- modifier = Modifier+ // Tapping a completed card still opens it, but the card is no focus target: a focusable card (even one the+ // remote skips) keeps focus from reaching its buttons or moving to the next card's, so on TV the card's own+ // Play and Delete buttons are the way in. It's still read as one item by accessibility services.+ modifier = modifier.fillMaxWidth().clip(BrowseShapes.Control)- .focusProperties { canFocus = false }- .clickable(enabled = download.status == DownloadStatus.COMPLETED, onClick = onOpenClick),+ .semantics(mergeDescendants = true) {+ if (download.status == DownloadStatus.COMPLETED) {+ onClick { onOpenClick(); true }+ }+ }+ .pointerInput(download.status) {+ if (download.status == DownloadStatus.COMPLETED) {+ detectTapGestures(onTap = { onOpenClick() })+ }+ },color = AppColors.Surface,shape = BrowseShapes.Control) {Column(modifier = Modifier.fillMaxWidth()+ .fillMaxHeight().padding(12.dp)) {Box(@@ -464,6 +517,8 @@ private fun DownloadCard()}+ Spacer(modifier = Modifier.weight(1f))+Row(modifier = Modifier.fillMaxWidth()