Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ import org.siloserver.silo.model.download.DownloadQuality
import org.siloserver.silo.model.feature.CLIENT_WATCH_TOGETHER_SURFACE_ENABLED
import org.siloserver.silo.common.settings.PlayerSettingsStore
import org.siloserver.silo.network.ServerRegistry
import org.siloserver.silo.playback.selectPlaybackVersion
import org.koin.compose.koinInject
import org.koin.compose.viewmodel.koinViewModel
import org.siloserver.silo.metadata.DescriptionTranslationPhase
Expand Down Expand Up @@ -621,8 +620,18 @@ fun ItemDetailScreen(
it.contentId == state.selectedEpisodeContentId
}
val selectedEpisodeDetail = state.selectedEpisodeDetail
val selectedEpisodeVersionIndex = state.selectedVersionIndex
.coerceIn(0, (selectedEpisodeDetail?.versions?.lastIndex ?: 0).coerceAtLeast(0))
// Auto resolves like the movie page and playback, so
// the version shown is the one Play and Download use.
val selectedEpisodeVersions = selectedEpisodeDetail?.versions.orEmpty()
val selectedEpisodeVersionIndex = detailDisplayVersionIndex(
versions = selectedEpisodeVersions,
explicitIndex = state.selectedVersionIndex
.coerceIn(0, selectedEpisodeVersions.lastIndex.coerceAtLeast(0))
.takeIf { state.hasExplicitVersionSelection },
lastFileId = selectedEpisodeDetail?.userData?.lastFileId,
preferredQuality = preferredQuality,
fallbackIndex = 0,
)
val selectedEpisodeFileId = selectedEpisodeDetail?.versions
?.getOrNull(selectedEpisodeVersionIndex)
?.fileId
Expand Down Expand Up @@ -821,41 +830,18 @@ fun ItemDetailScreen(
// flow through to the DownloadButton.
val downloadRecords by viewModel.downloads.collectAsState()
// Auto preview must resolve through the SAME shared
// selector as playback (lastFileId → preferred-quality
// rank → bestAvailable), not just lastFileId-else-[0] —
// selector as playback, not just lastFileId-else-[0] —
// otherwise the previewed version (and the audio/subtitle
// lists derived from it) can describe a file playback
// won't use. An explicit user pick still wins. TV parity:
// selectTvDetailDisplayVersion does the same.
val videoDisplayVersionIndex = if (
state.hasExplicitVersionSelection || detail.versions.isEmpty()
) {
effectiveSelectedVersionIndex
} else if (preferredQuality == null) {
// The quality pref hasn't emitted from DataStore yet
// (a frame or two): don't auto-resolve against a
// missing pref — it would name a version the arriving
// pref immediately contradicts (first-frame flash).
// lastFileId is pref-independent and always wins in
// selectPlaybackVersion, so it can be shown at once;
// otherwise hold the bare "Auto" placeholder (-1 →
// no resolved version) until the pref lands.
detail.userData?.lastFileId
?.let { lastFileId ->
detail.versions.indexOfFirst { it.fileId == lastFileId }
.takeIf { it >= 0 }
}
?: -1
} else {
val resolvedFileId = selectPlaybackVersion(
detail.versions,
detail.userData?.lastFileId,
preferredQuality,
).fileId
detail.versions.indexOfFirst { it.fileId == resolvedFileId }
.takeIf { it >= 0 }
?: effectiveSelectedVersionIndex
}
// won't use. TV parity: selectTvDetailDisplayVersion
// does the same.
val videoDisplayVersionIndex = detailDisplayVersionIndex(
versions = detail.versions,
explicitIndex = effectiveSelectedVersionIndex.takeIf { state.hasExplicitVersionSelection },
lastFileId = detail.userData?.lastFileId,
preferredQuality = preferredQuality,
fallbackIndex = effectiveSelectedVersionIndex,
)
val selectedVersion = detail.versions.getOrNull(videoDisplayVersionIndex)
val selectedLocalDownload = selectedVersion?.let { version ->
localDownloadFor(version.fileId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import org.siloserver.silo.model.catalog.hasEditionChoices
import org.siloserver.silo.model.catalog.FileVersion
import org.siloserver.silo.model.catalog.SubtitleTrack
import org.siloserver.silo.player.DolbyVisionDetection
import org.siloserver.silo.playback.selectPlaybackVersion

/**
* One iOS-style row inside [PlaybackSelectorCard]. Icon + group label lead,
Expand Down Expand Up @@ -520,6 +521,38 @@ private fun BadgePill(text: String) {
)
}

// ── Display version ───────────────────────────────────────────

/**
* Index of the version a video detail page shows for [versions], which is
* also the version its Download button saves. An explicit pick wins. Auto
* resolves through the same shared selector as playback (lastFileId →
* preferred-quality rank → bestAvailable), so what Auto previews is the file
* playback and downloads use. While the quality pref is still loading, only
* the pref-independent lastFileId is shown, otherwise -1 (the bare "Auto"
* placeholder), so the page never names a version the arriving pref
* contradicts. [fallbackIndex] covers an empty list or an unmatched version.
*/
internal fun detailDisplayVersionIndex(
versions: List<FileVersion>,
explicitIndex: Int?,
lastFileId: Int?,
preferredQuality: String?,
fallbackIndex: Int,
): Int {
if (versions.isEmpty()) return fallbackIndex
// A stale pick (the list shrank after a refresh) names no version rather
// than a different one, since playback would not use a clamped file.
if (explicitIndex != null) return explicitIndex.takeIf { it in versions.indices } ?: -1
if (preferredQuality == null) {
return lastFileId
?.let { id -> versions.indexOfFirst { it.fileId == id }.takeIf { it >= 0 } }
?: -1
}
val resolvedFileId = selectPlaybackVersion(versions, lastFileId, preferredQuality).fileId
return versions.indexOfFirst { it.fileId == resolvedFileId }.takeIf { it >= 0 } ?: fallbackIndex
}

// ── Label formatting ──────────────────────────────────────────

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.siloserver.silo.android.ui.screens.detail

import org.siloserver.silo.model.catalog.FileVersion
import kotlin.test.Test
import kotlin.test.assertEquals

class DetailDisplayVersionTest {
// The 1080p file is listed first; the 720p file is the one with subtitles
// the profile last played.
private val versions = listOf(
FileVersion(fileId = 1080, resolution = "1080p"),
FileVersion(fileId = 720, resolution = "720p"),
)

@Test
fun `auto shows and downloads the last played version, not the first listed`() {
val index = detailDisplayVersionIndex(
versions = versions,
explicitIndex = null,
lastFileId = 720,
preferredQuality = "auto",
fallbackIndex = 0,
)

assertEquals(720, versions[index].fileId)
}

@Test
fun `an explicit pick wins over the last played version`() {
val index = detailDisplayVersionIndex(
versions = versions,
explicitIndex = 0,
lastFileId = 720,
preferredQuality = "auto",
fallbackIndex = 0,
)

assertEquals(1080, versions[index].fileId)
}

@Test
fun `auto names no version until the quality preference loads`() {
val index = detailDisplayVersionIndex(
versions = versions,
explicitIndex = null,
lastFileId = null,
preferredQuality = null,
fallbackIndex = 0,
)

assertEquals(-1, index)
}

@Test
fun `an explicit pick with no versions loaded yields the fallback`() {
val index = detailDisplayVersionIndex(
versions = emptyList(),
explicitIndex = 1,
lastFileId = null,
preferredQuality = "auto",
fallbackIndex = 0,
)

assertEquals(0, index)
}

@Test
fun `a stale explicit pick names no version instead of another one`() {
val index = detailDisplayVersionIndex(
versions = versions,
explicitIndex = 5,
lastFileId = 720,
preferredQuality = "auto",
fallbackIndex = 0,
)

assertEquals(-1, index)
}
}
Loading