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
1 change: 1 addition & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ File pointers are repository-relative.
| AI subtitle transcription / translation (quota-tracked) | ✅ | ✅ | TV: `TvAiTranslateDialog` |
| AI description translation (on-view, server-gated) | ✅ | ✅ | `DescriptionTranslationController`; gated by `/api/v1/metadata/ai/status`; metadata-language setting in Settings |
| Intro auto-skip (+ manual skip banner) | ✅ | ✅ | |
| Shuffle (library, series, season, collection) | ✅ | ✅ | Server-picked random movies and episodes, gated by `/api/v2/shuffles/capabilities`. Picks play from the start; the up-next screen offers Pick Another and Stop shuffling. TV has no mixed-library screen, so mixed libraries shuffle on phone only. [docs](docs/shuffle-api-v2.md) |
| Chapters | ✅ | ✅ | Server-extracted; TV scrubber markers |
| Sleep timer | ✅ | ✅ | Configurable default |
| Playback speed | ✅ | ✅ | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.siloserver.silo.common.ui

import android.content.Context
import android.widget.Toast
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.siloserver.silo.model.feature.ShuffleFeatureStore
import org.siloserver.silo.model.feature.ShuffleStart
import org.siloserver.silo.model.shuffle.Shuffle
import org.siloserver.silo.model.shuffle.ShuffleCapability
import org.siloserver.silo.model.shuffle.ShuffleScopeKind

/**
* Starts shuffles from an entry point: offers a scope kind only when the
* server's capability allows it, ignores presses while a start is in flight,
* and reports a failure as a toast.
*/
class ShuffleLauncher internal constructor(
private val store: ShuffleFeatureStore,
private val scope: CoroutineScope,
private val context: Context,
private val onStarted: State<(Shuffle) -> Unit>,
private val capability: State<ShuffleCapability>,
) {
var isStarting by mutableStateOf(false)
private set

fun supports(kind: ShuffleScopeKind): Boolean = capability.value.supports(kind)

fun start(kind: ShuffleScopeKind, id: String) {
if (isStarting || id.isBlank()) return
isStarting = true
scope.launch {
try {
when (val result = store.start(kind, id)) {
is ShuffleStart.Started -> onStarted.value(result.shuffle)
is ShuffleStart.Failed -> Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show()
}
} finally {
isStarting = false
}
}
}
}

