Skip to content

Commit 3ce112d

Browse files
committed
fix: scroll the library rail and describe empty sections
1 parent 99edb34 commit 3ce112d

6 files changed

Lines changed: 175 additions & 10 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package video.typetype.tv.ui
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.ui.Alignment
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.input.key.Key
7+
import androidx.compose.ui.test.assertIsFocused
8+
import androidx.compose.ui.test.hasText
9+
import androidx.compose.ui.test.junit4.createComposeRule
10+
import androidx.compose.ui.test.onFirst
11+
import androidx.compose.ui.test.performKeyInput
12+
import androidx.compose.ui.test.pressKey
13+
import androidx.compose.ui.test.requestFocus
14+
import org.junit.Rule
15+
import org.junit.Test
16+
import video.typetype.tv.data.AccountBadge
17+
import video.typetype.tv.data.LibrarySection
18+
import video.typetype.tv.data.MockContentRepository
19+
import video.typetype.tv.navigation.TopTab
20+
import video.typetype.tv.ui.components.TopNav
21+
import video.typetype.tv.ui.library.LibraryScreen
22+
import video.typetype.tv.ui.library.LibrarySession
23+
24+
// An empty section has nothing to focus beside the rail: Right stays on the rail instead of jumping to the top bar,
25+
// and the section says what it is, never "no downloads" for the history.
26+
class LibraryEmptySectionTest {
27+
@get:Rule
28+
val rule = createComposeRule()
29+
30+
@Test
31+
fun rightFromAnEmptySectionStaysOnTheRail() {
32+
val session = LibrarySession().apply { selected = LibrarySection.Downloads }
33+
rule.setContent {
34+
Box {
35+
LibraryScreen(MockContentRepository(0), session, onOpen = {})
36+
Box(Modifier.align(Alignment.TopCenter)) { TopNav(TopTab.Library, onSelect = {}, account = AccountBadge("A")) }
37+
}
38+
}
39+
rule.waitUntil(LOAD_TIMEOUT_MILLIS) { rule.onAllNodes(hasText("No downloads yet")).fetchSemanticsNodes().isNotEmpty() }
40+
rule.waitForIdle()
41+
val downloads = rule.onAllNodes(hasText("Downloads")).onFirst()
42+
downloads.requestFocus()
43+
rule.waitForIdle()
44+
downloads.assertIsFocused()
45+
downloads.performKeyInput { pressKey(Key.DirectionRight) }
46+
rule.waitForIdle()
47+
downloads.assertIsFocused()
48+
}
49+
50+
private companion object {
51+
const val LOAD_TIMEOUT_MILLIS = 10_000L
52+
}
53+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package video.typetype.tv.ui
2+
3+
import androidx.compose.foundation.layout.Box
4+
import androidx.compose.foundation.layout.height
5+
import androidx.compose.material.icons.Icons
6+
import androidx.compose.material.icons.rounded.PlaylistPlay
7+
import androidx.compose.runtime.getValue
8+
import androidx.compose.runtime.mutableStateOf
9+
import androidx.compose.runtime.remember
10+
import androidx.compose.runtime.setValue
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.draw.clipToBounds
13+
import androidx.compose.ui.focus.FocusRequester
14+
import androidx.compose.ui.input.key.Key
15+
import androidx.compose.ui.semantics.Role
16+
import androidx.compose.ui.semantics.SemanticsProperties
17+
import androidx.compose.ui.test.SemanticsMatcher
18+
import androidx.compose.ui.test.SemanticsNodeInteraction
19+
import androidx.compose.ui.test.assertIsFocused
20+
import androidx.compose.ui.test.junit4.createComposeRule
21+
import androidx.compose.ui.test.performKeyInput
22+
import androidx.compose.ui.test.pressKey
23+
import androidx.compose.ui.test.requestFocus
24+
import androidx.compose.ui.unit.dp
25+
import org.junit.Assert.assertTrue
26+
import org.junit.Rule
27+
import org.junit.Test
28+
import video.typetype.tv.ui.components.RailEntry
29+
import video.typetype.tv.ui.components.SectionRail
30+
31+
// The library rail lists the user's playlists after its sections: with many of them, Down must scroll the rail to
32+
// each entry, down to the last one, and Up back to the first.
33+
class SectionRailScrollTest {
34+
@get:Rule
35+
val rule = createComposeRule()
36+
37+
private val entries = (1..ENTRIES).map { RailEntry("p$it", "Playlist $it", Icons.Rounded.PlaylistPlay) }
38+
39+
@Test
40+
fun downReachesAndShowsEveryEntry() {
41+
rule.setContent {
42+
var selected by remember { mutableStateOf(entries.first().id) }
43+
val requester = remember { FocusRequester() }
44+
Box(Modifier.height(WINDOW).clipToBounds()) {
45+
SectionRail("Library", entries, selected, onSelect = { selected = it }, selectedRequester = requester)
46+
}
47+
}
48+
val tabs = rule.onAllNodes(SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Tab))
49+
tabs[0].requestFocus()
50+
rule.waitForIdle()
51+
repeat(ENTRIES - 1) { tabs[it].press(Key.DirectionDown) }
52+
assertWhole(tabs[ENTRIES - 1])
53+
repeat(ENTRIES - 1) { tabs[ENTRIES - 1 - it].press(Key.DirectionUp) }
54+
assertWhole(tabs[0])
55+
}
56+
57+
private fun SemanticsNodeInteraction.press(key: Key) {
58+
performKeyInput { pressKey(key) }
59+
rule.waitForIdle()
60+
}
61+
62+
private fun assertWhole(tab: SemanticsNodeInteraction) {
63+
tab.assertIsFocused()
64+
val node = tab.fetchSemanticsNode()
65+
val window = with(rule.density) { WINDOW.toPx() }
66+
val whole = with(rule.density) { ENTRY_MIN_HEIGHT.toPx() }
67+
assertTrue("the focused entry is cut by the screen", node.positionInRoot.y >= 0f && node.positionInRoot.y + node.size.height <= window)
68+
assertTrue("the focused entry is squeezed", node.size.height >= whole)
69+
}
70+
}
71+
72+
private const val ENTRIES = 12
73+
private val WINDOW = 400.dp
74+
private val ENTRY_MIN_HEIGHT = 40.dp

