Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import org.siloserver.silo.network.ServerRegistry
* The phone and TV apps each pass their own [keyPrefix] (`android`,
* `androidtv`), as Apple keeps `ios.` / `tv.` / `mac.` apart.
*
* A Movies- or Series-scoped view of a mixed library saves under its own
* `.type-<scope>` key, so the two views keep separate filters.
*
* Gated by a user-facing "Preserve sort & filters" toggle (default ON);
* turning it off clears the saved state.
*/
Expand All @@ -26,35 +29,35 @@ class BrowsePrefsStore(
private val prefs = context.getSharedPreferences("browse_prefs", Context.MODE_PRIVATE)
private val json = Json { ignoreUnknownKeys = true }

private fun base(libraryId: Int?): String? {
private fun base(libraryId: Int?, mediaScope: String? = null): String? {
val serverId = serverRegistry.activeServerId.value ?: "default"
val profileId = serverRegistry.activeEntry.value?.profileId
?.takeIf { it.isNotBlank() } ?: return null
val lib = libraryId?.toString() ?: "all"
return "$keyPrefix.browsePrefs.$serverId.$profileId.$lib"
return "$keyPrefix.browsePrefs.$serverId.$profileId.$lib" + (mediaScope?.let { ".type-$it" } ?: "")
}

fun savedState(libraryId: Int?): CatalogFilterState? {
if (!preserveEnabled(libraryId)) return null
val key = base(libraryId) ?: return null
fun savedState(libraryId: Int?, mediaScope: String? = null): CatalogFilterState? {
if (!preserveEnabled(libraryId, mediaScope)) return null
val key = base(libraryId, mediaScope) ?: return null
val raw = prefs.getString("$key.state", null) ?: return null
return runCatching { json.decodeFromString<CatalogFilterState>(raw) }.getOrNull()
}

fun saveState(libraryId: Int?, state: CatalogFilterState) {
if (!preserveEnabled(libraryId)) return
val key = base(libraryId) ?: return
fun saveState(libraryId: Int?, state: CatalogFilterState, mediaScope: String? = null) {
if (!preserveEnabled(libraryId, mediaScope)) return
val key = base(libraryId, mediaScope) ?: return
prefs.edit { putString("$key.state", json.encodeToString(state)) }
}

/** Default ON when the key is absent (iOS parity). */
fun preserveEnabled(libraryId: Int?): Boolean {
val key = base(libraryId) ?: return false
fun preserveEnabled(libraryId: Int?, mediaScope: String? = null): Boolean {
val key = base(libraryId, mediaScope) ?: return false
return prefs.getBoolean("$key.preserve", true)
}

fun setPreserveEnabled(libraryId: Int?, enabled: Boolean) {
val key = base(libraryId) ?: return
fun setPreserveEnabled(libraryId: Int?, enabled: Boolean, mediaScope: String? = null) {
val key = base(libraryId, mediaScope) ?: return
prefs.edit {
putBoolean("$key.preserve", enabled)
if (!enabled) remove("$key.state")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,11 @@ fun AppNavigation(
nullable = true
defaultValue = null
},
navArgument("mediaScope") {
type = NavType.StringType
nullable = true
defaultValue = null
},
),
) { backStackEntry ->
CollectionDetailScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,20 @@ sealed class Route(val route: String) {
val collectionId: String,
val libraryId: Int? = null,
val source: String? = null,
val mediaScope: String? = null,
) : Route(
buildString {
append("collection/${collectionId.routeEncode()}")
val parameters = buildList {
libraryId?.let { add("libraryId=$it") }
source?.takeIf { it.isNotBlank() }?.let { add("source=${it.routeEncode()}") }
mediaScope?.let { add("mediaScope=${it.routeEncode()}") }
}
if (parameters.isNotEmpty()) append("?${parameters.joinToString("&")}")
},
) {
companion object {
const val ROUTE = "collection/{collectionId}?libraryId={libraryId}&source={source}"
const val ROUTE = "collection/{collectionId}?libraryId={libraryId}&source={source}&mediaScope={mediaScope}"
}
}

Expand Down Expand Up @@ -343,9 +345,11 @@ sealed class Route(val route: String) {
fun libraryCollectionDetailRoute(
collection: LibraryCollection,
libraryId: Int,
mediaScope: String? = null,
): String = Route.CollectionDetail(
collectionId = collection.id,
libraryId = libraryId,
mediaScope = mediaScope,
source = if (collection.kind == "user_collections") {
"user_collection"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ fun MainScreen(
onItemClick = { contentId, libraryId ->
navController.navigate(Route.ItemDetail(contentId, libraryId = libraryId).route)
},
onCollectionClick = { collection, libraryId ->
onCollectionClick = { collection, libraryId, mediaScope ->
navController.navigate(
libraryCollectionDetailRoute(collection, libraryId),
libraryCollectionDetailRoute(collection, libraryId, mediaScope),
)
},
viewModel = requireNotNull(librariesViewModel),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fun CollectionDetailScreen(
title = state.title,
onBackClick = onBackClick,
actions = {
if (state.items.isNotEmpty() && shuffleLauncher.supports(shuffleKind)) {
if (shuffleKind != null && state.items.isNotEmpty() && shuffleLauncher.supports(shuffleKind)) {
IconButton(
onClick = { shuffleLauncher.start(shuffleKind, collectionId) },
enabled = !shuffleLauncher.isStarting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ class CollectionDetailViewModel(
private var collectionId: String = ""
private val libraryId: Int? = savedStateHandle.get<String>("libraryId")?.toIntOrNull()
private val collectionSource: String? = savedStateHandle.get<String>("source")
private val mediaScope = savedStateHandle.get<String>("mediaScope")?.takeIf { it in setOf("movie", "series") }
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) {
/**
* The shuffle scope this collection is: a library's server collection or a
* user collection. Null in a Movies- or Series-scoped view: a shuffle
* request carries no media type or library, so it would draw from both.
*/
val shuffleScopeKind: org.siloserver.silo.model.shuffle.ShuffleScopeKind?
get() = if (mediaScope != null) {
null
} else if (libraryId != null && !isLibraryUserCollection) {
org.siloserver.silo.model.shuffle.ShuffleScopeKind.LIBRARY_COLLECTION
} else {
org.siloserver.silo.model.shuffle.ShuffleScopeKind.USER_COLLECTION
Expand Down Expand Up @@ -150,7 +157,7 @@ class CollectionDetailViewModel(
}
}

val itemsResult = if (isLibraryUserCollection) {
val itemsResult = if (isLibraryUserCollection && mediaScope == null) {
collectionRepository.getItems(
collectionId,
limit = pageSize,
Expand All @@ -163,6 +170,9 @@ class CollectionDetailViewModel(
sectionRepository.getLibraryCollectionItems(
collectionId,
limit = pageSize,
mediaType = mediaScope,
libraryId = libraryId,
source = if (isLibraryUserCollection) "user_collection" else "library_collection",
).map {
libraryContinuation = it.continuation
it
Expand Down Expand Up @@ -208,11 +218,14 @@ class CollectionDetailViewModel(
pagingJob = viewModelScope.launch {
_uiState.update { it.copy(isLoadingMore = true) }
// Library paging is migrated separately; personal collections use opaque cursors.
val result = if (libraryId != null && !isLibraryUserCollection) {
val result = if (libraryId != null && (!isLibraryUserCollection || mediaScope != null)) {
sectionRepository.getLibraryCollectionItems(
collectionId,
continuation = libraryContinuation,
limit = pageSize,
mediaType = mediaScope,
libraryId = libraryId,
source = if (isLibraryUserCollection) "user_collection" else "library_collection",
).map { libraryContinuation = it.continuation; it }
} else {
collectionRepository.getItems(
Expand Down
Loading
Loading