Refactoring

This commit is contained in:
Damontecres 2026-01-25 01:16:38 -05:00
parent d7236e7bbf
commit 668a3960f3
No known key found for this signature in database
6 changed files with 56 additions and 45 deletions

View file

@ -21,11 +21,11 @@ import org.jellyfin.sdk.model.serializer.UUIDSerializer
import java.util.UUID
@Serializable
sealed class HomeRowConfig {
abstract val id: Int
abstract val viewOptions: HomeRowViewOptions
sealed interface HomeRowConfig {
val id: Int
val viewOptions: HomeRowViewOptions
abstract fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig
fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig
/**
* Continue watching media that the user has started but not finished
@ -35,8 +35,8 @@ sealed class HomeRowConfig {
data class ContinueWatching(
override val id: Int,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): ContinueWatching = this.copy(viewOptions = viewOptions)
}
/**
@ -47,8 +47,8 @@ sealed class HomeRowConfig {
data class NextUp(
override val id: Int,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): NextUp = this.copy(viewOptions = viewOptions)
}
/**
@ -59,8 +59,8 @@ sealed class HomeRowConfig {
data class ContinueWatchingCombined(
override val id: Int,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): ContinueWatchingCombined = this.copy(viewOptions = viewOptions)
}
/**
@ -72,8 +72,8 @@ sealed class HomeRowConfig {
override val id: Int,
val parentId: UUID,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): RecentlyAdded = this.copy(viewOptions = viewOptions)
}
/**
@ -85,8 +85,8 @@ sealed class HomeRowConfig {
override val id: Int,
val parentId: UUID,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): RecentlyReleased = this.copy(viewOptions = viewOptions)
}
/**
@ -102,8 +102,8 @@ sealed class HomeRowConfig {
heightDp = (Cards.HEIGHT_2X3_DP * .75f).toInt().let { it - it % 4 },
aspectRatio = AspectRatio.WIDE,
),
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): Genres = this.copy(viewOptions = viewOptions)
}
/**
@ -117,8 +117,8 @@ sealed class HomeRowConfig {
val recursive: Boolean,
val sort: SortAndDirection? = null,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): ByParent = this.copy(viewOptions = viewOptions)
}
/**
@ -131,34 +131,26 @@ sealed class HomeRowConfig {
val name: String,
val getItems: GetItemsRequest,
override val viewOptions: HomeRowViewOptions,
) : HomeRowConfig() {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): HomeRowConfig = this.copy(viewOptions = viewOptions)
) : HomeRowConfig {
override fun updateViewOptions(viewOptions: HomeRowViewOptions): GetItems = this.copy(viewOptions = viewOptions)
}
}
data class HomeRowConfigDisplay(
val title: String,
val config: HomeRowConfig,
)
@Serializable
@SerialName("HomePageSettings")
data class HomePageSettings(
val rows: List<HomeRowConfig>,
val version: Int = 1,
val version: Int = SUPPORTED_HOME_PAGE_SETTINGS_VERSION,
) {
companion object {
val EMPTY = HomePageSettings(listOf())
}
}
data class HomePageResolvedSettings(
val rows: List<HomeRowConfigDisplay>,
) {
companion object {
val EMPTY = HomePageResolvedSettings(listOf())
}
}
/**
* This is the max version supported by this version of the app
*/
const val SUPPORTED_HOME_PAGE_SETTINGS_VERSION = 1
@Serializable
data class HomeRowViewOptions(

View file

@ -4,11 +4,10 @@ import android.content.Context
import com.github.damontecres.wholphin.R
import com.github.damontecres.wholphin.data.NavDrawerItemRepository
import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.HomePageResolvedSettings
import com.github.damontecres.wholphin.data.model.HomePageSettings
import com.github.damontecres.wholphin.data.model.HomeRowConfig
import com.github.damontecres.wholphin.data.model.HomeRowConfigDisplay
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
@ -86,7 +85,9 @@ class HomeSettingsService
): HomePageSettings? {
val current = getDisplayPreferences(userId, displayPreferencesId)
return current.customPrefs[DISPLAY_PREF_ID]?.let {
Json.decodeFromString<HomePageSettings>(it)
Json
.decodeFromString<HomePageSettings>(it)
.takeIf { it.version <= SUPPORTED_HOME_PAGE_SETTINGS_VERSION }
}
}
@ -120,7 +121,9 @@ class HomeSettingsService
val file = File(dir, filename(userId))
return if (file.exists()) {
file.inputStream().use {
jsonParser.decodeFromStream<HomePageSettings>(it)
jsonParser
.decodeFromStream<HomePageSettings>(it)
.takeIf { it.version <= SUPPORTED_HOME_PAGE_SETTINGS_VERSION }
}
} else {
null
@ -534,3 +537,16 @@ class HomeSettingsService
const val CUSTOM_PREF_ID = "home_settings"
}
}
data class HomeRowConfigDisplay(
val title: String,
val config: HomeRowConfig,
)
data class HomePageResolvedSettings(
val rows: List<HomeRowConfigDisplay>,
) {
companion object {
val EMPTY = HomePageResolvedSettings(listOf())
}
}

View file

@ -11,7 +11,6 @@ import com.github.damontecres.wholphin.api.seerr.model.PublicSettings
import com.github.damontecres.wholphin.api.seerr.model.User
import com.github.damontecres.wholphin.data.SeerrServerDao
import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.HomePageResolvedSettings
import com.github.damontecres.wholphin.data.model.SeerrAuthMethod
import com.github.damontecres.wholphin.data.model.SeerrPermission
import com.github.damontecres.wholphin.data.model.SeerrServer

View file

@ -6,10 +6,10 @@ import androidx.lifecycle.viewModelScope
import com.github.damontecres.wholphin.data.NavDrawerItemRepository
import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.HomePageResolvedSettings
import com.github.damontecres.wholphin.services.BackdropService
import com.github.damontecres.wholphin.services.DatePlayedService
import com.github.damontecres.wholphin.services.FavoriteWatchManager
import com.github.damontecres.wholphin.services.HomePageResolvedSettings
import com.github.damontecres.wholphin.services.HomeSettingsService
import com.github.damontecres.wholphin.services.NavigationManager
import com.github.damontecres.wholphin.services.UserPreferencesService

View file

@ -34,7 +34,7 @@ import androidx.tv.material3.MaterialTheme
import androidx.tv.material3.Text
import androidx.tv.material3.surfaceColorAtElevation
import com.github.damontecres.wholphin.R
import com.github.damontecres.wholphin.data.model.HomeRowConfigDisplay
import com.github.damontecres.wholphin.services.HomeRowConfigDisplay
import com.github.damontecres.wholphin.ui.FontAwesome
import com.github.damontecres.wholphin.ui.components.Button
import com.github.damontecres.wholphin.ui.tryRequestFocus
@ -108,7 +108,7 @@ fun HomeSettingsRowList(
config = row,
moveUpAllowed = index > 0,
moveDownAllowed = index != state.rows.lastIndex,
deleteAllowed = true,
deleteAllowed = state.rows.size > 1,
onClickMove = { onClickMove.invoke(it, index) },
onClickDelete = { onClickDelete.invoke(index) },
onClick = { onClick.invoke(index, row) },

View file

@ -9,12 +9,12 @@ import com.github.damontecres.wholphin.R
import com.github.damontecres.wholphin.data.NavDrawerItemRepository
import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.HomePageResolvedSettings
import com.github.damontecres.wholphin.data.model.HomePageSettings
import com.github.damontecres.wholphin.data.model.HomeRowConfig
import com.github.damontecres.wholphin.data.model.HomeRowConfigDisplay
import com.github.damontecres.wholphin.data.model.HomeRowViewOptions
import com.github.damontecres.wholphin.services.BackdropService
import com.github.damontecres.wholphin.services.HomePageResolvedSettings
import com.github.damontecres.wholphin.services.HomeRowConfigDisplay
import com.github.damontecres.wholphin.services.HomeSettingsService
import com.github.damontecres.wholphin.services.UserPreferencesService
import com.github.damontecres.wholphin.services.hilt.IoCoroutineScope
@ -316,8 +316,12 @@ class HomeSettingsViewModel
val settings = HomePageSettings(rows = rows)
try {
Timber.d("saveToLocal")
homeSettingsService.saveToLocal(user.id, settings)
showToast(context, context.getString(R.string.save), Toast.LENGTH_SHORT)
val local = homeSettingsService.loadFromLocal(user.id)
// Only save if there are changes
if (local != settings) {
homeSettingsService.saveToLocal(user.id, settings)
showToast(context, context.getString(R.string.save), Toast.LENGTH_SHORT)
}
} catch (ex: Exception) {
Timber.e(ex)
showToast(context, "Error saving: ${ex.localizedMessage}")