Skip to content

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

refactor: model phase 2 modal and channel state

David Nashash8f24fe70c52c
diff --git a/app/src/main/java/com/streamvault/app/ui/components/Cards.kt b/app/src/main/java/com/streamvault/app/ui/components/Cards.kt
index 0c3a366d..2f65a145 100644
--- a/app/src/main/java/com/streamvault/app/ui/components/Cards.kt
+++ b/app/src/main/java/com/streamvault/app/ui/components/Cards.kt
@@ -27,13 +27,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.compose.collectAsStateWithLifecycle
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.SupervisorJob
-import kotlinx.coroutines.delay
-import kotlinx.coroutines.flow.SharingStarted
-import kotlinx.coroutines.flow.flow
-import kotlinx.coroutines.flow.stateIn
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -83,21 +76,6 @@ import com.streamvault.app.ui.design.FocusSpec
import com.streamvault.app.ui.interaction.mouseClickable
import com.streamvault.app.ui.interaction.rememberTvInteractionSounds
-private object ChannelProgressTicker {
- private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
-
- val nowMs = flow {
- while (true) {
- emit(System.currentTimeMillis())
- delay(30_000L)
- }
- }.stateIn(
- scope = scope,
- started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 30_000L),
- initialValue = System.currentTimeMillis()
- )
-}
-
@Composable
fun FocusableCard(
onClick: () -> Unit,
diff --git a/app/src/main/java/com/streamvault/app/ui/components/ChannelProgressTicker.kt b/app/src/main/java/com/streamvault/app/ui/components/ChannelProgressTicker.kt
new file mode 100644
index 00000000..fdf579b1
--- /dev/null
+++ b/app/src/main/java/com/streamvault/app/ui/components/ChannelProgressTicker.kt
@@ -0,0 +1,37 @@
+package com.streamvault.app.ui.components
+
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.flow.SharingStarted
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.flow.stateIn
+
+internal object ChannelProgressTicker {
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
+
+ val nowMs: StateFlow<Long> = flow {
+ while (true) {
+ emit(System.currentTimeMillis())
+ delay(30_000L)
+ }
+ }.stateIn(
+ scope = scope,
+ started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 30_000L),
+ initialValue = System.currentTimeMillis()
+ )
+}
+
+internal fun channelProgressFraction(
+ nowMs: Long,
+ startTimeMs: Long,
+ endTimeMs: Long
+): Float {
+ val durationMs = endTimeMs - startTimeMs
+ if (durationMs <= 0L) return 0f
+
+ return ((nowMs - startTimeMs).toFloat() / durationMs.toFloat()).coerceIn(0f, 1f)
+}
+
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerModalState.kt b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerModalState.kt
index 75010c49..7d690655 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerModalState.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/player/PlayerModalState.kt
@@ -2,32 +2,87 @@ package com.streamvault.app.ui.screens.player
import com.streamvault.player.TrackType
+internal sealed interface PlayerModal {
+ data class TrackSelection(val trackType: TrackType) : PlayerModal
+ data object VariantSelection : PlayerModal
+ data object SpeedSelection : PlayerModal
+ data object AudioVideoOffset : PlayerModal
+ data object StopPlaybackTimer : PlayerModal
+ data object IdleStandbyTimer : PlayerModal
+ data object ProgramHistory : PlayerModal
+ data object Split : PlayerModal
+ data object EpisodePicker : PlayerModal
+}
/**
* UI-owned modal state for the player screen.
*
- * Keeping these flags in one immutable snapshot makes modal visibility a
- * separate concern from high-frequency playback state and gives input,
- * focus, auto-hide, and modal rendering one consistent source of truth.
+ * The active modal is the single source of truth. The derived queries retain
+ * the existing screen contracts while making modal visibility exclusive.
*/
internal data class PlayerModalState(
- val trackSelection: TrackType? = null,
- val showVariantSelection: Boolean = false,
- val showSpeedSelection: Boolean = false,
- val showAudioVideoOffsetDialog: Boolean = false,
- val showStopPlaybackTimerDialog: Boolean = false,
- val showIdleStandbyTimerDialog: Boolean = false,
- val showProgramHistory: Boolean = false,
- val showSplitDialog: Boolean = false,
- val showEpisodePicker: Boolean = false
+ val active: PlayerModal? = null
) {
+ val trackSelection: TrackType?
+ get() = (active as? PlayerModal.TrackSelection)?.trackType
+
+ val showVariantSelection: Boolean
+ get() = active is PlayerModal.VariantSelection
+
+ val showSpeedSelection: Boolean
+ get() = active is PlayerModal.SpeedSelection
+
+ val showAudioVideoOffsetDialog: Boolean
+ get() = active is PlayerModal.AudioVideoOffset
+
+ val showStopPlaybackTimerDialog: Boolean
+ get() = active is PlayerModal.StopPlaybackTimer
+
+ val showIdleStandbyTimerDialog: Boolean
+ get() = active is PlayerModal.IdleStandbyTimer
+
+ val showProgramHistory: Boolean
+ get() = active is PlayerModal.ProgramHistory
+
+ val showSplitDialog: Boolean
+ get() = active is PlayerModal.Split
+
+ val showEpisodePicker: Boolean
+ get() = active is PlayerModal.EpisodePicker
+
val hasVisibleModal: Boolean
- get() = trackSelection != null ||
- showVariantSelection ||
- showSpeedSelection ||
- showAudioVideoOffsetDialog ||
- showStopPlaybackTimerDialog ||
- showIdleStandbyTimerDialog ||
- showProgramHistory ||
- showSplitDialog ||
- showEpisodePicker
+ get() = active != null
+
+ fun open(modal: PlayerModal): PlayerModalState = PlayerModalState(active = modal)
+
+ fun dismiss(): PlayerModalState = PlayerModalState()
+
+ /**
+ * Temporary compatibility overload for the Phase 1 call sites. It
+ * derives one active modal and stores no duplicate flags. Task 2 replaces
+ * these calls with open and dismiss.
+ */
+ fun copy(
+ trackSelection: TrackType? = this.trackSelection,
+ showVariantSelection: Boolean = this.showVariantSelection,
+ showSpeedSelection: Boolean = this.showSpeedSelection,
+ showAudioVideoOffsetDialog: Boolean = this.showAudioVideoOffsetDialog,
+ showStopPlaybackTimerDialog: Boolean = this.showStopPlaybackTimerDialog,
+ showIdleStandbyTimerDialog: Boolean = this.showIdleStandbyTimerDialog,
+ showProgramHistory: Boolean = this.showProgramHistory,
+ showSplitDialog: Boolean = this.showSplitDialog,
+ showEpisodePicker: Boolean = this.showEpisodePicker
+ ): PlayerModalState = PlayerModalState(
+ active = when {
+ trackSelection != null -> PlayerModal.TrackSelection(trackSelection)
+ showVariantSelection -> PlayerModal.VariantSelection
+ showSpeedSelection -> PlayerModal.SpeedSelection
+ showAudioVideoOffsetDialog -> PlayerModal.AudioVideoOffset
+ showStopPlaybackTimerDialog -> PlayerModal.StopPlaybackTimer
+ showIdleStandbyTimerDialog -> PlayerModal.IdleStandbyTimer
+ showProgramHistory -> PlayerModal.ProgramHistory
+ showSplitDialog -> PlayerModal.Split
+ showEpisodePicker -> PlayerModal.EpisodePicker
+ else -> null
+ }
+ )
}
diff --git a/app/src/test/java/com/streamvault/app/ui/components/ChannelProgressTickerTest.kt b/app/src/test/java/com/streamvault/app/ui/components/ChannelProgressTickerTest.kt
new file mode 100644
index 00000000..2e06fdb8
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/ui/components/ChannelProgressTickerTest.kt
@@ -0,0 +1,20 @@
+package com.streamvault.app.ui.components
+
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+
+class ChannelProgressTickerTest {
+
+ @Test
+ fun `progress is clamped to the program interval`() {
+ assertThat(channelProgressFraction(90L, 100L, 200L)).isEqualTo(0f)
+ assertThat(channelProgressFraction(150L, 100L, 200L)).isEqualTo(0.5f)
+ assertThat(channelProgressFraction(250L, 100L, 200L)).isEqualTo(1f)
+ }
+
+ @Test
+ fun `progress is zero for a non-positive duration`() {
+ assertThat(channelProgressFraction(150L, 100L, 100L)).isEqualTo(0f)
+ assertThat(channelProgressFraction(150L, 200L, 100L)).isEqualTo(0f)
+ }
+}
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/player/PlayerModalStateTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/player/PlayerModalStateTest.kt
index 7aca7abf..ff7be41f 100644
--- a/app/src/test/java/com/streamvault/app/ui/screens/player/PlayerModalStateTest.kt
+++ b/app/src/test/java/com/streamvault/app/ui/screens/player/PlayerModalStateTest.kt
@@ -7,9 +7,52 @@ import org.junit.Test
class PlayerModalStateTest {
@Test
- fun `modal state reports whether any player modal is visible`() {
- assertThat(PlayerModalState().hasVisibleModal).isFalse()
- assertThat(PlayerModalState(showSpeedSelection = true).hasVisibleModal).isTrue()
- assertThat(PlayerModalState(trackSelection = TrackType.AUDIO).hasVisibleModal).isTrue()
+ fun `opening a modal replaces the previous modal`() {
+ val state = PlayerModalState()
+ .open(PlayerModal.ProgramHistory)
+ .open(PlayerModal.SpeedSelection)
+
+ assertThat(state.showProgramHistory).isFalse()
+ assertThat(state.showSpeedSelection).isTrue()
+ }
+
+ @Test
+ fun `track selection retains its track type`() {
+ val state = PlayerModalState().open(PlayerModal.TrackSelection(TrackType.AUDIO))
+
+ assertThat(state.trackSelection).isEqualTo(TrackType.AUDIO)
+ }
+
+ @Test
+ fun `derived visibility reflects the active modal`() {
+ val state = PlayerModalState().open(PlayerModal.AudioVideoOffset)
+
+ assertThat(state.showAudioVideoOffsetDialog).isTrue()
+ assertThat(state.showVariantSelection).isFalse()
+ assertThat(state.showSpeedSelection).isFalse()
+ assertThat(state.showStopPlaybackTimerDialog).isFalse()
+ assertThat(state.showIdleStandbyTimerDialog).isFalse()
+ assertThat(state.showProgramHistory).isFalse()
+ assertThat(state.showSplitDialog).isFalse()
+ assertThat(state.showEpisodePicker).isFalse()
+ assertThat(state.hasVisibleModal).isTrue()
+ }
+
+ @Test
+ fun `dismissing a modal clears the active modal`() {
+ val state = PlayerModalState()
+ .open(PlayerModal.EpisodePicker)
+ .dismiss()
+
+ assertThat(state.active).isNull()
+ assertThat(state.hasVisibleModal).isFalse()
+ }
+
+ @Test
+ fun `empty state has no active modal`() {
+ val state = PlayerModalState()
+
+ assertThat(state.active).isNull()
+ assertThat(state.hasVisibleModal).isFalse()
}
}