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 @@ -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 @@ -202,7 +202,7 @@ class TvLibraryDetailViewModel(
UiState(
title = libraryTitle,
libraryType = libraryType,
preserveFilters = browsePrefs?.preserveEnabled(libraryId) ?: true,
preserveFilters = browsePrefs?.preserveEnabled(libraryId, mediaScope) ?: true,
),
)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
Expand Down Expand Up @@ -405,7 +405,7 @@ class TvLibraryDetailViewModel(
* current Browse sort and filters at once (tvOS `setPreserveEnabled`).
*/
fun onPreserveFiltersChanged(enabled: Boolean) {
browsePrefs?.setPreserveEnabled(libraryId, enabled)
browsePrefs?.setPreserveEnabled(libraryId, enabled, mediaScope)
_uiState.update { it.copy(preserveFilters = enabled) }
if (enabled) saveBrowseFilter(_uiState.value)
}
Expand All @@ -426,7 +426,7 @@ class TvLibraryDetailViewModel(
// sections are fixed presets.
private fun saveBrowseFilter(state: UiState) {
if (state.selectedTab != TvLibraryTab.Browse) return
browsePrefs?.saveState(libraryId, state.browseFilter.toSavedState())
browsePrefs?.saveState(libraryId, state.browseFilter.toSavedState(), mediaScope)
}

private var recommendedGeneration = 0L
Expand Down Expand Up @@ -832,7 +832,7 @@ class TvLibraryDetailViewModel(
TvLibraryTab.Collections -> this
// Browse restores the saved sort and filters, else lands on the
// tvOS default view: Title A–Z, no facets.
TvLibraryTab.Browse -> browsePrefs?.savedState(libraryId)?.toTvBrowseFilter() ?: copy(
TvLibraryTab.Browse -> browsePrefs?.savedState(libraryId, mediaScope)?.toTvBrowseFilter() ?: copy(
genre = null,
namePrefix = null,
sort = TvLibrarySortOption.Title.wireValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ class TvLibraryBrowsePrefsTest {
assertEquals("year", saved?.sort)
}

@Test
fun mixedLibraryMoviesAndSeriesKeepSeparateBrowseState() = runPrefsTest {
val prefs = BrowsePrefsStore(ApplicationProvider.getApplicationContext(), FakeServerRegistry(), "androidtv")
val movies = viewModel(prefs, libraryType = "mixed", mediaScope = "movie")
movies.onTabSelected(TvLibraryTab.Browse)
movies.onSortKeySelected(TvLibrarySortOption.Year)
movies.onFacetSelectionApplied(
TvCatalogFacetSelection().toggled(TvCatalogFacet.Genre, "Drama"),
)
movies.onPreserveFiltersChanged(false)

// The Series tab of the same library opens on its own defaults and
// keeps its own preserve toggle.
val series = viewModel(prefs, libraryType = "mixed", mediaScope = "series")
assertEquals(true, series.uiState.value.preserveFilters)
series.onTabSelected(TvLibraryTab.Browse)
assertEquals("title", series.uiState.value.browseFilter.sort)
assertEquals(TvCatalogFacetSelection(), series.uiState.value.browseFilter.facetSelection)
series.onSortKeySelected(TvLibrarySortOption.Rating)

assertEquals("rating_imdb", prefs.savedState(LIBRARY_ID, "series")?.sort)
assertNull(prefs.savedState(LIBRARY_ID, "movie"))
assertNull(prefs.savedState(LIBRARY_ID))
assertEquals(false, viewModel(prefs, libraryType = "mixed", mediaScope = "movie").uiState.value.preserveFilters)
}

private val createdViewModels = mutableListOf<TvLibraryDetailViewModel>()

private fun runPrefsTest(block: suspend () -> Unit) = runTest {
Expand All @@ -152,7 +178,11 @@ class TvLibraryBrowsePrefsTest {
}
}

private suspend fun viewModel(prefs: BrowsePrefsStore): TvLibraryDetailViewModel {
private suspend fun viewModel(
prefs: BrowsePrefsStore,
libraryType: String = "movies",
mediaScope: String? = null,
): TvLibraryDetailViewModel {
val client = HttpClient(
MockEngine { request ->
val body = when (request.url.encodedPath) {
Expand All @@ -170,7 +200,8 @@ class TvLibraryBrowsePrefsTest {
catalogRepository = CatalogRepository(CatalogApi(client)),
libraryId = LIBRARY_ID,
libraryTitle = "Movies",
libraryType = "movies",
libraryType = libraryType,
mediaScope = mediaScope,
browsePrefs = prefs,
).also { createdViewModels += it }
// Let the eager Recommended load finish before the test drives tabs.
Expand Down
Loading