/** A [ShuffleLauncher] that calls [onStarted] with each shuffle it starts. */
@Composable
fun rememberShuffleLauncher(store: ShuffleFeatureStore, onStarted: (Shuffle) -> Unit): ShuffleLauncher {
val scope = rememberCoroutineScope()
val context = LocalContext.current.applicationContext
val latestOnStarted = rememberUpdatedState(onStarted)
val capability = store.capability.collectAsState()
return remember(store, scope, context) {
ShuffleLauncher(store, scope, context, latestOnStarted, capability)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ val androidModule = module {
castPlaybackPreparer = get(),
seekIntervalStore = get(),
activeProfileStore = get(),
shuffleFeatureStore = get(),
)
}
viewModel { HomeViewModel(get(), get(), get(), get(), getOrNull(), get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,14 @@ fun AppNavigation(
CollectionDetailScreen(
collectionId = backStackEntry.arguments?.getString("collectionId") ?: "",
onBackClick = { navController.popBackStack() },
onShuffleStarted = { shuffle ->
navController.navigate(
org.siloserver.silo.android.ui.screens.shuffle.shufflePlayerRoute(
shuffle,
backStackEntry.arguments?.getString("libraryId")?.toIntOrNull(),
),
)
},
onItemClick = { contentId ->
navController.navigate(
Route.ItemDetail(
Expand Down Expand Up @@ -1277,6 +1285,11 @@ fun AppNavigation(
resolvedEpisodeId = episodeId
resolvedSeriesId = seriesId
},
onShuffleStarted = { shuffle ->
navController.navigate(
org.siloserver.silo.android.ui.screens.shuffle.shufflePlayerRoute(shuffle, libraryId),
)
},
onPersonClick = { personId ->
personId.toLongOrNull()?.let { id ->
navController.navigate(Route.PersonDetail(id).route)
Expand Down Expand Up @@ -1471,6 +1484,11 @@ fun AppNavigation(
nullable = true
defaultValue = null
},
navArgument("shuffleId") {
type = NavType.StringType
nullable = true
defaultValue = null
},
),
) { backStackEntry ->
val playerViewModel = koinViewModel<PlayerViewModel>()
Expand Down Expand Up @@ -1499,6 +1517,7 @@ fun AppNavigation(
backStackEntry.arguments?.getString("resumePosition"),
),
roomId = backStackEntry.arguments?.getString("roomId"),
shuffleId = backStackEntry.arguments?.getString("shuffleId"),
navController = navController,
viewModel = playerViewModel,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ sealed class Route(val route: String) {
val resumePositionSeconds: Double? = null,
val roomId: String? = null,
val libraryId: Int? = null,
/** The running shuffle this item is a pick of; picks play from the beginning. */
val shuffleId: String? = null,
) : Route(
buildString {
append("player/${contentId.routeEncode()}")
Expand All @@ -230,6 +232,7 @@ sealed class Route(val route: String) {
VideoPlayerRouteArgs.encodeResumePosition(resumePositionSeconds)
?.let { "${VideoPlayerRouteArgs.RESUME_POSITION}=$it" },
roomId?.takeIf { it.isNotBlank() }?.let { "roomId=${Uri.encode(it)}" },
shuffleId?.takeIf { it.isNotBlank() }?.let { "shuffleId=${Uri.encode(it)}" },
)
if (queryParams.isNotEmpty()) {
append("?")
Expand All @@ -239,7 +242,7 @@ sealed class Route(val route: String) {
) {
companion object {
const val ROUTE =
"player/{contentId}?libraryId={libraryId}&fileId={fileId}&quality={quality}&audioTrackIndex={audioTrackIndex}&subtitleTrackIndex={subtitleTrackIndex}&resumePosition={resumePosition}&roomId={roomId}"
"player/{contentId}?libraryId={libraryId}&fileId={fileId}&quality={quality}&audioTrackIndex={audioTrackIndex}&subtitleTrackIndex={subtitleTrackIndex}&resumePosition={resumePosition}&roomId={roomId}&shuffleId={shuffleId}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ fun MainScreen(
val reachabilityMonitor: ServerReachabilityMonitor = koinInject()
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()
Expand Down Expand Up @@ -284,6 +285,8 @@ fun MainScreen(
requestsFeatureStore.refresh()
metadataAiFeatureStore.reset()
metadataAiFeatureStore.refresh()
shuffleFeatureStore.reset()
shuffleFeatureStore.refresh()
}

fun signOutFromProfileMenu() {
Expand Down Expand Up @@ -441,7 +444,16 @@ fun MainScreen(
)
}
Tab.Libraries -> {
val shuffleLauncher = org.siloserver.silo.android.ui.screens.shuffle.rememberShuffleLauncher { shuffle ->
navController.navigate(
org.siloserver.silo.android.ui.screens.shuffle.shufflePlayerRoute(
shuffle,
libraryId = shuffle.scope.id.toIntOrNull(),
),
)
}
LibrariesScreen(
shuffleLauncher = shuffleLauncher,
onItemClick = { contentId, libraryId ->
navController.navigate(Route.ItemDetail(contentId, libraryId = libraryId).route)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Shuffle
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material.icons.outlined.CollectionsBookmark
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -42,10 +45,14 @@ fun CollectionDetailScreen(
collectionId: String,
onBackClick: () -> Unit,
onItemClick: (String) -> Unit,
// Plays the first pick of a shuffle started from the top bar.
onShuffleStarted: (org.siloserver.silo.model.shuffle.Shuffle) -> Unit = {},
viewModel: CollectionDetailViewModel = koinViewModel(),
) {
val state by viewModel.uiState.collectAsState()
val gridState = rememberLazyGridState()
val shuffleLauncher = org.siloserver.silo.android.ui.screens.shuffle.rememberShuffleLauncher(onShuffleStarted)
val shuffleKind = viewModel.shuffleScopeKind

LaunchedEffect(collectionId) {
viewModel.initialize(collectionId)
Expand All @@ -70,6 +77,16 @@ fun CollectionDetailScreen(
SiloTopBar(
title = state.title,
onBackClick = onBackClick,
actions = {
if (state.items.isNotEmpty() && shuffleLauncher.supports(shuffleKind)) {
IconButton(
onClick = { shuffleLauncher.start(shuffleKind, collectionId) },
enabled = !shuffleLauncher.isStarting,
) {
Icon(Icons.Filled.Shuffle, contentDescription = "Shuffle")
}
}
},
)
},
containerColor = MaterialTheme.colorScheme.background,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class CollectionDetailViewModel(
private val collectionSource: String? = savedStateHandle.get<String>("source")
private val isLibraryUserCollection: Boolean
get() = libraryId != null && collectionSource == "user_collection"

/** The shuffle scope this collection is: a library's server collection or a user collection. */
val shuffleScopeKind: org.siloserver.silo.model.shuffle.ShuffleScopeKind
get() = if (libraryId != null && !isLibraryUserCollection) {
org.siloserver.silo.model.shuffle.ShuffleScopeKind.LIBRARY_COLLECTION
} else {
org.siloserver.silo.model.shuffle.ShuffleScopeKind.USER_COLLECTION
}

/** The library the collection was opened from, carried into shuffled playback. */
val browseLibraryId: Int? get() = libraryId
private val pageSize = 40

fun initialize(id: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ fun ItemDetailScreen(
// Auto-presents the cast remote after "Play on device" launches, mirroring
// Apple's playOnTV: the connect/handoff handshake renders in the remote.
onOpenCastRemote: () -> Unit = {},
// Plays the first pick of a shuffle started from the series overflow menu.
onShuffleStarted: (org.siloserver.silo.model.shuffle.Shuffle) -> Unit = {},
viewModel: ItemDetailViewModel,
modifier: Modifier = Modifier,
) {
val state by viewModel.uiState.collectAsState()
val shuffleLauncher = org.siloserver.silo.android.ui.screens.shuffle.rememberShuffleLauncher(onShuffleStarted)
val seriesRedirect = remember(state.detail) { state.detail?.let(::seriesDetailRedirect) }
var seriesRedirectFailed by rememberSaveable(state.detail?.contentId) { mutableStateOf(false) }
LaunchedEffect(seriesRedirect) {
Expand Down Expand Up @@ -742,6 +745,30 @@ fun ItemDetailScreen(
onSeasonWatchedChange = { season, watched ->
viewModel.setSeasonWatched(season, watched)
},
onShuffleSeries = if (
shuffleLauncher.supports(org.siloserver.silo.model.shuffle.ShuffleScopeKind.SERIES)
) {
{
shuffleLauncher.start(
org.siloserver.silo.model.shuffle.ShuffleScopeKind.SERIES,
detail.contentId,
)
}
} else {
null
},
onShuffleSeason = if (
shuffleLauncher.supports(org.siloserver.silo.model.shuffle.ShuffleScopeKind.SEASON)
) {
{ season ->
shuffleLauncher.start(
org.siloserver.silo.model.shuffle.ShuffleScopeKind.SEASON,
season.contentId,
)
}
} else {
null
},
onFavoriteClick = { viewModel.toggleFavorite() },
onWatchlistClick = { viewModel.toggleWatchlist() },
onToggleWatched = { viewModel.toggleWatched() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Shuffle
import androidx.compose.material.icons.outlined.Groups
import androidx.compose.material.icons.outlined.AudioFile
import androidx.compose.material.icons.outlined.CheckCircle
Expand Down Expand Up @@ -75,6 +76,10 @@ fun SeriesDetailContent(
onEpisodeWatchedChange: (String, Boolean) -> Unit,
onSeasonSelected: (Int) -> Unit,
onSeasonWatchedChange: ((Season, Boolean) -> Unit)? = null,
// Shuffle the whole series, or the season on screen; null where the
// server doesn't offer that scope.
onShuffleSeries: (() -> Unit)? = null,
onShuffleSeason: ((Season) -> Unit)? = null,
onFavoriteClick: () -> Unit,
onWatchlistClick: () -> Unit,
onToggleWatched: () -> Unit,
Expand Down Expand Up @@ -119,6 +124,10 @@ fun SeriesDetailContent(
val markableSelectedSeason = selectedSeason?.takeIf {
onSeasonWatchedChange != null && it.episodeCount > 0
}
// Hidden on a season with fewer than two playable episodes.
val shuffleableSelectedSeason = selectedSeason?.takeIf {
onShuffleSeason != null && org.siloserver.silo.model.shuffle.canShuffleSeason(episodes)
}
val episodeCountSubtitle = selectedSeason?.episodeCount?.takeIf { it > 0 }?.let { count ->
"$count episode${if (count == 1) "" else "s"}"
}
Expand Down Expand Up @@ -287,8 +296,31 @@ fun SeriesDetailContent(
onToggleFavorite = onFavoriteClick,
onToggleWatchlist = onWatchlistClick,
onToggleWatched = onToggleWatched,
overflow = if (markableSelectedSeason != null || partyAction != null) {
overflow = if (
markableSelectedSeason != null || partyAction != null ||
onShuffleSeries != null || shuffleableSelectedSeason != null
) {
{ dismiss ->
if (onShuffleSeries != null) {
DropdownMenuItem(
text = { Text("Shuffle Series") },
leadingIcon = { Icon(Icons.Filled.Shuffle, contentDescription = null) },
onClick = {
dismiss()
onShuffleSeries()
},
)
}
if (shuffleableSelectedSeason != null && onShuffleSeason != null) {
DropdownMenuItem(
text = { Text("Shuffle ${phoneSeasonLabel(shuffleableSelectedSeason)}") },
leadingIcon = { Icon(Icons.Filled.Shuffle, contentDescription = null) },
onClick = {
dismiss()
onShuffleSeason(shuffleableSelectedSeason)
},
)
}
if (markableSelectedSeason != null && onSeasonWatchedChange != null) {
val seasonWatched = markableSelectedSeason.userData?.played == true
DropdownMenuItem(
Expand Down
Loading
Loading