Skip to content
Open
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 @@ -268,18 +268,21 @@ class AndroidPlayerSettingsStore(
}

/**
* Whether the active server's manifest defines
* `playback.buffering_quality_mode`: null until its manifest is read.
* Whether the active server's manifest defines [key], one of
* [PlaybackSettingsKeys.ManifestDetectedKeys]: null until its manifest is read.
*/
private val bufferingQualityModeDefined: Flow<Boolean?> =
private fun manifestDefines(key: String): Flow<Boolean?> =
if (contractRevision == null) {
flowOf(true)
} else {
combine(currentScopeFlow, contractRevision.known) { scope, _ ->
scope?.let { contractRevision.stores(it.serverUrl, PlaybackSettingsKeys.BufferingQualityMode) }
scope?.let { contractRevision.stores(it.serverUrl, key) }
}.distinctUntilChanged()
}

private val bufferingQualityModeDefined: Flow<Boolean?> =
manifestDefines(PlaybackSettingsKeys.BufferingQualityMode)

// No legacy boolean behind this one: an absent or unknown value is the
// contract default. Until the server's manifest is read, the last mode
// seen here applies; a server without the setting plays the default, and
Expand All @@ -297,6 +300,21 @@ class AndroidPlayerSettingsStore(
override val bufferingQualityModeSupportedFlow: Flow<Boolean> =
bufferingQualityModeDefined.map { it == true }.distinctUntilChanged()

private val nextUpEnabledDefined: Flow<Boolean?> =
manifestDefines(PlaybackSettingsKeys.NextUpEnabled)

// As with the buffering mode: until the manifest is read the last choice
// seen applies, and a server without the setting keeps Next Up on.
override val nextUpEnabledFlow: Flow<Boolean> =
combine(
profileScopedFlow(true) { p, s -> p.boolFor(s, PlaybackSettingsKeys.NextUpEnabled, true) },
nextUpEnabledDefined,
) { enabled, defined -> enabled || defined == false }
.distinctUntilChanged()

override val nextUpEnabledSupportedFlow: Flow<Boolean> =
nextUpEnabledDefined.map { it == true }.distinctUntilChanged()

// ---- Booleans ------------------------------------------------------
override val autoSkipCreditsFlow: Flow<Boolean> =
profileScopedFlow(false) { p, s -> p.boolFor(s, PlaybackSettingsKeys.AutoSkipCredits, false) }
Expand Down Expand Up @@ -525,22 +543,10 @@ class AndroidPlayerSettingsStore(

/**
* Settings offers this only while the server's manifest defines the key
* ([bufferingQualityModeSupportedFlow]). Like [setCellularQuality] it is
* marked unconfirmed, so a write the flusher held while the manifest was
* unknown is sent again by the first refresh rather than replaced with
* the server's default.
* ([bufferingQualityModeSupportedFlow]); [writeString] marks it unconfirmed.
*/
override suspend fun setBufferingQualityMode(value: BufferingQualityMode) {
val key = PlaybackSettingsKeys.BufferingQualityMode
withScope { scope, store ->
store.edit {
it[stringPreferencesKey(scope.keyPrefix + key)] = value.wireValue
markDeviceOverride(it, scope, key)
it[unconfirmedKeysKey(scope)] = it[unconfirmedKeysKey(scope)].orEmpty() + key
}
serverSettingsFlusher.enqueue(scope.profileId, key, value.wireValue, scope.serverUrl, scope.authority)
}
}
override suspend fun setBufferingQualityMode(value: BufferingQualityMode) =
writeString(PlaybackSettingsKeys.BufferingQualityMode, value.wireValue)

override suspend fun setAutoSkipCredits(value: Boolean) =
writeBool(PlaybackSettingsKeys.AutoSkipCredits, value)
Expand Down Expand Up @@ -677,6 +683,9 @@ class AndroidPlayerSettingsStore(
override suspend fun setNextUpPromptSeconds(value: Int) =
writeInt(PlaybackSettingsKeys.NextUpPromptSeconds, value.coerceIn(0, 120))

override suspend fun setNextUpEnabled(value: Boolean) =
writeBool(PlaybackSettingsKeys.NextUpEnabled, value)

override suspend fun setResumeRewindSeconds(value: Int) =
writeIntLocal(PlaybackSettingsKeys.ResumeRewindSeconds, value.coerceIn(0, 30))

Expand Down Expand Up @@ -1240,6 +1249,7 @@ class AndroidPlayerSettingsStore(
store.edit {
it[booleanPreferencesKey(scope.keyPrefix + key)] = value
markDeviceOverride(it, scope, key)
markUnconfirmedIfManifestDetected(it, scope, key)
}
serverSettingsFlusher.enqueue(scope.profileId, key, value.toString(), scope.serverUrl, scope.authority)
}
Expand Down Expand Up @@ -1284,6 +1294,7 @@ class AndroidPlayerSettingsStore(
store.edit {
it[stringPreferencesKey(scope.keyPrefix + key)] = value
markDeviceOverride(it, scope, key)
markUnconfirmedIfManifestDetected(it, scope, key)
}
serverSettingsFlusher.enqueue(scope.profileId, key, value, scope.serverUrl, scope.authority)
}
Expand Down Expand Up @@ -1330,6 +1341,22 @@ class AndroidPlayerSettingsStore(
private fun deviceOverridesKey(scope: Scope): Preferences.Key<Set<String>> =
stringSetPreferencesKey(scope.keyPrefix + DEVICE_OVERRIDE_KEYS)

/**
* A key the server may not store yet ([PlaybackSettingsKeys.ManifestDetectedKeys])
* stays unconfirmed, like the cellular pair, so a write the flusher held while
* the manifest was unknown is sent again by the first refresh rather than
* replaced with the server's default.
*/
private fun markUnconfirmedIfManifestDetected(
prefs: androidx.datastore.preferences.core.MutablePreferences,
scope: Scope,
key: String,
) {
if (key in PlaybackSettingsKeys.ManifestDetectedKeys) {
prefs[unconfirmedKeysKey(scope)] = prefs[unconfirmedKeysKey(scope)].orEmpty() + key
}
}

private fun unconfirmedKeysKey(scope: Scope): Preferences.Key<Set<String>> =
stringSetPreferencesKey(scope.keyPrefix + UNCONFIRMED_GATED_KEYS)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.siloserver.silo.common.settings

import androidx.compose.runtime.Composable
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import org.siloserver.silo.common.R

/**
* The Show Next Up picker: one control over `playback.next_up_enabled` and
* `playback.next_up_prompt_seconds`. A choice is the seconds before the end
* (0 = at end), or null for Off.
*/
object NextUpChoice {
private val promptSeconds: List<Int?> = listOf(0, 10, 30, 60, 120)

/**
* Off comes first while the server stores the setting, and while this
* device's last choice is Off and the server's manifest is not read yet,
* so that choice stays visible and can be changed.
*/
fun options(offSupported: Boolean, enabled: Boolean): List<Int?> =
if (offSupported || !enabled) listOf(null) + promptSeconds else promptSeconds

fun selected(enabled: Boolean, seconds: Int): Int? = if (enabled) seconds.coerceAtLeast(0) else null

/**
* Off leaves the seconds alone. A time writes both keys where the server
* stores Off, so the picker's choice is this device's whole choice: one
* inherited half would follow a later profile change to Off. Where the
* server lacks the key, only a saved Off is turned back on.
*/
suspend fun apply(store: PlayerSettingsStore, choice: Int?, currentlyEnabled: Boolean, offSupported: Boolean) {
if (choice == null) {
store.setNextUpEnabled(false)
return
}
if (offSupported || !currentlyEnabled) store.setNextUpEnabled(true)
store.setNextUpPromptSeconds(choice)
}
}

/** The phone and TV label for a [NextUpChoice] choice. */
@Composable
fun nextUpChoiceLabel(choice: Int?): String = when {
choice == null -> stringResource(R.string.settings_off)
choice <= 0 -> stringResource(R.string.settings_nextUp_atEnd)
choice < 60 -> pluralStringResource(R.plurals.settings_nextUp_secondsBeforeEnd, choice, choice)
else -> (choice / 60).let { pluralStringResource(R.plurals.settings_nextUp_minutesBeforeEnd, it, it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ interface PlayerSettingsStore {
val bufferingQualityModeSupportedFlow: Flow<Boolean>
get() = flowOf(true)

/**
* `playback.next_up_enabled`: false turns Next Up off. True against a
* server whose manifest lacks the setting.
*/
val nextUpEnabledFlow: Flow<Boolean>
get() = flowOf(true)

/**
* True once the active server's manifest is known to define
* `playback.next_up_enabled`: the Show Next Up picker offers Off only then.
*/
val nextUpEnabledSupportedFlow: Flow<Boolean>
get() = flowOf(true)

// Booleans
/**
* The deprecated boolean, projected from [introSkipModeFlow] rather than
Expand Down Expand Up @@ -228,6 +242,9 @@ interface PlayerSettingsStore {
suspend fun setAudioSyncMs(value: Int)
suspend fun setSubtitleSyncMs(value: Int)
suspend fun setNextUpPromptSeconds(value: Int)

/** Writes `playback.next_up_enabled`; offered only while [nextUpEnabledSupportedFlow]. */
suspend fun setNextUpEnabled(value: Boolean) = Unit
suspend fun setSleepTimerDefaultMinutes(value: Int)
/** Set resume skip-back seconds (clamped 0..30; 0 = off). */
suspend fun setResumeRewindSeconds(value: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,43 @@ class AndroidPlayerSettingsStoreTest {
assertTrue(store.bufferingQualityModeSupportedFlow.first())
}

@Test
fun `Next Up stays on against a server whose manifest lacks the Off setting`() = runTest {
var keys: Set<String> = setOf(PlaybackSettingsKeys.PreferredQuality)
var contract = capabilitiesAt(30)
val api = FakeSettingsApi(
effective = mapOf(stored(PlaybackSettingsKeys.NextUpEnabled, JsonPrimitive(false))),
)
val store = AndroidPlayerSettingsStore(
context = mockContextStub(),
legacyCache = fakeLegacyCache,
getActiveProfileId = { activeProfileId },
getServerUrl = { serverUrl },
serverSettingsFlusher = fakeFlusher,
settingsRepository = SettingsRepository(api),
getDeviceId = { null },
contractRevision = SettingsContractRevision({ contract }, { serverUrl }, { ApiResult.Success(keys) }),
dataStoreFactory = { _, id ->
PreferenceDataStoreFactory.create(produceFile = { File(tempFolder.root, "ds_nu_$id.preferences_pb") })
},
)
store.setNextUpEnabled(false)
assertFalse(store.nextUpEnabledFlow.first(), "the last choice seen, until the manifest is read")

// A server without the setting: Off hides and Next Up shows as before.
store.refreshFromServer()
assertFalse(PlaybackSettingsKeys.NextUpEnabled in api.requestedKeys)
assertFalse(store.nextUpEnabledSupportedFlow.first())
assertTrue(store.nextUpEnabledFlow.first())

// The server upgrades to a contract with the setting.
keys = keys + PlaybackSettingsKeys.NextUpEnabled
contract = capabilitiesAt(31)
store.refreshFromServer()
assertTrue(store.nextUpEnabledSupportedFlow.first())
assertFalse(store.nextUpEnabledFlow.first())
}

@Test
fun `cellular quality is left out of the batch while the server revision is unknown`() = runTest {
val contractRevision = SettingsContractRevision(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.siloserver.silo.common.settings

import java.lang.reflect.Proxy
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

class NextUpChoiceTest {
@Test
fun offIsOfferedWhereTheServerStoresItAndWhileASavedOffAwaitsTheManifest() {
val times = listOf(0, 10, 30, 60, 120)
assertEquals(listOf<Int?>(null) + times, NextUpChoice.options(offSupported = true, enabled = true))
assertEquals(times, NextUpChoice.options(offSupported = false, enabled = true))
// A saved Off before the manifest is read stays visible and selected.
assertEquals(listOf<Int?>(null) + times, NextUpChoice.options(offSupported = false, enabled = false))
assertEquals(null, NextUpChoice.selected(enabled = false, seconds = 30))
}

@Test
fun aTimeIsAChoiceOfBothKeysWhereverTheServerStoresOff() = runTest {
// On already (inherited from the profile): the time still writes On, so
// this device's choice survives the profile turning Next Up off later.
assertEquals(listOf("enabled=true", "seconds=10"), writes(10, currentlyEnabled = true, offSupported = true))
assertEquals(listOf("enabled=true", "seconds=30"), writes(30, currentlyEnabled = false, offSupported = true))
assertEquals(listOf("enabled=false"), writes(null, currentlyEnabled = true, offSupported = true))
// A server without the key is never sent it while Next Up is on.
assertEquals(listOf("seconds=60"), writes(60, currentlyEnabled = true, offSupported = false))
// A saved Off made before the manifest was read is turned back on.
assertEquals(listOf("enabled=true", "seconds=0"), writes(0, currentlyEnabled = false, offSupported = false))
}

private suspend fun writes(choice: Int?, currentlyEnabled: Boolean, offSupported: Boolean): List<String> {
val calls = mutableListOf<String>()
val unused = Proxy.newProxyInstance(
PlayerSettingsStore::class.java.classLoader,
arrayOf(PlayerSettingsStore::class.java),
) { _, method, _ -> throw UnsupportedOperationException(method.name) } as PlayerSettingsStore
val store = object : PlayerSettingsStore by unused {
override suspend fun setNextUpEnabled(value: Boolean) { calls += "enabled=$value" }
override suspend fun setNextUpPromptSeconds(value: Int) { calls += "seconds=$value" }
}
NextUpChoice.apply(store, choice, currentlyEnabled, offSupported)
return calls
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,11 @@ fun PlayerScreen(
}
}

// With Next Up off, the end of playback leaves the player like Back.
LaunchedEffect(Unit) {
viewModel.closeAtEndRequests.collect { exitPlayer() }
}

// Surface transient Watch Party server rejections as a brief Toast. These
// flow on the repository's errors stream and never eject the user. Only
// collected while bound to a room.
Expand Down
Loading
Loading