diff --git a/app/src/main/java/com/github/damontecres/wholphin/data/model/DiscoverItem.kt b/app/src/main/java/com/github/damontecres/wholphin/data/model/DiscoverItem.kt index 738bdbc5..10362b41 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/data/model/DiscoverItem.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/data/model/DiscoverItem.kt @@ -12,12 +12,15 @@ import com.github.damontecres.wholphin.api.seerr.model.TvResult import com.github.damontecres.wholphin.api.seerr.model.TvTvIdRatingsGet200Response import com.github.damontecres.wholphin.services.SeerrSearchResult import com.github.damontecres.wholphin.ui.nav.Destination +import com.github.damontecres.wholphin.ui.toLocalDate +import com.github.damontecres.wholphin.util.LocalDateSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers import org.jellyfin.sdk.model.api.BaseItemKind import org.jellyfin.sdk.model.serializer.UUIDSerializer import org.jellyfin.sdk.model.serializer.toUUIDOrNull +import java.time.LocalDate import java.util.UUID @Serializable @@ -73,7 +76,7 @@ data class DiscoverItem( val subtitle: String?, val overview: String?, val availability: SeerrAvailability, - val releaseDate: String?, + @Serializable(LocalDateSerializer::class) val releaseDate: LocalDate?, val posterPath: String?, val backdropPath: String?, val jellyfinItemId: UUID?, @@ -107,7 +110,7 @@ data class DiscoverItem( subtitle = null, overview = movie.overview, availability = SeerrAvailability.from(movie.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = movie.releaseDate, + releaseDate = toLocalDate(movie.releaseDate), posterPath = movie.posterPath, backdropPath = movie.backdropPath, jellyfinItemId = movie.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -120,7 +123,7 @@ data class DiscoverItem( subtitle = null, overview = movie.overview, availability = SeerrAvailability.from(movie.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = movie.releaseDate, + releaseDate = toLocalDate(movie.releaseDate), posterPath = movie.posterPath, backdropPath = movie.backdropPath, jellyfinItemId = movie.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -133,7 +136,7 @@ data class DiscoverItem( subtitle = null, overview = tv.overview, availability = SeerrAvailability.from(tv.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = tv.firstAirDate, + releaseDate = toLocalDate(tv.firstAirDate), posterPath = tv.posterPath, backdropPath = tv.backdropPath, jellyfinItemId = tv.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -146,7 +149,7 @@ data class DiscoverItem( subtitle = null, overview = tv.overview, availability = SeerrAvailability.from(tv.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = tv.firstAirDate, + releaseDate = toLocalDate(tv.firstAirDate), posterPath = tv.posterPath, backdropPath = tv.backdropPath, jellyfinItemId = tv.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -161,7 +164,7 @@ data class DiscoverItem( availability = SeerrAvailability.from(search.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = search.releaseDate ?: search.firstAirDate, + releaseDate = toLocalDate(search.releaseDate ?: search.firstAirDate), posterPath = search.posterPath, backdropPath = search.backdropPath, jellyfinItemId = search.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -176,7 +179,7 @@ data class DiscoverItem( availability = SeerrAvailability.from(credit.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = credit.firstAirDate, + releaseDate = toLocalDate(credit.firstAirDate), posterPath = credit.posterPath, backdropPath = credit.backdropPath, jellyfinItemId = credit.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), @@ -191,7 +194,7 @@ data class DiscoverItem( availability = SeerrAvailability.from(credit.mediaInfo?.status) ?: SeerrAvailability.UNKNOWN, - releaseDate = credit.firstAirDate, + releaseDate = toLocalDate(credit.firstAirDate), posterPath = credit.posterPath, backdropPath = credit.backdropPath, jellyfinItemId = credit.mediaInfo?.jellyfinMediaId?.toUUIDOrNull(), diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/Formatting.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/Formatting.kt index f6d0e467..2461b642 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/Formatting.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/Formatting.kt @@ -4,9 +4,11 @@ import androidx.annotation.StringRes import com.github.damontecres.wholphin.R import org.jellyfin.sdk.model.api.BaseItemDto import org.jellyfin.sdk.model.api.MediaSegmentType +import timber.log.Timber import java.time.LocalDate import java.time.LocalDateTime import java.time.format.DateTimeFormatter +import java.time.format.DateTimeParseException import java.time.format.FormatStyle import java.util.Locale @@ -23,6 +25,16 @@ fun formatDateTime(dateTime: LocalDateTime): String = DateFormatter.format(dateT fun formatDate(dateTime: LocalDate): String = DateFormatter.format(dateTime) +fun toLocalDate(date: String?): LocalDate? = + date?.let { + try { + LocalDate.parse(it) + } catch (_: DateTimeParseException) { + Timber.w("Could not parse date: %s", date) + null + } + } + /** * If the item has season & episode info, format as `S# E#` */ diff --git a/app/src/main/java/com/github/damontecres/wholphin/ui/cards/DiscoverItemCard.kt b/app/src/main/java/com/github/damontecres/wholphin/ui/cards/DiscoverItemCard.kt index 7794f216..7daaa297 100644 --- a/app/src/main/java/com/github/damontecres/wholphin/ui/cards/DiscoverItemCard.kt +++ b/app/src/main/java/com/github/damontecres/wholphin/ui/cards/DiscoverItemCard.kt @@ -189,7 +189,7 @@ fun DiscoverItemCard( .enableMarquee(focusedAfterDelay), ) Text( - text = item?.releaseDate ?: "", + text = item?.releaseDate?.year?.toString() ?: "", maxLines = 1, textAlign = TextAlign.Center, modifier = diff --git a/app/src/main/java/com/github/damontecres/wholphin/util/LocalDateSerializer.kt b/app/src/main/java/com/github/damontecres/wholphin/util/LocalDateSerializer.kt new file mode 100644 index 00000000..89b900f0 --- /dev/null +++ b/app/src/main/java/com/github/damontecres/wholphin/util/LocalDateSerializer.kt @@ -0,0 +1,24 @@ +package com.github.damontecres.wholphin.util + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import java.time.LocalDate +import java.time.format.DateTimeFormatter + +object LocalDateSerializer : KSerializer { + override val descriptor: SerialDescriptor + get() = SerialDescriptor("LocalDate", String.serializer().descriptor) + + override fun serialize( + encoder: Encoder, + value: LocalDate, + ) { + encoder.encodeString(DateTimeFormatter.ISO_LOCAL_DATE.format(value)) + } + + override fun deserialize(decoder: Decoder): LocalDate = + decoder.decodeString().let { LocalDate.parse(it, DateTimeFormatter.ISO_LOCAL_DATE) } +}