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): move channel progress primitive

David Nashashc7246cb0292d
diff --git a/app/src/main/java/com/streamvault/app/ui/screens/home/HomeScreen.kt b/app/src/main/java/com/streamvault/app/ui/screens/home/HomeScreen.kt
index b9776f93..351f1581 100644
--- a/app/src/main/java/com/streamvault/app/ui/screens/home/HomeScreen.kt
+++ b/app/src/main/java/com/streamvault/app/ui/screens/home/HomeScreen.kt
@@ -49,7 +49,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import com.streamvault.app.ui.components.CategoryRow
import com.streamvault.app.ui.components.ChannelCard
-import com.streamvault.app.ui.components.ChannelProgressTicker
+import com.streamvault.feature.live.presentation.components.LiveChannelProgressTicker
import com.streamvault.app.ui.components.LiveSourceSwitcher
import com.streamvault.core.ui.components.shell.ContentMetadataStrip
import com.streamvault.app.ui.components.shell.LiveChannelRowSurface
@@ -186,7 +186,7 @@ fun HomeScreen(
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val remoteShortcutPreferences by viewModel.remoteShortcutPreferences.collectAsStateWithLifecycle()
- val nowMs by ChannelProgressTicker.nowMs.collectAsStateWithLifecycle()
+ val nowMs by LiveChannelProgressTicker.nowMs.collectAsStateWithLifecycle()
val providerNameById = remember(uiState.allProviders) {
uiState.allProviders.associateBy({ it.id }, { it.name })
}
diff --git a/feature/live/src/main/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTicker.kt b/feature/live/src/main/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTicker.kt
new file mode 100644
index 00000000..f6af5abf
--- /dev/null
+++ b/feature/live/src/main/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTicker.kt
@@ -0,0 +1,37 @@
+package com.streamvault.feature.live.presentation.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
+
+/** Shared clock for live-program progress indicators. */
+object LiveChannelProgressTicker {
+ 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(),
+ )
+}
+
+fun liveChannelProgressFraction(
+ 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/feature/live/src/test/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTickerTest.kt b/feature/live/src/test/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTickerTest.kt
new file mode 100644
index 00000000..00eb3ac8
--- /dev/null
+++ b/feature/live/src/test/java/com/streamvault/feature/live/presentation/components/LiveChannelProgressTickerTest.kt
@@ -0,0 +1,19 @@
+package com.streamvault.feature.live.presentation.components
+
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+
+class LiveChannelProgressTickerTest {
+ @Test
+ fun progressFraction_clampsToLiveProgramBounds() {
+ assertThat(liveChannelProgressFraction(90L, 100L, 200L)).isEqualTo(0f)
+ assertThat(liveChannelProgressFraction(150L, 100L, 200L)).isEqualTo(0.5f)
+ assertThat(liveChannelProgressFraction(250L, 100L, 200L)).isEqualTo(1f)
+ }
+
+ @Test
+ fun progressFraction_rejectsInvalidDurations() {
+ assertThat(liveChannelProgressFraction(150L, 100L, 100L)).isEqualTo(0f)
+ assertThat(liveChannelProgressFraction(150L, 200L, 100L)).isEqualTo(0f)
+ }
+}