diff --git a/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt b/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt index 6f1bc365..cef6561b 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/data/model/HomeRowConfig.kt @@ -34,7 +34,7 @@ sealed interface HomeRowConfig { @SerialName("ContinueWatching") data class ContinueWatching( override val id: Int, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): ContinueWatching = this.copy(viewOptions = viewOptions) } @@ -46,7 +46,7 @@ sealed interface HomeRowConfig { @SerialName("NextUp") data class NextUp( override val id: Int, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): NextUp = this.copy(viewOptions = viewOptions) } @@ -58,7 +58,7 @@ sealed interface HomeRowConfig { @SerialName("ContinueWatchingCombined") data class ContinueWatchingCombined( override val id: Int, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): ContinueWatchingCombined = this.copy(viewOptions = viewOptions) } @@ -71,7 +71,7 @@ sealed interface HomeRowConfig { data class RecentlyAdded( override val id: Int, val parentId: UUID, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): RecentlyAdded = this.copy(viewOptions = viewOptions) } @@ -84,7 +84,7 @@ sealed interface HomeRowConfig { data class RecentlyReleased( override val id: Int, val parentId: UUID, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): RecentlyReleased = this.copy(viewOptions = viewOptions) } @@ -116,7 +116,7 @@ sealed interface HomeRowConfig { val parentId: UUID, val recursive: Boolean, val sort: SortAndDirection? = null, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): ByParent = this.copy(viewOptions = viewOptions) } @@ -130,7 +130,7 @@ sealed interface HomeRowConfig { override val id: Int, val name: String, val getItems: GetItemsRequest, - override val viewOptions: HomeRowViewOptions, + override val viewOptions: HomeRowViewOptions = HomeRowViewOptions(), ) : HomeRowConfig { override fun updateViewOptions(viewOptions: HomeRowViewOptions): GetItems = this.copy(viewOptions = viewOptions) } diff --git a/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt b/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt index 995cae8c..a213707e 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/services/HomeSettingsService.kt @@ -7,7 +7,6 @@ import com.github.damontecres.wholphin.data.model.BaseItem import com.github.damontecres.wholphin.data.model.HomePageSettings import com.github.damontecres.wholphin.data.model.HomeRowConfig import com.github.damontecres.wholphin.data.model.HomeRowViewOptions -import com.github.damontecres.wholphin.data.model.SUPPORTED_HOME_PAGE_SETTINGS_VERSION import com.github.damontecres.wholphin.preferences.HomePagePreferences import com.github.damontecres.wholphin.ui.DefaultItemFields import com.github.damontecres.wholphin.ui.SlimItemFields @@ -24,8 +23,11 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.decodeFromJsonElement import kotlinx.serialization.json.encodeToStream +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject import org.jellyfin.sdk.api.client.ApiClient import org.jellyfin.sdk.api.client.extensions.displayPreferencesApi import org.jellyfin.sdk.api.client.extensions.userLibraryApi @@ -85,9 +87,8 @@ class HomeSettingsService ): HomePageSettings? { val current = getDisplayPreferences(userId, displayPreferencesId) return current.customPrefs[DISPLAY_PREF_ID]?.let { - Json - .decodeFromString(it) - .takeIf { it.version <= SUPPORTED_HOME_PAGE_SETTINGS_VERSION } + val jsonElement = jsonParser.parseToJsonElement(it) + decode(jsonElement) } } @@ -120,16 +121,30 @@ class HomeSettingsService val dir = File(context.filesDir, CUSTOM_PREF_ID) val file = File(dir, filename(userId)) return if (file.exists()) { - file.inputStream().use { - jsonParser - .decodeFromStream(it) - .takeIf { it.version <= SUPPORTED_HOME_PAGE_SETTINGS_VERSION } - } + val fileContents = file.readText() + val jsonElement = jsonParser.parseToJsonElement(fileContents) + decode(jsonElement) } else { null } } + fun decode(element: JsonElement): HomePageSettings { + val rowsElement = element.jsonObject["rows"]?.jsonArray + val rows = + rowsElement + ?.mapNotNull { row -> + try { + jsonParser.decodeFromJsonElement(row) + } catch (ex: Exception) { + Timber.w(ex, "Unknown row %s", row) + // TODO maybe use placeholder instead of null? + null + } + }.orEmpty() + return HomePageSettings(rows) + } + suspend fun updateCurrent(userId: UUID) { Timber.v("Getting setting for %s", userId) // User local then server/remote otherwise create a default diff --git a/app/src/test/java/com/github/damontecres/wholphin/test/TestHomeRowSamples.kt b/app/src/test/java/com/github/damontecres/wholphin/test/TestHomeRowSamples.kt index 5ab69cad..d0289348 100644 --- a/app/src/test/java/com/github/damontecres/wholphin/test/TestHomeRowSamples.kt +++ b/app/src/test/java/com/github/damontecres/wholphin/test/TestHomeRowSamples.kt @@ -3,9 +3,11 @@ package com.github.damontecres.wholphin.test import com.github.damontecres.wholphin.data.model.HomeRowConfig import com.github.damontecres.wholphin.data.model.HomeRowViewOptions import com.github.damontecres.wholphin.preferences.PrefContentScale +import com.github.damontecres.wholphin.services.HomeSettingsService import com.github.damontecres.wholphin.ui.AspectRatio import com.github.damontecres.wholphin.ui.components.ViewOptionImageType import com.github.damontecres.wholphin.ui.data.SortAndDirection +import io.mockk.mockk import kotlinx.serialization.json.Json import org.jellyfin.sdk.model.UUID import org.jellyfin.sdk.model.api.BaseItemKind @@ -112,4 +114,37 @@ class TestHomeRowSamples { println(string) json.decodeFromString>(string) } + + @Test + fun test() { + val service = + HomeSettingsService( + context = mockk(), + api = mockk(), + userPreferencesService = mockk(), + navDrawerItemRepository = mockk(), + latestNextUpService = mockk(), + imageUrlService = mockk(), + ) + + val str = """{ + "type": "HomePageSettings", + "version": 1, + "rows": [ + { + "type": "RecentlyAdded", + "id": 0, + "parentId": "1dd1c2fd-2e1b-48e4-ba94-17a2350fe9cf" + }, + { + "type": "Does not exist", + "viewOptions": {} + } + ] + }""" + + val jsonElement = service.jsonParser.parseToJsonElement(str) + val settings = service.decode(jsonElement) + Assert.assertEquals(1, settings.rows.size) + } }