‎core/resources/src/main/res/values/strings.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
</plurals>
107107
<string name="library_empty_title">No downloads yet</string>
108108
<string name="library_empty_body">Videos you download from the details screen appear here.</string>
109+
<string name="library_empty_history_title">Nothing watched yet</string>
110+
<string name="library_empty_history_body">The videos you watch appear here, the latest first.</string>
111+
<string name="library_empty_favorites_title">No favorites yet</string>
112+
<string name="library_empty_favorites_body">Add a video to your favorites from its details screen to find it here.</string>
113+
<string name="library_empty_watch_later_title">Nothing saved for later</string>
114+
<string name="library_empty_watch_later_body">Save a video to watch later from its details screen to find it here.</string>
115+
<string name="library_empty_playlist_title">This playlist is empty</string>
116+
<string name="library_empty_playlist_body">Add videos to it from their details screen to find them here.</string>
109117

110118
<string name="settings_title">Settings</string>
111119
<string name="settings_group_account">Account</string>

‎core/ui/src/main/java/video/typetype/tv/ui/components/SectionRail.kt‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import androidx.compose.foundation.layout.fillMaxWidth
1313
import androidx.compose.foundation.layout.height
1414
import androidx.compose.foundation.layout.padding
1515
import androidx.compose.foundation.layout.width
16+
import androidx.compose.foundation.rememberScrollState
17+
import androidx.compose.foundation.verticalScroll
1618
import androidx.compose.runtime.Composable
1719
import androidx.compose.ui.Alignment
1820
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.focus.FocusDirection
1922
import androidx.compose.ui.focus.FocusRequester
2023
import androidx.compose.ui.focus.focusRequester
2124
import androidx.compose.ui.graphics.vector.ImageVector
@@ -28,7 +31,7 @@ import video.typetype.tv.theme.Palette
2831
import video.typetype.tv.theme.Shapes
2932
import video.typetype.tv.theme.Type
3033
import video.typetype.tv.ui.util.focusEntersAt
31-
import video.typetype.tv.ui.util.trapFocusDown
34+
import video.typetype.tv.ui.util.trapFocus
3235

3336
data class RailEntry(val id: String, val label: String, val icon: ImageVector)
3437

