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 @@ -14,6 +14,7 @@ import org.siloserver.silo.model.download.DownloadQuality
import org.siloserver.silo.model.settings.EffectiveSettingValue
import org.siloserver.silo.model.settings.LanguageOptions
import org.siloserver.silo.model.settings.PlaybackSettingsKeys
import org.siloserver.silo.model.settings.PlaybackSpeedRange
import org.siloserver.silo.model.settings.QualityPresets
import org.siloserver.silo.model.settings.SettingKeys
import org.siloserver.silo.model.settings.SettingScope
Expand Down Expand Up @@ -263,8 +264,12 @@ class AndroidPlayerSettingsStore(
override val hdrEnabledFlow: Flow<Boolean> =
profileScopedFlow(true) { p, s -> p.boolFor(s, PlaybackSettingsKeys.HdrEnabled, true) }

// Off until the server says otherwise: the contract default is false, so
// defaulting on here sent Profile 7 sources down the HDR10 path before the
// first refresh (or offline) when the server would have played them as
// Dolby Vision.
override val dvProfile7HDR10FallbackFlow: Flow<Boolean> =
profileScopedFlow(true) { p, s -> p.boolFor(s, PlaybackSettingsKeys.DvProfile7HDR10Fallback, true) }
profileScopedFlow(false) { p, s -> p.boolFor(s, PlaybackSettingsKeys.DvProfile7HDR10Fallback, false) }

override val dolbyVisionEnabledFlow: Flow<Boolean> =
profileScopedFlow(true) { p, s -> p.boolFor(s, PlaybackSettingsKeys.DolbyVisionEnabled, true) }
Expand Down Expand Up @@ -312,7 +317,10 @@ class AndroidPlayerSettingsStore(
// ---- Doubles -------------------------------------------------------
override val playbackSpeedFlow: Flow<Double> =
profileScopedFlow(1.0) { p, s ->
p.stringFor(s, PlaybackSettingsKeys.PlaybackSpeed, "1.0").toDoubleOrNull() ?: 1.0
// Normalized on read too, so a value an older build stored outside
// the contract range (up to 4.0) is not played back as is.
p.stringFor(s, PlaybackSettingsKeys.PlaybackSpeed, "1.0").toDoubleOrNull()
?.let(PlaybackSpeedRange::normalize) ?: 1.0
}

// ---- Ints ----------------------------------------------------------
Expand Down Expand Up @@ -478,7 +486,9 @@ class AndroidPlayerSettingsStore(
writeStringLocal(PlaybackSettingsKeys.DefaultDownloadQuality, DownloadQuality.fromWire(value).wire)

override suspend fun setPlaybackSpeed(value: Double) {
val clamped = value.coerceIn(0.25, 4.0)
// The server refuses a speed outside the contract range or off its
// step, and the flusher then drops the write for good.
val clamped = PlaybackSpeedRange.normalize(value)
Comment thread
Quick104 marked this conversation as resolved.
Resolved
withScope { scope, store ->
store.edit { it[stringPreferencesKey(scope.keyPrefix + PlaybackSettingsKeys.PlaybackSpeed)] = clamped.toString() }
serverSettingsFlusher.enqueue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,40 @@ class AndroidPlayerSettingsStoreTest {
)

@Test
fun `setPlaybackSpeed clamps out-of-range values`() = runTest {
fun `setPlaybackSpeed clamps to the contract range and step`() = runTest {
val store = newStore()
store.setPlaybackSpeed(10.0)
assertEquals(4.0, store.playbackSpeedFlow.first(), 0.0)
assertEquals(3.0, store.playbackSpeedFlow.first(), 0.0)
store.setPlaybackSpeed(3.5)
assertEquals(3.0, store.playbackSpeedFlow.first(), 0.0)
store.setPlaybackSpeed(0.01)
assertEquals(0.25, store.playbackSpeedFlow.first(), 0.0)
// Off the 0.05 grid: the server would refuse it and the write would
// be dropped, so it is snapped before it is stored or sent.
store.setPlaybackSpeed(1.33)
assertEquals(1.35, store.playbackSpeedFlow.first(), 0.0)
assertEquals(
listOf("3.0", "3.0", "0.25", "1.35"),
fakeFlusher.calls.filter { it.key == PlaybackSettingsKeys.PlaybackSpeed }.map { it.value },
)
}

@Test
fun `a playback speed stored by an older build is normalized on read`() = runTest {
// Older builds clamped to 4.0, so a stored speed can sit outside the
// contract range until the next server refresh, or forever offline.
val store = newStoreSeededWith { prefs ->
prefs[stringPreferencesKey(PlaybackSettingsKeys.PlaybackSpeed)] = "3.5"
}
assertEquals(3.0, store.playbackSpeedFlow.first(), 0.0)
}

@Test
fun `Profile 7 HDR10 fallback defaults off before the first refresh`() = runTest {
// The contract default is false; reading true before hydration (or
// offline) sent Profile 7 sources down the HDR10 path.
assertFalse(newStore().dvProfile7HDR10FallbackFlow.first())
assertFalse(newStore(profileId = null).dvProfile7HDR10FallbackFlow.first())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.siloserver.silo.android.auth

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withTimeoutOrNull
import org.siloserver.silo.common.settings.CardPresentationStore
import org.siloserver.silo.common.settings.OverlayPrefsStore
import org.siloserver.silo.common.settings.PlayerSettingsStore
import org.siloserver.silo.common.settings.SeekIntervalStore
import org.siloserver.silo.common.settings.TitleArtStore
import org.siloserver.silo.model.profile.ActiveProfileStore

/**
* The phone's "Switch Profile", shared by the profile menu and Settings.
*
* Settings writes wait in the flusher's debounce for a moment, and the flusher
* drops a write once its profile is no longer active. So the pending writes are
* pushed first, while the profile they were made on is still the active one
* (the TV switch does the same). Then the shell is left and the per-profile
* caches are dropped, or the next profile keeps rendering, and writing back,
* the previous one's values.
*/
class ProfileSwitchTeardown(
private val playerSettingsStore: PlayerSettingsStore,
private val overlayPrefsStore: OverlayPrefsStore,
private val activeProfileStore: ActiveProfileStore,
private val cardPresentationStore: CardPresentationStore,
private val seekIntervalStore: SeekIntervalStore,
private val titleArtStore: TitleArtStore,
private val flushTimeoutMs: Long = FLUSH_TIMEOUT_MS,
) {
private val _switching = MutableStateFlow(false)

/** True while a switch is pushing settings; the shell shows progress and blocks input. */
val switching: StateFlow<Boolean> = _switching.asStateFlow()

/**
* Push pending settings, then call [leaveShell] and drop per-profile state.
*
* The push is bounded: against an unreachable server each write can wait
* out a connect timeout, and the switch must not hang on that. A write
* still unsent when the bound passes stays queued under the old profile,
* as it would have without this flush.
*
* A request made while a switch is already running is ignored, so a
* second tap during the push cannot open a second profile picker.
* Returns whether this call performed the switch.
*/
suspend fun switchProfile(leaveShell: () -> Unit): Boolean {
if (!_switching.compareAndSet(expect = false, update = true)) return false
try {
withTimeoutOrNull(flushTimeoutMs) { playerSettingsStore.flushPendingDeviceSettings() }
// Navigate before clearing: clearing while the shell is still composed
// repaints it with default cards behind the picker.
leaveShell()
overlayPrefsStore.clear()
activeProfileStore.reset()
cardPresentationStore.clear()
seekIntervalStore.clear()
titleArtStore.clear()
} finally {
_switching.value = false
}
return true
}

private companion object {
const val FLUSH_TIMEOUT_MS = 5_000L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ val androidModule = module {
single {
org.siloserver.silo.android.auth.SignOutTeardown(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get())
}
single {
org.siloserver.silo.android.auth.ProfileSwitchTeardown(get(), get(), get(), get(), get(), get())
}
viewModel { SettingsViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
viewModel { DiagnosticsViewModel(get()) }
viewModel { DownloadsViewModel(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import org.siloserver.silo.android.cast.GOOGLE_CAST_LEGACY_SEEK_INTERVALS
import org.siloserver.silo.android.ui.screens.auth.DevicePairingWrongServerScreen
import org.siloserver.silo.android.ui.screens.auth.DevicePairingUnknownServerScreen
import androidx.compose.runtime.collectAsState
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import org.siloserver.silo.android.ui.components.LoadingIndicator
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
Expand Down Expand Up @@ -181,11 +186,25 @@ fun AppNavigation(
val seekIntervalStore: org.siloserver.silo.common.settings.SeekIntervalStore = koinInject()
val titleArtStore: TitleArtStore = koinInject()
val signOutTeardown: org.siloserver.silo.android.auth.SignOutTeardown = koinInject()
val profileSwitchTeardown: org.siloserver.silo.android.auth.ProfileSwitchTeardown = koinInject()
val siloCastController: SiloCastController = koinInject()
// Lives as long as the nav host, so work started from a destination that is
// popped in the same gesture (re-hydrating after a profile switch) is not
// cancelled with that destination's own scope.
val navScope = rememberCoroutineScope()
val profileSwitching by profileSwitchTeardown.switching.collectAsState()
// Both "Switch Profile" entry points (profile menu, Settings) run here, so
// leaving a tab while settings are pushed cannot cancel the switch; the
// teardown ignores a repeat request while one is in flight.
val switchProfile: () -> Unit = remember(navController, profileSwitchTeardown) {
{
navScope.launch {
profileSwitchTeardown.switchProfile {
navController.navigate(Route.ProfileSelection.route)
}
}
}
}
val diagnosticsViewModel = koinViewModel<DiagnosticsViewModel>()
val diagnosticsState by diagnosticsViewModel.state.collectAsState()
var activePlayerTargetProvider by remember {
Expand Down Expand Up @@ -390,6 +409,7 @@ fun AppNavigation(
CompositionLocalProvider(
LocalSharedTransitionScope provides this,
LocalHeroSourceHandoff provides heroSourceHandoff,
LocalProfileSwitch provides switchProfile,
) {
Box(modifier = Modifier.fillMaxSize()) {
// The first-run screens draw over one shared brand-light backdrop that
Expand Down Expand Up @@ -951,16 +971,9 @@ fun AppNavigation(
onPairDevice = {
navController.navigate(Route.PairDevice().route)
},
onSwitchProfile = {
// Leave the shell first, then drop the per-profile caches
// (see the profile-menu path in MainScreen).
navController.navigate(Route.ProfileSelection.route)
overlayPrefsStore.clear()
activeProfileStore.reset()
cardPresentationStore.clear()
seekIntervalStore.clear()
titleArtStore.clear()
},
// Same switch as the profile menu: push pending settings,
// leave the shell, then drop the per-profile caches.
onSwitchProfile = switchProfile,
onNavigateToWatchlist = { navController.navigate(Route.Watchlist.route) },
onNavigateToFavorites = { navController.navigate(Route.Favorites.route) },
onNavigateToHistory = { navController.navigate(Route.History.route) },
Expand Down Expand Up @@ -1689,6 +1702,26 @@ fun AppNavigation(
enabled = currentRoute != null && currentRoute !in companionHiddenRoutes,
)
org.siloserver.silo.android.ui.screens.profiles.HouseholdReverifyHost()

// While a profile switch pushes pending settings (bounded at a few
// seconds), show that it is under way and take input, so nothing else
// can be opened over a switch that is about to leave the shell.
if (profileSwitching) {
// System Back too: composed after the NavHost, this handler wins, so
// Back can neither pop the screen under the switch nor close the
// app and cancel it.
BackHandler {}
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(alpha = 0.4f))
.pointerInput(Unit) {
awaitPointerEventScope { while (true) awaitPointerEvent().changes.forEach { it.consume() } }
Comment thread
Quick104 marked this conversation as resolved.
Resolved
},
) {
LoadingIndicator()
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.siloserver.silo.android.ui.navigation

import androidx.compose.runtime.staticCompositionLocalOf

/**
* Starts "Switch Profile" in the navigation host's scope, which outlives the
* tab screen the request came from. Run from a tab's own scope, opening a
* title while settings were still being pushed cancelled the switch.
*/
val LocalProfileSwitch = staticCompositionLocalOf<() -> Unit> { {} }
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import org.siloserver.silo.model.profile.ActiveProfileStore
import org.siloserver.silo.android.ui.components.MainAppHeaderBodyHeight
import org.siloserver.silo.android.ui.components.MainAppTopBar
import org.siloserver.silo.android.ui.components.TabTopBarActions
Expand Down Expand Up @@ -76,8 +75,6 @@ import org.siloserver.silo.model.feature.MetadataAiFeatureStore
import org.siloserver.silo.model.feature.RequestsFeatureStore
import org.siloserver.silo.common.network.ServerReachabilityMonitor
import org.siloserver.silo.common.network.ServerReachabilityStatus
import org.siloserver.silo.common.settings.CardPresentationStore
import org.siloserver.silo.common.settings.OverlayPrefsStore
import org.siloserver.silo.android.ui.theme.siloPageBackdrop
import org.siloserver.silo.network.ApiResult
import org.siloserver.silo.network.ServerRegistry
Expand Down Expand Up @@ -164,12 +161,8 @@ fun MainScreen(
val requestsFeatureStore: RequestsFeatureStore = koinInject()
val metadataAiFeatureStore: MetadataAiFeatureStore = koinInject()
val shuffleFeatureStore: org.siloserver.silo.model.feature.ShuffleFeatureStore = koinInject()
val overlayPrefsStore: OverlayPrefsStore = koinInject()
val activeProfileStore: ActiveProfileStore = koinInject()
val cardPresentationStore: CardPresentationStore = koinInject()
val seekIntervalStore: org.siloserver.silo.common.settings.SeekIntervalStore = koinInject()
val titleArtStore: org.siloserver.silo.common.settings.TitleArtStore = koinInject()
val signOutTeardown: org.siloserver.silo.android.auth.SignOutTeardown = koinInject()
val switchProfile = org.siloserver.silo.android.ui.navigation.LocalProfileSwitch.current
val reachabilityState by reachabilityMonitor.state.collectAsState()
val requestsEnabled by requestsFeatureStore.isEnabled.collectAsState()
val reachabilityScope = rememberCoroutineScope()
Expand Down Expand Up @@ -303,21 +296,12 @@ fun MainScreen(
}

/**
* Profile-menu "Switch Profile". Overlays and card presentation are cached
* per profile and the app never backgrounds during an in-app switch, so
* drop them here or the next profile keeps rendering — and writing back —
* the previous one's values. Navigate first: clearing while the shell is
* still composed repaints it with default cards behind the picker (the
* TV shell's switch-profile path takes the same order).
* Profile-menu "Switch Profile": pushes pending settings while this
* profile is still active, then leaves the shell and drops the per-profile
* caches (see [org.siloserver.silo.android.auth.ProfileSwitchTeardown]).
* Runs in the navigation host's scope ([org.siloserver.silo.android.ui.navigation.LocalProfileSwitch]).
*/
fun switchProfileFromMenu() {
navController.navigate(Route.ProfileSelection.route)
overlayPrefsStore.clear()
activeProfileStore.reset()
cardPresentationStore.clear()
seekIntervalStore.clear()
titleArtStore.clear()
}
fun switchProfileFromMenu() = switchProfile()
val requestsMenuAction: (() -> Unit)? = if (requestsEnabled) {
{ navController.navigate(Route.Requests.route) }
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ data class SettingsUiState(
val autoSkipCredits: Boolean = false,
val pictureInPictureEnabled: Boolean = true,
val dolbyVisionEnabled: Boolean = true,
val dvProfile7HDR10Fallback: Boolean = true,
val dvProfile7HDR10Fallback: Boolean = false,
val subtitleMatchesDevice: Boolean = false,
val showAudiobooks: Boolean = false,
val subtitleAppearance: org.siloserver.silo.model.settings.SubtitleAppearance =
Expand Down
Loading
Loading