Skip to content

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

refactor: extract startup navigation coordination

David Nashash466bdeab6878
diff --git a/app/src/main/java/com/streamvault/app/MainActivity.kt b/app/src/main/java/com/streamvault/app/MainActivity.kt
index ac719125..664fa695 100644
--- a/app/src/main/java/com/streamvault/app/MainActivity.kt
+++ b/app/src/main/java/com/streamvault/app/MainActivity.kt
@@ -17,6 +17,7 @@ import com.streamvault.app.localization.resolveAppLocale
import com.streamvault.app.navigation.AppNavigation
import com.streamvault.app.navigation.AppNavigationCoordinator
import com.streamvault.app.navigation.ExternalNavigationRequestParser
+import com.streamvault.app.navigation.StartupNavigationResolver
import com.streamvault.core.navigation.PlayerNavigationRequest
import com.streamvault.core.navigation.ExternalNavigationRequest
import com.streamvault.core.ui.theme.StreamVaultTheme
@@ -101,6 +102,9 @@ class MainActivity : ComponentActivity() {
@Inject
lateinit var externalNavigationRequestParser: ExternalNavigationRequestParser
+ @Inject
+ internal lateinit var startupNavigationResolver: StartupNavigationResolver
+
internal val pendingNavigationCommand = appNavigationCoordinator.pendingCommand
private var playerPictureInPictureState = PlayerPictureInPictureState()
diff --git a/app/src/main/java/com/streamvault/app/navigation/AppNavigationCoordinator.kt b/app/src/main/java/com/streamvault/app/navigation/AppNavigationCoordinator.kt
index 6f41afe5..7ac36a85 100644
--- a/app/src/main/java/com/streamvault/app/navigation/AppNavigationCoordinator.kt
+++ b/app/src/main/java/com/streamvault/app/navigation/AppNavigationCoordinator.kt
@@ -1,17 +1,32 @@
package com.streamvault.app.navigation
import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
import com.streamvault.core.navigation.AppDestination
import com.streamvault.core.navigation.ExternalNavigationRequest
import com.streamvault.core.navigation.NavigationCommand
import com.streamvault.core.navigation.NavigationOptions
import com.streamvault.core.navigation.PlayerNavigationRequest
+import com.streamvault.data.preferences.PreferencesRepository
+import com.streamvault.domain.model.AppLandingDestination
+import com.streamvault.domain.model.AppTopLevelDestination
+import com.streamvault.domain.model.CatalogLayout
+import com.streamvault.domain.model.ContentType
+import com.streamvault.domain.repository.ProviderRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.flow.collectLatest
+import kotlinx.coroutines.flow.combine
+import kotlinx.coroutines.flow.distinctUntilChanged
+import kotlinx.coroutines.flow.flatMapLatest
+import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.update
+import kotlinx.coroutines.launch
data class PendingNavigationCommand(
val id: Long,
@@ -20,29 +35,188 @@ data class PendingNavigationCommand(
@HiltViewModel
class AppNavigationCoordinator @Inject constructor(
- private val commandIds: NavigationCommandIdSource
+ private val commandIds: NavigationCommandIdSource,
+ private val startupResolver: StartupNavigationResolver,
+ private val preferencesRepository: PreferencesRepository,
+ private val providerRepository: ProviderRepository
) : ViewModel() {
private val commandQueue = ArrayDeque<PendingNavigationCommand>()
private val _pendingCommand = MutableStateFlow<PendingNavigationCommand?>(null)
val pendingCommand: StateFlow<PendingNavigationCommand?> = _pendingCommand.asStateFlow()
+ private val _state = MutableStateFlow(AppNavigationState())
+ val state: StateFlow<AppNavigationState> = _state.asStateFlow()
+
+ private var activeProviderId: Long? = null
+ private var startupNavigationRequested = false
+ private var startupNavigationAcknowledged = false
+ private var startupNavigationCommandId: Long? = null
+ private var startupPlayerRequest: PlayerNavigationRequest? = null
+ private var startupPlayerEnqueued = false
+
+ init {
+ observeStartupState()
+ observeCatalogState()
+ }
+
+ private fun observeStartupState() {
+ viewModelScope.launch {
+ combine(
+ preferencesRepository.appLandingDestination,
+ preferencesRepository.appTopLevelDestinations,
+ providerRepository.getActiveProvider()
+ ) { preferredLanding, topLevelDestinations, provider ->
+ StartupInputs(
+ preferredLanding = preferredLanding,
+ topLevelDestinations = topLevelDestinations,
+ providerId = provider?.id,
+ catalogLayout = provider?.catalogLayout
+ )
+ }
+ .distinctUntilChanged()
+ .collectLatest { inputs ->
+ activeProviderId = inputs.providerId
+ val landingDestination = AppTopLevelDestination.resolveLandingDestination(
+ preferred = inputs.preferredLanding,
+ destinations = inputs.topLevelDestinations
+ )
+ val startupTarget = startupResolver.resolve(landingDestination)
+ _state.update {
+ it.copy(
+ startupTarget = startupTarget,
+ topLevelDestinations = inputs.topLevelDestinations,
+ catalogLayout = inputs.catalogLayout
+ )
+ }
+ }
+ }
+ }
+
+ private fun observeCatalogState() {
+ viewModelScope.launch {
+ providerRepository.getActiveProvider()
+ .flatMapLatest { provider ->
+ if (provider == null) {
+ flowOf(CatalogState(null, null, ContentType.MOVIE, ready = false))
+ } else {
+ preferencesRepository.getLastSplitCatalogType(provider.id).map { lastSplitType ->
+ CatalogState(
+ providerId = provider.id,
+ layout = provider.catalogLayout,
+ lastSplitType = lastSplitType,
+ ready = true
+ )
+ }
+ }
+ }
+ .collectLatest { catalogState ->
+ activeProviderId = catalogState.providerId
+ _state.update {
+ it.copy(
+ catalogLayout = catalogState.layout,
+ lastSplitCatalogType = catalogState.lastSplitType,
+ splitPreferenceReady = catalogState.ready
+ )
+ }
+ }
+ }
+ }
fun submitExternalRequest(request: ExternalNavigationRequest) {
submit(request.toNavigationCommand())
}
fun submit(command: NavigationCommand) {
- commandQueue += PendingNavigationCommand(commandIds.next(), command)
+ enqueue(command)
+ }
+
+ fun requestStartupNavigation(popUpTo: AppDestination) {
+ if (startupNavigationRequested) return
+ val target = state.value.startupTarget ?: return
+ startupNavigationRequested = true
+ startupPlayerRequest = target.playerRequest
+ startupNavigationCommandId = enqueue(
+ NavigationCommand.Navigate(
+ destination = target.destination,
+ options = NavigationOptions(popUpTo = popUpTo, inclusive = true)
+ )
+ ).id
+ }
+
+ fun onDestinationResumed(destination: AppDestination) {
+ if (!startupNavigationAcknowledged || destination !is AppDestination.LiveTv) return
+ val request = startupPlayerRequest ?: return
+ if (startupPlayerEnqueued) return
+ startupPlayerEnqueued = true
+ submit(NavigationCommand.OpenPlayer(request))
+ }
+
+ fun requestTopLevelNavigation(requested: AppDestination) {
+ val currentState = state.value
+ val destination = resolveCatalogDestination(
+ layout = currentState.catalogLayout,
+ requested = requested,
+ lastSplitCatalogType = currentState.lastSplitCatalogType,
+ splitPreferenceReady = currentState.splitPreferenceReady
+ )
+ val splitType = when (requested) {
+ AppDestination.Movies -> ContentType.MOVIE
+ AppDestination.Series -> ContentType.SERIES
+ else -> null
+ }
+ if (currentState.catalogLayout == CatalogLayout.SPLIT && splitType != null) {
+ _state.update { it.copy(lastSplitCatalogType = splitType, splitPreferenceReady = true) }
+ activeProviderId?.let { providerId ->
+ viewModelScope.launch {
+ preferencesRepository.setLastSplitCatalogType(providerId, splitType)
+ }
+ }
+ }
+ submit(
+ NavigationCommand.Navigate(
+ destination = destination,
+ options = NavigationOptions(
+ launchSingleTop = true,
+ restoreState = true,
+ saveState = true,
+ popUpTo = AppDestination.Welcome
+ )
+ )
+ )
+ }
+
+ private fun enqueue(command: NavigationCommand): PendingNavigationCommand {
+ val pending = PendingNavigationCommand(commandIds.next(), command)
+ commandQueue += pending
if (_pendingCommand.value == null) {
- _pendingCommand.value = commandQueue.first()
+ _pendingCommand.value = pending
}
+ return pending
}
fun acknowledge(id: Long) {
val pending = commandQueue.firstOrNull() ?: return
if (pending.id != id) return
commandQueue.removeFirst()
+ if (startupNavigationCommandId == id) {
+ startupNavigationAcknowledged = true
+ startupNavigationCommandId = null
+ }
_pendingCommand.value = commandQueue.firstOrNull()
}
+
+ private data class StartupInputs(
+ val preferredLanding: AppLandingDestination,
+ val topLevelDestinations: List<AppTopLevelDestination>,
+ val providerId: Long?,
+ val catalogLayout: CatalogLayout?
+ )
+
+ private data class CatalogState(
+ val providerId: Long?,
+ val layout: CatalogLayout?,
+ val lastSplitType: ContentType,
+ val ready: Boolean
+ )
}
internal fun ExternalNavigationRequest.toNavigationCommand(): NavigationCommand = when (this) {
diff --git a/app/src/main/java/com/streamvault/app/navigation/AppNavigationPolicy.kt b/app/src/main/java/com/streamvault/app/navigation/AppNavigationPolicy.kt
index 7f80e7ed..e334827c 100644
--- a/app/src/main/java/com/streamvault/app/navigation/AppNavigationPolicy.kt
+++ b/app/src/main/java/com/streamvault/app/navigation/AppNavigationPolicy.kt
@@ -1,5 +1,6 @@
package com.streamvault.app.navigation
+import com.streamvault.core.navigation.AppDestination
import com.streamvault.domain.model.AppLandingDestination
import com.streamvault.domain.model.CatalogLayout
import com.streamvault.domain.model.ContentType
@@ -8,14 +9,27 @@ internal fun requiresResolvedStartupTarget(landingDestination: AppLandingDestina
landingDestination == AppLandingDestination.FIRST_FAVORITE_LIVE ||
landingDestination == AppLandingDestination.LAST_WATCHED_LIVE
+internal fun resolveCatalogDestination(
+ layout: CatalogLayout?,
+ requested: AppDestination,
+ lastSplitCatalogType: ContentType,
+ splitPreferenceReady: Boolean
+): AppDestination = when {
+ layout != CatalogLayout.SPLIT && requested in setOf(AppDestination.Movies, AppDestination.Series) ->
+ AppDestination.Vod
+ layout == CatalogLayout.SPLIT && requested == AppDestination.Vod && splitPreferenceReady ->
+ if (lastSplitCatalogType == ContentType.SERIES) AppDestination.Series else AppDestination.Movies
+ else -> requested
+}
+
+/** Temporary route adapter for the pre-coordinator root wiring. */
internal fun resolveCatalogRoute(
layout: CatalogLayout?,
requestedRoute: String,
lastSplitCatalogType: ContentType,
splitPreferenceReady: Boolean
-): String = when {
- layout != CatalogLayout.SPLIT && requestedRoute in setOf(Routes.MOVIES, Routes.SERIES) -> Routes.VOD
- layout == CatalogLayout.SPLIT && requestedRoute == Routes.VOD && splitPreferenceReady ->
- if (lastSplitCatalogType == ContentType.SERIES) Routes.SERIES else Routes.MOVIES
- else -> requestedRoute
-}
+): String = AppRouteCodec.decode(requestedRoute)?.let { decoded ->
+ AppRouteCodec.encode(
+ resolveCatalogDestination(layout, decoded, lastSplitCatalogType, splitPreferenceReady)
+ )
+} ?: requestedRoute
diff --git a/app/src/main/java/com/streamvault/app/navigation/AppNavigationStartupPolicy.kt b/app/src/main/java/com/streamvault/app/navigation/AppNavigationStartupPolicy.kt
index 0d0f763d..16911434 100644
--- a/app/src/main/java/com/streamvault/app/navigation/AppNavigationStartupPolicy.kt
+++ b/app/src/main/java/com/streamvault/app/navigation/AppNavigationStartupPolicy.kt
@@ -1,113 +1,13 @@
package com.streamvault.app.navigation
-import com.streamvault.core.navigation.PlayerNavigationRequest
-import com.streamvault.core.navigation.AppDestination
import com.streamvault.app.MainActivity
-import com.streamvault.domain.model.ActiveLiveSource
+import com.streamvault.core.navigation.PlayerNavigationRequest
import com.streamvault.domain.model.AppLandingDestination
-import com.streamvault.domain.model.ContentType
-import com.streamvault.domain.model.VirtualCategoryIds
-import kotlinx.coroutines.flow.first
+/** Temporary compatibility bridge for the pre-coordinator root wiring. */
internal suspend fun resolveStartupPlayerRequest(
mainActivity: MainActivity,
landingDestination: AppLandingDestination
-): PlayerNavigationRequest? = when (landingDestination) {
- AppLandingDestination.FIRST_FAVORITE_LIVE -> resolveFirstFavoriteStartupTarget(mainActivity)
- AppLandingDestination.LAST_WATCHED_LIVE -> resolveLastWatchedStartupTarget(mainActivity)
- else -> null
-}
-
-private suspend fun resolveFirstFavoriteStartupTarget(
- mainActivity: MainActivity
-): PlayerNavigationRequest? {
- if (!mainActivity.preferencesRepository.showFavoritesCategory.first()) return null
- val context = resolveLiveStartupContext(mainActivity) ?: return null
- val favorites = when (context) {
- is LiveStartupContext.Provider -> mainActivity.favoriteRepository.getFavorites(context.providerId, ContentType.LIVE).first()
- is LiveStartupContext.Combined -> mainActivity.favoriteRepository.getFavorites(context.providerIds, ContentType.LIVE).first()
- }.sortedBy { it.position }
- return resolveStartupChannelTarget(
- mainActivity = mainActivity,
- channelIds = favorites.map { it.contentId },
- sourceContext = context,
- virtualCategoryId = VirtualCategoryIds.FAVORITES
- )
-}
-
-private suspend fun resolveLastWatchedStartupTarget(
- mainActivity: MainActivity
-): PlayerNavigationRequest? {
- val context = resolveLiveStartupContext(mainActivity) ?: return null
- val recentHistory = when (context) {
- is LiveStartupContext.Provider -> mainActivity.playbackHistoryRepository.getRecentlyWatchedByProvider(context.providerId, limit = 24).first()
- is LiveStartupContext.Combined -> mainActivity.playbackHistoryRepository.getRecentlyWatchedByProviders(context.providerIds.toSet(), limit = 24).first()
- }
- return resolveStartupChannelTarget(
- mainActivity = mainActivity,
- channelIds = recentHistory
- .filter { it.contentType == ContentType.LIVE }
- .sortedByDescending { it.lastWatchedAt }
- .map { it.contentId },
- sourceContext = context,
- virtualCategoryId = VirtualCategoryIds.RECENT
- )
-}
-
-private suspend fun resolveStartupChannelTarget(
- mainActivity: MainActivity,
- channelIds: List<Long>,
- sourceContext: LiveStartupContext,
- virtualCategoryId: Long
-): PlayerNavigationRequest? {
- if (channelIds.isEmpty()) return null
- val hiddenChannelIdsByProvider = sourceContext.providerIds.associateWith { providerId ->
- mainActivity.preferencesRepository.getHiddenChannelIds(providerId).first()
- }
- for (channelId in channelIds.distinct()) {
- val channel = mainActivity.channelRepository.getChannel(channelId) ?: continue
- if (channel.providerId !in sourceContext.providerIds) continue
- if (channel.id in hiddenChannelIdsByProvider[channel.providerId].orEmpty()) continue
- return channel.toLivePlayerRequest(
- categoryId = virtualCategoryId,
- providerId = channel.providerId,
- isVirtual = true,
- combinedProfileId = (sourceContext as? LiveStartupContext.Combined)?.profileId,
- returnDestination = AppDestination.LiveTv()
- )
- }
- return null
-}
-
-private suspend fun resolveLiveStartupContext(
- mainActivity: MainActivity
-): LiveStartupContext? {
- return when (val activeSource = mainActivity.combinedM3uRepository.getActiveLiveSource().first()) {
- is ActiveLiveSource.ProviderSource -> LiveStartupContext.Provider(activeSource.providerId)
- is ActiveLiveSource.CombinedM3uSource -> {
- val providerIds = mainActivity.combinedM3uRepository.getProfile(activeSource.profileId)
- ?.members
- .orEmpty()
- .filter { it.enabled }
- .map { it.providerId }
- .distinct()
- if (providerIds.isEmpty()) null else LiveStartupContext.Combined(activeSource.profileId, providerIds)
- }
- null -> {
- mainActivity.providerRepository.getActiveProvider().first()?.id?.let { providerId ->
- LiveStartupContext.Provider(providerId)
- }
- }
- }
-}
-
-private sealed interface LiveStartupContext {
- val providerIds: List<Long>
-
- data class Provider(val providerId: Long) : LiveStartupContext {
- override val providerIds: List<Long> = listOf(providerId)
- }
-
- data class Combined(val profileId: Long, override val providerIds: List<Long>) : LiveStartupContext
-}
-
+): PlayerNavigationRequest? = mainActivity.startupNavigationResolver
+ .resolve(landingDestination)
+ .playerRequest
diff --git a/app/src/main/java/com/streamvault/app/navigation/AppNavigationState.kt b/app/src/main/java/com/streamvault/app/navigation/AppNavigationState.kt
new file mode 100644
index 00000000..d68c4525
--- /dev/null
+++ b/app/src/main/java/com/streamvault/app/navigation/AppNavigationState.kt
@@ -0,0 +1,13 @@
+package com.streamvault.app.navigation
+
+import com.streamvault.domain.model.AppTopLevelDestination
+import com.streamvault.domain.model.CatalogLayout
+import com.streamvault.domain.model.ContentType
+
+data class AppNavigationState(
+ val startupTarget: StartupNavigationTarget? = null,
+ val topLevelDestinations: List<AppTopLevelDestination> = AppTopLevelDestination.defaultOrder,
+ val catalogLayout: CatalogLayout? = null,
+ val lastSplitCatalogType: ContentType = ContentType.MOVIE,
+ val splitPreferenceReady: Boolean = false
+)
diff --git a/app/src/main/java/com/streamvault/app/navigation/StartupNavigationResolver.kt b/app/src/main/java/com/streamvault/app/navigation/StartupNavigationResolver.kt
new file mode 100644
index 00000000..dbb59973
--- /dev/null
+++ b/app/src/main/java/com/streamvault/app/navigation/StartupNavigationResolver.kt
@@ -0,0 +1,137 @@
+package com.streamvault.app.navigation
+
+import com.streamvault.core.navigation.AppDestination
+import com.streamvault.core.navigation.PlayerNavigationRequest
+import com.streamvault.data.preferences.PreferencesRepository
+import com.streamvault.domain.model.ActiveLiveSource
+import com.streamvault.domain.model.AppLandingDestination
+import com.streamvault.domain.model.ContentType
+import com.streamvault.domain.model.VirtualCategoryIds
+import com.streamvault.domain.repository.ChannelRepository
+import com.streamvault.domain.repository.CombinedM3uRepository
+import com.streamvault.domain.repository.FavoriteRepository
+import com.streamvault.domain.repository.PlaybackHistoryRepository
+import com.streamvault.domain.repository.ProviderRepository
+import javax.inject.Inject
+import kotlinx.coroutines.flow.first
+
+data class StartupNavigationTarget(
+ val destination: AppDestination,
+ val playerRequest: PlayerNavigationRequest? = null
+)
+
+class StartupNavigationResolver @Inject constructor(
+ private val preferencesRepository: PreferencesRepository,
+ private val combinedM3uRepository: CombinedM3uRepository,
+ private val favoriteRepository: FavoriteRepository,
+ private val playbackHistoryRepository: PlaybackHistoryRepository,
+ private val channelRepository: ChannelRepository,
+ private val providerRepository: ProviderRepository
+) {
+ suspend fun resolve(landingDestination: AppLandingDestination): StartupNavigationTarget {
+ val destination = landingDestination.toAppDestination()
+ val playerRequest = when (landingDestination) {
+ AppLandingDestination.FIRST_FAVORITE_LIVE -> resolveFirstFavoriteStartupTarget()
+ AppLandingDestination.LAST_WATCHED_LIVE -> resolveLastWatchedStartupTarget()
+ else -> null
+ }
+ return StartupNavigationTarget(destination, playerRequest)
+ }
+
+ private suspend fun resolveFirstFavoriteStartupTarget(): PlayerNavigationRequest? {
+ if (!preferencesRepository.showFavoritesCategory.first()) return null
+ val context = resolveLiveStartupContext() ?: return null
+ val favorites = when (context) {
+ is LiveStartupContext.Provider ->
+ favoriteRepository.getFavorites(context.providerId, ContentType.LIVE).first()
+ is LiveStartupContext.Combined ->
+ favoriteRepository.getFavorites(context.providerIds, ContentType.LIVE).first()
+ }.sortedBy { it.position }
+ return resolveStartupChannelTarget(
+ channelIds = favorites.map { it.contentId },
+ sourceContext = context,
+ virtualCategoryId = VirtualCategoryIds.FAVORITES
+ )
+ }
+
+ private suspend fun resolveLastWatchedStartupTarget(): PlayerNavigationRequest? {
+ val context = resolveLiveStartupContext() ?: return null
+ val recentHistory = when (context) {
+ is LiveStartupContext.Provider ->
+ playbackHistoryRepository.getRecentlyWatchedByProvider(context.providerId, limit = 24).first()
+ is LiveStartupContext.Combined ->
+ playbackHistoryRepository.getRecentlyWatchedByProviders(context.providerIds.toSet(), limit = 24).first()
+ }
+ return resolveStartupChannelTarget(
+ channelIds = recentHistory
+ .filter { it.contentType == ContentType.LIVE }
+ .sortedByDescending { it.lastWatchedAt }
+ .map { it.contentId },
+ sourceContext = context,
+ virtualCategoryId = VirtualCategoryIds.RECENT
+ )
+ }
+
+ private suspend fun resolveStartupChannelTarget(
+ channelIds: List<Long>,
+ sourceContext: LiveStartupContext,
+ virtualCategoryId: Long
+ ): PlayerNavigationRequest? {
+ if (channelIds.isEmpty()) return null
+ val hiddenChannelIdsByProvider = sourceContext.providerIds.associateWith { providerId ->
+ preferencesRepository.getHiddenChannelIds(providerId).first()
+ }
+ for (channelId in channelIds.distinct()) {
+ val channel = channelRepository.getChannel(channelId) ?: continue
+ if (channel.providerId !in sourceContext.providerIds) continue
+ if (channel.id in hiddenChannelIdsByProvider[channel.providerId].orEmpty()) continue
+ return channel.toLivePlayerRequest(
+ categoryId = virtualCategoryId,
+ providerId = channel.providerId,
+ isVirtual = true,
+ combinedProfileId = (sourceContext as? LiveStartupContext.Combined)?.profileId,
+ returnDestination = AppDestination.LiveTv()
+ )
+ }
+ return null
+ }
+
+ private suspend fun resolveLiveStartupContext(): LiveStartupContext? = when (
+ val activeSource = combinedM3uRepository.getActiveLiveSource().first()
+ ) {
+ is ActiveLiveSource.ProviderSource -> LiveStartupContext.Provider(activeSource.providerId)
+ is ActiveLiveSource.CombinedM3uSource -> {
+ val providerIds = combinedM3uRepository.getProfile(activeSource.profileId)
+ ?.members
+ .orEmpty()
+ .filter { it.enabled }
+ .map { it.providerId }
+ .distinct()
+ if (providerIds.isEmpty()) null else LiveStartupContext.Combined(activeSource.profileId, providerIds)
+ }
+ null -> providerRepository.getActiveProvider().first()?.id?.let(LiveStartupContext::Provider)
+ }
+
+ private sealed interface LiveStartupContext {
+ val providerIds: List<Long>
+
+ data class Provider(val providerId: Long) : LiveStartupContext {
+ override val providerIds: List<Long> = listOf(providerId)
+ }
+
+ data class Combined(val profileId: Long, override val providerIds: List<Long>) : LiveStartupContext
+ }
+}
+
+private fun AppLandingDestination.toAppDestination(): AppDestination = when (this) {
+ AppLandingDestination.HOME -> AppDestination.Home
+ AppLandingDestination.LIVE_TV,
+ AppLandingDestination.FIRST_FAVORITE_LIVE,
+ AppLandingDestination.LAST_WATCHED_LIVE -> AppDestination.LiveTv()
+ AppLandingDestination.MOVIES -> AppDestination.Movies
+ AppLandingDestination.SERIES -> AppDestination.Series
+ AppLandingDestination.GUIDE -> AppDestination.Guide()
+ AppLandingDestination.DOWNLOADS -> AppDestination.Downloads
+ AppLandingDestination.PLUGINS -> AppDestination.Plugins
+ AppLandingDestination.SETTINGS -> AppDestination.Settings()
+}
diff --git a/app/src/test/java/com/streamvault/app/navigation/AppNavigationCoordinatorTest.kt b/app/src/test/java/com/streamvault/app/navigation/AppNavigationCoordinatorTest.kt
index 0adc2a8a..e6a9b619 100644
--- a/app/src/test/java/com/streamvault/app/navigation/AppNavigationCoordinatorTest.kt
+++ b/app/src/test/java/com/streamvault/app/navigation/AppNavigationCoordinatorTest.kt
@@ -3,13 +3,45 @@ package com.streamvault.app.navigation
import com.google.common.truth.Truth.assertThat
import com.streamvault.core.navigation.AppDestination
import com.streamvault.core.navigation.ExternalNavigationRequest
+import com.streamvault.data.preferences.PreferencesRepository
+import com.streamvault.domain.model.ActiveLiveSource
+import com.streamvault.domain.model.AppLandingDestination
+import com.streamvault.domain.model.AppTopLevelDestination
+import com.streamvault.domain.model.Channel
+import com.streamvault.domain.model.ContentType
+import com.streamvault.domain.model.Favorite
+import com.streamvault.domain.repository.ChannelRepository
+import com.streamvault.domain.repository.CombinedM3uRepository
+import com.streamvault.domain.repository.FavoriteRepository
+import com.streamvault.domain.repository.PlaybackHistoryRepository
+import com.streamvault.domain.repository.ProviderRepository
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Before
import org.junit.Test
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
class AppNavigationCoordinatorTest {
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(Dispatchers.Unconfined)
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
@Test
- fun commandIsClearedOnlyAfterMatchingAcknowledgement() {
+ fun commandIsClearedOnlyAfterMatchingAcknowledgement() = runTest {
val ids = sequenceOf(10L, 11L).iterator()
- val coordinator = AppNavigationCoordinator(NavigationCommandIdSource { ids.next() })
+ val coordinator = coordinator(NavigationCommandIdSource { ids.next() })
coordinator.submitExternalRequest(ExternalNavigationRequest.Search("news"))
val pending = coordinator.pendingCommand.value
@@ -21,9 +53,9 @@ class AppNavigationCoordinatorTest {
}
@Test
- fun queuedCommandsArePublishedInSubmissionOrder() {
+ fun queuedCommandsArePublishedInSubmissionOrder() = runTest {
val ids = sequenceOf(10L, 11L).iterator()
- val coordinator = AppNavigationCoordinator(NavigationCommandIdSource { ids.next() })
+ val coordinator = coordinator(NavigationCommandIdSource { ids.next() })
coordinator.submitExternalRequest(ExternalNavigationRequest.Search("news"))
coordinator.submitExternalRequest(ExternalNavigationRequest.Destination(AppDestination.Home))
@@ -36,4 +68,72 @@ class AppNavigationCoordinatorTest {
ExternalNavigationRequest.Destination(AppDestination.Home).toNavigationCommand()
)
}
+
+ @Test
+ fun repeatedStartupRequestsSubmitOnlyOneCommandAndOpenPlayerAfterLiveResumes() = runTest {
+ val ids = sequenceOf(10L, 11L, 12L).iterator()
+ val coordinator = coordinator(
+ NavigationCommandIdSource { ids.next() },
+ AppLandingDestination.FIRST_FAVORITE_LIVE
+ )
+ coordinator.requestStartupNavigation(AppDestination.Welcome)
+ coordinator.requestStartupNavigation(AppDestination.Welcome)
+
+ assertThat(coordinator.pendingCommand.value?.id).isEqualTo(10L)
+ coordinator.acknowledge(10L)
+ coordinator.onDestinationResumed(AppDestination.Home)
+ assertThat(coordinator.pendingCommand.value).isNull()
+ coordinator.onDestinationResumed(AppDestination.LiveTv())
+ assertThat(coordinator.pendingCommand.value?.id).isEqualTo(11L)
+ coordinator.onDestinationResumed(AppDestination.LiveTv())
+ assertThat(coordinator.pendingCommand.value?.id).isEqualTo(11L)
+ }
+
+ private fun coordinator(
+ ids: NavigationCommandIdSource,
+ startupLanding: AppLandingDestination = AppLandingDestination.HOME
+ ): AppNavigationCoordinator {
+ val preferencesRepository = mock<PreferencesRepository>()
+ whenever(preferencesRepository.appLandingDestination)
+ .thenReturn(flowOf(startupLanding))
+ whenever(preferencesRepository.appTopLevelDestinations)
+ .thenReturn(flowOf(AppTopLevelDestination.defaultOrder))
+ whenever(preferencesRepository.showFavoritesCategory).thenReturn(flowOf(true))
+ val providerRepository = mock<ProviderRepository>()
+ whenever(providerRepository.getActiveProvider()).thenReturn(flowOf(null))
+ val combinedM3uRepository = mock<CombinedM3uRepository>()
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.ProviderSource(7L))
+ )
+ val favoriteRepository = mock<FavoriteRepository>()
+ whenever(favoriteRepository.getFavorites(7L, ContentType.LIVE)).thenReturn(
+ flowOf(listOf(Favorite(providerId = 7L, contentId = 42L, contentType = ContentType.LIVE)))
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(emptySet()))
+ val channelRepository = mock<ChannelRepository>()
+ runBlocking {
+ whenever(channelRepository.getChannel(42L)).thenReturn(
+ Channel(
+ id = 42L,
+ name = "News",
+ streamUrl = "https://example.com/live.m3u8",
+ providerId = 7L
+ )
+ )
+ }
+ val startupResolver = StartupNavigationResolver(
+ preferencesRepository = preferencesRepository,
+ combinedM3uRepository = combinedM3uRepository,
+ favoriteRepository = favoriteRepository,
+ playbackHistoryRepository = mock<PlaybackHistoryRepository>(),
+ channelRepository = channelRepository,
+ providerRepository = providerRepository
+ )
+ return AppNavigationCoordinator(
+ commandIds = ids,
+ startupResolver = startupResolver,
+ preferencesRepository = preferencesRepository,
+ providerRepository = providerRepository
+ )
+ }
}
diff --git a/app/src/test/java/com/streamvault/app/navigation/CatalogRouteResolverTest.kt b/app/src/test/java/com/streamvault/app/navigation/CatalogRouteResolverTest.kt
index 3064a7ea..012cb07e 100644
--- a/app/src/test/java/com/streamvault/app/navigation/CatalogRouteResolverTest.kt
+++ b/app/src/test/java/com/streamvault/app/navigation/CatalogRouteResolverTest.kt
@@ -1,6 +1,7 @@
package com.streamvault.app.navigation
import com.google.common.truth.Truth.assertThat
+import com.streamvault.core.navigation.AppDestination
import com.streamvault.domain.model.CatalogLayout
import com.streamvault.domain.model.ContentType
import org.junit.Test
@@ -8,17 +9,17 @@ import org.junit.Test
class CatalogRouteResolverTest {
@Test
fun unifiedAndUnknownLayoutsUseVodRoute() {
- assertThat(resolveCatalogRoute(CatalogLayout.UNIFIED_VOD, Routes.MOVIES, ContentType.MOVIE, true))
- .isEqualTo(Routes.VOD)
- assertThat(resolveCatalogRoute(CatalogLayout.UNKNOWN, Routes.SERIES, ContentType.SERIES, true))
- .isEqualTo(Routes.VOD)
+ assertThat(resolveCatalogDestination(CatalogLayout.UNIFIED_VOD, AppDestination.Movies, ContentType.MOVIE, true))
+ .isEqualTo(AppDestination.Vod)
+ assertThat(resolveCatalogDestination(CatalogLayout.UNKNOWN, AppDestination.Series, ContentType.SERIES, true))
+ .isEqualTo(AppDestination.Vod)
}
@Test
fun splitLayoutWaitsForDestinationPreferenceBeforeRedirecting() {
- assertThat(resolveCatalogRoute(CatalogLayout.SPLIT, Routes.VOD, ContentType.SERIES, false))
- .isEqualTo(Routes.VOD)
- assertThat(resolveCatalogRoute(CatalogLayout.SPLIT, Routes.VOD, ContentType.SERIES, true))
- .isEqualTo(Routes.SERIES)
+ assertThat(resolveCatalogDestination(CatalogLayout.SPLIT, AppDestination.Vod, ContentType.SERIES, false))
+ .isEqualTo(AppDestination.Vod)
+ assertThat(resolveCatalogDestination(CatalogLayout.SPLIT, AppDestination.Vod, ContentType.SERIES, true))
+ .isEqualTo(AppDestination.Series)
}
}
diff --git a/app/src/test/java/com/streamvault/app/navigation/StartupNavigationResolverTest.kt b/app/src/test/java/com/streamvault/app/navigation/StartupNavigationResolverTest.kt
new file mode 100644
index 00000000..a86b3e6d
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/navigation/StartupNavigationResolverTest.kt
@@ -0,0 +1,189 @@
+package com.streamvault.app.navigation
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.data.preferences.PreferencesRepository
+import com.streamvault.domain.model.ActiveLiveSource
+import com.streamvault.domain.model.AppLandingDestination
+import com.streamvault.domain.model.Channel
+import com.streamvault.domain.model.CombinedM3uProfile
+import com.streamvault.domain.model.CombinedM3uProfileMember
+import com.streamvault.domain.model.ContentType
+import com.streamvault.domain.model.Favorite
+import com.streamvault.domain.model.PlaybackHistory
+import com.streamvault.domain.repository.ChannelRepository
+import com.streamvault.domain.repository.CombinedM3uRepository
+import com.streamvault.domain.repository.FavoriteRepository
+import com.streamvault.domain.repository.PlaybackHistoryRepository
+import com.streamvault.domain.repository.ProviderRepository
+import com.streamvault.domain.model.VirtualCategoryIds
+import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.test.runTest
+import org.junit.Test
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
+
+class StartupNavigationResolverTest {
+ private val preferencesRepository = mock<PreferencesRepository>()
+ private val combinedM3uRepository = mock<CombinedM3uRepository>()
+ private val favoriteRepository = mock<FavoriteRepository>()
+ private val playbackHistoryRepository = mock<PlaybackHistoryRepository>()
+ private val channelRepository = mock<ChannelRepository>()
+ private val providerRepository = mock<ProviderRepository>()
+ private val resolver = StartupNavigationResolver(
+ preferencesRepository = preferencesRepository,
+ combinedM3uRepository = combinedM3uRepository,
+ favoriteRepository = favoriteRepository,
+ playbackHistoryRepository = playbackHistoryRepository,
+ channelRepository = channelRepository,
+ providerRepository = providerRepository
+ )
+
+ @Test
+ fun homeReturnsTypedLandingWithoutPlayer() = runTest {
+ val result = resolver.resolve(AppLandingDestination.HOME)
+
+ assertThat(result.destination).isEqualTo(com.streamvault.core.navigation.AppDestination.Home)
+ assertThat(result.playerRequest).isNull()
+ }
+
+ @Test
+ fun firstFavoriteProducesVirtualLiveRequestReturningToLive() = runTest {
+ whenever(preferencesRepository.showFavoritesCategory).thenReturn(flowOf(true))
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.ProviderSource(7L))
+ )
+ whenever(favoriteRepository.getFavorites(7L, ContentType.LIVE)).thenReturn(
+ flowOf(listOf(Favorite(providerId = 7L, contentId = 42L, contentType = ContentType.LIVE)))
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(emptySet()))
+ whenever(channelRepository.getChannel(42L)).thenReturn(
+ Channel(
+ id = 42L,
+ name = "News",
+ streamUrl = "https://example.com/live.m3u8",
+ providerId = 7L
+ )
+ )
+
+ val result = resolver.resolve(AppLandingDestination.FIRST_FAVORITE_LIVE)
+
+ assertThat(result.destination).isEqualTo(com.streamvault.core.navigation.AppDestination.LiveTv())
+ assertThat(result.playerRequest?.internalId).isEqualTo(42L)
+ assertThat(result.playerRequest?.categoryId).isEqualTo(VirtualCategoryIds.FAVORITES)
+ assertThat(result.playerRequest?.returnDestination)
+ .isEqualTo(com.streamvault.core.navigation.AppDestination.LiveTv())
+ }
+
+ @Test
+ fun lastWatchedSelectsMostRecentLiveChannel() = runTest {
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.ProviderSource(7L))
+ )
+ whenever(playbackHistoryRepository.getRecentlyWatchedByProvider(7L, 24)).thenReturn(
+ flowOf(
+ listOf(
+ history(41L, 100L),
+ history(42L, 200L)
+ )
+ )
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(emptySet()))
+ whenever(channelRepository.getChannel(42L)).thenReturn(channel(42L, 7L))
+
+ val result = resolver.resolve(AppLandingDestination.LAST_WATCHED_LIVE)
+
+ assertThat(result.playerRequest?.internalId).isEqualTo(42L)
+ }
+
+ @Test
+ fun hiddenFavoriteIsSkippedWhenNoVisibleCandidateRemains() = runTest {
+ whenever(preferencesRepository.showFavoritesCategory).thenReturn(flowOf(true))
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.ProviderSource(7L))
+ )
+ whenever(favoriteRepository.getFavorites(7L, ContentType.LIVE)).thenReturn(
+ flowOf(listOf(Favorite(providerId = 7L, contentId = 42L, contentType = ContentType.LIVE)))
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(setOf(42L)))
+ whenever(channelRepository.getChannel(42L)).thenReturn(channel(42L, 7L))
+
+ assertThat(resolver.resolve(AppLandingDestination.FIRST_FAVORITE_LIVE).playerRequest).isNull()
+ }
+
+ @Test
+ fun combinedSourceAcceptsOnlyEnabledMembersAndCarriesProfileId() = runTest {
+ whenever(preferencesRepository.showFavoritesCategory).thenReturn(flowOf(true))
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.CombinedM3uSource(99L))
+ )
+ whenever(combinedM3uRepository.getProfile(99L)).thenReturn(
+ CombinedM3uProfile(
+ id = 99L,
+ name = "Combined",
+ members = listOf(
+ CombinedM3uProfileMember(profileId = 99L, providerId = 7L, priority = 0, enabled = true),
+ CombinedM3uProfileMember(profileId = 99L, providerId = 8L, priority = 1, enabled = true),
+ CombinedM3uProfileMember(profileId = 99L, providerId = 9L, priority = 2, enabled = false)
+ )
+ )
+ )
+ whenever(favoriteRepository.getFavorites(listOf(7L, 8L), ContentType.LIVE)).thenReturn(
+ flowOf(
+ listOf(
+ Favorite(providerId = 9L, contentId = 90L, contentType = ContentType.LIVE),
+ Favorite(providerId = 8L, contentId = 80L, contentType = ContentType.LIVE)
+ )
+ )
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(emptySet()))
+ whenever(preferencesRepository.getHiddenChannelIds(8L)).thenReturn(flowOf(emptySet()))
+ whenever(channelRepository.getChannel(80L)).thenReturn(channel(80L, 8L))
+
+ val result = resolver.resolve(AppLandingDestination.FIRST_FAVORITE_LIVE)
+
+ assertThat(result.playerRequest?.internalId).isEqualTo(80L)
+ assertThat(result.playerRequest?.providerId).isEqualTo(8L)
+ assertThat(result.playerRequest?.combinedProfileId).isEqualTo(99L)
+ }
+
+ @Test
+ fun noActiveSourceOrProviderReturnsTypedLandingWithoutPlayer() = runTest {
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(flowOf(null))
+ whenever(providerRepository.getActiveProvider()).thenReturn(flowOf(null))
+
+ val result = resolver.resolve(AppLandingDestination.LAST_WATCHED_LIVE)
+
+ assertThat(result.destination).isEqualTo(com.streamvault.core.navigation.AppDestination.LiveTv())
+ assertThat(result.playerRequest).isNull()
+ }
+
+ @Test
+ fun missingChannelCandidateProducesNoPlayerRequest() = runTest {
+ whenever(combinedM3uRepository.getActiveLiveSource()).thenReturn(
+ flowOf(ActiveLiveSource.ProviderSource(7L))
+ )
+ whenever(playbackHistoryRepository.getRecentlyWatchedByProvider(7L, 24)).thenReturn(
+ flowOf(listOf(history(42L, 200L)))
+ )
+ whenever(preferencesRepository.getHiddenChannelIds(7L)).thenReturn(flowOf(emptySet()))
+ whenever(channelRepository.getChannel(42L)).thenReturn(null)
+
+ assertThat(resolver.resolve(AppLandingDestination.LAST_WATCHED_LIVE).playerRequest).isNull()
+ }
+
+ private fun channel(id: Long, providerId: Long) = Channel(
+ id = id,
+ name = "Channel $id",
+ streamUrl = "https://example.com/$id.m3u8",
+ providerId = providerId
+ )
+
+ private fun history(contentId: Long, lastWatchedAt: Long) = PlaybackHistory(
+ contentId = contentId,
+ contentType = ContentType.LIVE,
+ providerId = 7L,
+ title = "Channel $contentId",
+ streamUrl = "https://example.com/$contentId.m3u8",
+ lastWatchedAt = lastWatchedAt
+ )
+}