Skip to content

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

refactor(live): extract epg focus reconciliation

David Nashash20f5f6206f4c
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/epg/EpgScreen.kt b/app/src/main/java/com/streamvault/app/ui/screens/epg/EpgScreen.kt
index 0cd93dd6..8ffb45d6 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/epg/EpgScreen.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/epg/EpgScreen.kt
@@ -4,7 +4,7 @@ import android.view.inputmethod.InputMethodManager
import com.streamvault.domain.playback.ArchiveReplayMechanism
import com.streamvault.domain.playback.archivePlaybackCapability
import com.streamvault.domain.playback.isArchivePlayable
-import com.streamvault.feature.live.presentation.model.guideLookupKey
+import com.streamvault.feature.live.presentation.epg.resolveLiveGuideFocus
import com.streamvault.feature.live.presentation.epg.LiveGuideNowProvider
import com.streamvault.feature.live.presentation.epg.currentLiveGuideNow
import com.streamvault.feature.live.presentation.epg.LiveGuideSearchOverlay
@@ -336,29 +336,15 @@ fun FullEpgScreen(
}
LaunchedEffect(uiState.channels, uiState.programsByChannel) {
- if (uiState.channels.isEmpty()) {
- focusedChannel = null
- focusedProgram = null
- return@LaunchedEffect
- }
- val resolvedChannel = focusedChannel?.let { current ->
- uiState.channels.firstOrNull { it.id == current.id }
- } ?: uiState.channels.firstOrNull()
- focusedChannel = resolvedChannel
- val resolvedPrograms = resolvedChannel?.let { channel ->
- channel.guideLookupKey()?.let { lookupKey ->
- uiState.programsByChannel[lookupKey].orEmpty()
- }.orEmpty()
- }.orEmpty()
- focusedProgram = focusedProgram?.let { focused ->
- resolvedPrograms.firstOrNull {
- it.startTime == focused.startTime &&
- it.endTime == focused.endTime &&
- it.title == focused.title
- }
- } ?: resolvedPrograms.firstOrNull {
- System.currentTimeMillis() in it.startTime until it.endTime
- }
+ val resolvedFocus = resolveLiveGuideFocus(
+ channels = uiState.channels,
+ programsByChannel = uiState.programsByChannel,
+ focusedChannel = focusedChannel,
+ focusedProgram = focusedProgram,
+ now = System.currentTimeMillis()
+ )
+ focusedChannel = resolvedFocus.channel
+ focusedProgram = resolvedFocus.program
}
val contentMessage = when {
diff --git a/docs/COMPOSE_REDUCTION_PHASE5_TRANSITIONAL_DEPENDENCIES.md b/docs/COMPOSE_REDUCTION_PHASE5_TRANSITIONAL_DEPENDENCIES.md
index 5d6dee20..f696da3a 100644
--- a/docs/COMPOSE_REDUCTION_PHASE5_TRANSITIONAL_DEPENDENCIES.md
+++ b/docs/COMPOSE_REDUCTION_PHASE5_TRANSITIONAL_DEPENDENCIES.md
@@ -138,8 +138,8 @@ quick-filter add dialog, delete-group dialog, rename-group dialog, M3U category
organizer dialog, M3U series-assignment dialogs, EPG search overlay, guide
category picker, compact guide-program dialog, guide control rows, EPG preview
pane, guide toolbar, guide message state, hero badge, timeline header, grid
-rows/cells, grid host, and the EPG content host are now feature-owned, with
-the options overlay and EPG-match dialog also feature-owned
+rows/cells, grid host, EPG content host, and focus reconciliation are now
+feature-owned, with the options overlay and EPG-match dialog also feature-owned
while the Home and EPG
screen/dialog composition remains in `:app` for the remaining app-specific
dialogs.
@@ -152,7 +152,8 @@ and the rename-group dialog, M3U category organizer dialog, and M3U
series-assignment dialogs, the EPG search overlay, guide category picker, and
compact guide-program dialog, guide control rows, EPG preview pane, guide
toolbar, guide message state, hero badge, stable EPG channel-key helper,
-timeline header, grid rows/cells, grid host, EPG content host, options overlay,
-and EPG-match dialog are independent of app code;
+timeline header, grid rows/cells, grid host, EPG content host, focus
+reconciliation, options overlay, and EPG-match dialog are independent of app
+code;
screen/resource moves, golden/runtime/performance gates, and legacy app cleanup
remain open, so no live slice acceptance gate is claimed yet.
diff --git a/docs/superpowers/plans/2026-08-28-live-feature-extraction.md b/docs/superpowers/plans/2026-08-28-live-feature-extraction.md
index d270466f..9efc4294 100644
--- a/docs/superpowers/plans/2026-08-28-live-feature-extraction.md
+++ b/docs/superpowers/plans/2026-08-28-live-feature-extraction.md
@@ -28,8 +28,8 @@ quick-filter add dialog, delete-group dialog, rename-group dialog, M3U category
organizer dialog, M3U series-assignment dialogs, EPG search overlay, guide
category picker, compact guide-program dialog, guide control rows, EPG preview
pane, guide toolbar, guide message state, hero badge, timeline header, grid
-rows/cells, grid host, and the EPG content host are now feature-owned, with
-the options overlay and EPG-match dialog also feature-owned
+rows/cells, grid host, EPG content host, and focus reconciliation are now
+feature-owned, with the options overlay and EPG-match dialog also feature-owned
and their feature checks are green. Home screen/dialog composition, the
remaining live cards/rows, complete locale resource migration, and acceptance
gates remain open.
@@ -718,7 +718,9 @@ identity/timeline tests. The timeline header owns marker rendering and
live-time presentation while the app supplies sizing and scroll state. Grid
rows/cells and the grid host own guide row composition, sizing, scrolling, and
focus behavior while the app supplies localized labels, guide data, and
-state/action callbacks. The EPG content host now owns loading, message, preview,
+state/action callbacks. Focus reconciliation now resolves stale channel/program
+selection and current-program fallback behind a feature-owned pure contract.
+The EPG content host now owns loading, message, preview,
toolbar, refresh, and grid composition while the app supplies the localized
label bundles, content message, state, and action callbacks. The options
overlay and EPG-match dialog likewise own their presentation and focus behavior
diff --git a/feature/live/src/main/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocus.kt b/feature/live/src/main/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocus.kt
new file mode 100644
index 00000000..f17f5856
--- /dev/null
+++ b/feature/live/src/main/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocus.kt
@@ -0,0 +1,45 @@
+package com.streamvault.feature.live.presentation.epg
+
+import com.streamvault.domain.model.Channel
+import com.streamvault.domain.model.Program
+import com.streamvault.feature.live.presentation.model.guideLookupKey
+
+data class LiveGuideFocusSelection(
+ val channel: Channel?,
+ val program: Program?
+)
+
+fun resolveLiveGuideFocus(
+ channels: List<Channel>,
+ programsByChannel: Map<String, List<Program>>,
+ focusedChannel: Channel?,
+ focusedProgram: Program?,
+ now: Long
+): LiveGuideFocusSelection {
+ if (channels.isEmpty()) {
+ return LiveGuideFocusSelection(channel = null, program = null)
+ }
+
+ val resolvedChannel = focusedChannel?.let { current ->
+ channels.firstOrNull { it.id == current.id }
+ } ?: channels.firstOrNull()
+ val resolvedPrograms = resolvedChannel?.let { channel ->
+ channel.guideLookupKey()?.let { lookupKey ->
+ programsByChannel[lookupKey].orEmpty()
+ }.orEmpty()
+ }.orEmpty()
+ val resolvedProgram = focusedProgram?.let { focused ->
+ resolvedPrograms.firstOrNull {
+ it.startTime == focused.startTime &&
+ it.endTime == focused.endTime &&
+ it.title == focused.title
+ }
+ } ?: resolvedPrograms.firstOrNull {
+ now in it.startTime until it.endTime
+ }
+
+ return LiveGuideFocusSelection(
+ channel = resolvedChannel,
+ program = resolvedProgram
+ )
+}
diff --git a/feature/live/src/test/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocusTest.kt b/feature/live/src/test/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocusTest.kt
new file mode 100644
index 00000000..e7cb8827
--- /dev/null
+++ b/feature/live/src/test/java/com/streamvault/feature/live/presentation/epg/LiveGuideFocusTest.kt
@@ -0,0 +1,71 @@
+package com.streamvault.feature.live.presentation.epg
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.domain.model.Channel
+import com.streamvault.domain.model.Program
+import org.junit.Test
+
+class LiveGuideFocusTest {
+ @Test
+ fun `focus resolution preserves the selected channel and matching program`() {
+ val channel = Channel(id = 10L, name = "News", streamId = 10L)
+ val program = Program(
+ channelId = "10",
+ title = "Headlines",
+ startTime = 1_000L,
+ endTime = 2_000L
+ )
+
+ val result = resolveLiveGuideFocus(
+ channels = listOf(channel),
+ programsByChannel = mapOf("10" to listOf(program)),
+ focusedChannel = channel,
+ focusedProgram = program,
+ now = 1_500L
+ )
+
+ assertThat(result.channel).isEqualTo(channel)
+ assertThat(result.program).isEqualTo(program)
+ }
+
+ @Test
+ fun `focus resolution falls back to the first channel and current program`() {
+ val channel = Channel(id = 10L, name = "News", streamId = 10L)
+ val program = Program(
+ channelId = "10",
+ title = "Headlines",
+ startTime = 1_000L,
+ endTime = 2_000L
+ )
+
+ val result = resolveLiveGuideFocus(
+ channels = listOf(channel),
+ programsByChannel = mapOf("10" to listOf(program)),
+ focusedChannel = null,
+ focusedProgram = null,
+ now = 1_500L
+ )
+
+ assertThat(result.channel).isEqualTo(channel)
+ assertThat(result.program).isEqualTo(program)
+ }
+
+ @Test
+ fun `focus resolution clears both values when channels are empty`() {
+ val result = resolveLiveGuideFocus(
+ channels = emptyList(),
+ programsByChannel = emptyMap(),
+ focusedChannel = Channel(id = 10L, name = "News"),
+ focusedProgram = Program(
+ channelId = "10",
+ title = "Headlines",
+ startTime = 1_000L,
+ endTime = 2_000L
+ ),
+ now = 1_500L
+ )
+
+ assertThat(result.channel).isNull()
+ assertThat(result.program).isNull()
+ }
+}