@@ -40,11 +43,22 @@ fun SectionRail(
4043
onSelect: (String) -> Unit,
4144
selectedRequester: FocusRequester,
4245
modifier: Modifier = Modifier,
46+
// False while the content beside the rail has nothing to focus (empty, loading): Right then stays on the rail
47+
// instead of jumping to the top bar.
48+
contentFocusable: Boolean = true,
4349
) {
44-
Column(modifier = modifier.width(232.dp).trapFocusDown()) {
50+
val blocked = if (contentFocusable) listOf(FocusDirection.Down) else listOf(FocusDirection.Down, FocusDirection.Right)
51+
Column(modifier = modifier.width(232.dp).trapFocus(blocked)) {
4552
TextLabel(title, Type.Headline)
4653
Spacer(Modifier.height(20.dp))
47-
Column(Modifier.focusEntersAt(selectedRequester), verticalArrangement = Arrangement.spacedBy(6.dp)) {
54+
// The entries include the user's playlists, so the rail scrolls to the focused one instead of running off screen.
55+
Column(
56+
modifier = Modifier
57+
.focusEntersAt(selectedRequester)
58+
.verticalScroll(rememberScrollState())
59+
.padding(bottom = 48.dp),
60+
verticalArrangement = Arrangement.spacedBy(6.dp),
61+
) {
4862
entries.forEach { entry ->
4963
val isSelected = entry.id == selectedId
5064
RailItem(

‎feature/library/src/main/java/video/typetype/tv/ui/library/LibraryScreen.kt‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import androidx.compose.foundation.lazy.grid.GridCells
2323
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
2424
import androidx.compose.foundation.lazy.grid.items
2525
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
26-
import androidx.compose.material.icons.Icons
27-
import androidx.compose.material.icons.rounded.Download
2826
import androidx.compose.runtime.Composable
2927
import video.typetype.tv.ui.components.PlaylistContext
3028
import video.typetype.tv.ui.components.LocalPlaylistContext
@@ -130,6 +128,7 @@ fun LibraryScreen(
130128
selectedId = session.selected.id,
131129
onSelect = { id -> tabs.firstOrNull { it.id == id }?.let { session.selected = it } },
132130
selectedRequester = selectedRequester,
131+
contentFocusable = !items.isNullOrEmpty() || itemsFailure != null,
133132
)
134133
Crossfade(
135134
targetState = items,
@@ -140,7 +139,7 @@ fun LibraryScreen(
140139
when {
141140
loaded == null && itemsFailure != null -> ScreenFailure(itemsFailure, onRetry = { itemsAttempt++ })
142141
loaded == null -> VideoGridSkeleton()
143-
loaded.isEmpty() -> EmptyLibrary()
142+
loaded.isEmpty() -> EmptyLibrary(session.selected)
144143
else -> CompositionLocalProvider(LocalPlaylistContext provides playlist) {
145144
LibraryGrid(
146145
videos = loaded,
@@ -206,7 +205,7 @@ private fun LibraryGrid(
206205
}
207206

208207
@Composable
209-
private fun EmptyLibrary() {
208+
private fun EmptyLibrary(section: LibrarySection) {
210209
Column(
211210
modifier = Modifier.fillMaxSize(),
212211
horizontalAlignment = Alignment.CenterHorizontally,
@@ -216,14 +215,14 @@ private fun EmptyLibrary() {
216215
Modifier.size(84.dp).background(Palette.Glass, Shapes.Panel),
217216
contentAlignment = Alignment.Center,
218217
) {
219-
Glyph(Icons.Rounded.Download, Palette.TextHigh, 40)
218+
Glyph(section.icon(), Palette.TextHigh, 40)
220219
}
221220
Spacer(Modifier.height(20.dp))
222-
TextLabel(stringResource(R.string.library_empty_title), Type.Headline)
221+
TextLabel(stringResource(section.emptyTitle()), Type.Headline)
223222
Spacer(Modifier.height(8.dp))
224223
Box(Modifier.size(width = 380.dp, height = 44.dp), contentAlignment = Alignment.TopCenter) {
225224
TextLabel(
226-
stringResource(R.string.library_empty_body),
225+
stringResource(section.emptyBody()),
227226
Type.Body.copy(textAlign = androidx.compose.ui.text.style.TextAlign.Center),
228227
maxLines = 2,
229228
)

‎feature/library/src/main/java/video/typetype/tv/ui/library/LibrarySession.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,20 @@ internal fun LibrarySection.label(): String = when (this) {
4040
is LibrarySection.Playlist -> name
4141
is LibrarySection.Saved -> name
4242
}
43+
44+
// What an empty section says: each one tells how videos get there, so an empty history never talks about downloads.
45+
internal fun LibrarySection.emptyTitle(): Int = when (this) {
46+
LibrarySection.History -> R.string.library_empty_history_title
47+
LibrarySection.Favorites -> R.string.library_empty_favorites_title
48+
LibrarySection.WatchLater -> R.string.library_empty_watch_later_title
49+
LibrarySection.Downloads -> R.string.library_empty_title
50+
is LibrarySection.Playlist, is LibrarySection.Saved -> R.string.library_empty_playlist_title
51+
}
52+
53+
internal fun LibrarySection.emptyBody(): Int = when (this) {
54+
LibrarySection.History -> R.string.library_empty_history_body
55+
LibrarySection.Favorites -> R.string.library_empty_favorites_body
56+
LibrarySection.WatchLater -> R.string.library_empty_watch_later_body
57+
LibrarySection.Downloads -> R.string.library_empty_body
58+
is LibrarySection.Playlist, is LibrarySection.Saved -> R.string.library_empty_playlist_body
59+
}

0 commit comments

Comments
 (0)