From 14573af2299e6e32fdb7149f290e72f6e01c41bb Mon Sep 17 00:00:00 2001 From: warrior Date: Sun, 8 Aug 2021 15:21:32 +0200 Subject: [PATCH] Add playlist classes --- .../github/warriorzz/ktify/model/Playlist.kt | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/main/kotlin/com/github/warriorzz/ktify/model/Playlist.kt b/src/main/kotlin/com/github/warriorzz/ktify/model/Playlist.kt index 6c707f9..a7f61f2 100755 --- a/src/main/kotlin/com/github/warriorzz/ktify/model/Playlist.kt +++ b/src/main/kotlin/com/github/warriorzz/ktify/model/Playlist.kt @@ -1 +1,71 @@ package com.github.warriorzz.ktify.model + +import com.github.warriorzz.ktify.model.external.ExternalUrl +import com.github.warriorzz.ktify.model.util.Image +import com.github.warriorzz.ktify.model.util.ObjectType +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonContentPolymorphicSerializer +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.jsonObject + +@Serializable +data class Playlist( + val collaborative: Boolean, + val description: String? = null, + @SerialName("external_urls") + val externalUrl: ExternalUrl, + val followers: Followers, + val href: String, + val id: String, + val images: List, + val name: String, + val owner: PublicUser, + val public: Boolean? = null, + @SerialName("snapshot_id") + val snapshotId: String, + val tracks: List? = null, + override val type: ObjectType = ObjectType.PLAYLIST, + val uri: String, +) : KtifyObject() + +@Serializable +data class PlaylistTrack( + @SerialName("added_at") + val addedAt: String? = null, + @SerialName("added_by") + val addedBy: PublicUser? = null, + @SerialName("is_local") + val isLocal: Boolean, + val track: KtifyObject, +) : PlaylistTrackObject() + +@Serializable +data class PlaylistTrackRef( + val href: String, + val total: Int +) : PlaylistTrackObject() + +@Serializable(with = PlaylistTrackObjectSerializer::class) +sealed class PlaylistTrackObject { + + /** + * Get the instance as a PlaylistTrack object + * @return Null if the instance is an PlaylistTrackRef + */ + fun asPlaylistTrack() : PlaylistTrack? = if (this is PlaylistTrack) this else null + + /** + * Get the instance as a PlaylistTrackRef object + * @return Null if the instance is an PlaylistTrack + */ + fun asPlaylistTrackRef() : PlaylistTrackRef? = if (this is PlaylistTrackRef) this else null + +} + +object PlaylistTrackObjectSerializer: JsonContentPolymorphicSerializer(PlaylistTrackObject::class) { + override fun selectDeserializer(element: JsonElement): DeserializationStrategy { + return if (element.jsonObject["track"] != null) PlaylistTrack.serializer() else PlaylistTrackRef.serializer() + } +}