Use Kotlinx Serialization with playlists.

This commit is contained in:
Isira Seneviratne 2023-01-19 09:17:45 +05:30
parent 4876068c54
commit 1eeeed4292
6 changed files with 43 additions and 65 deletions

View File

@ -93,10 +93,7 @@ object PlaylistsHelper {
}.last().playlist.id.toString() }.last().playlist.id.toString()
} }
val response = try { val response = try {
RetrofitInstance.authApi.createPlaylist( RetrofitInstance.authApi.createPlaylist(token, Playlists(name = playlistName))
token,
Playlists(name = playlistName)
)
} catch (e: IOException) { } catch (e: IOException) {
appContext.toastFromMainThread(R.string.unknown_error) appContext.toastFromMainThread(R.string.unknown_error)
return null return null
@ -107,7 +104,7 @@ object PlaylistsHelper {
} }
if (response.playlistId != null) { if (response.playlistId != null) {
appContext.toastFromMainThread(R.string.playlistCreated) appContext.toastFromMainThread(R.string.playlistCreated)
return response.playlistId!! return response.playlistId
} }
return null return null
} }
@ -141,13 +138,8 @@ object PlaylistsHelper {
return true return true
} }
return RetrofitInstance.authApi.addToPlaylist( val playlist = PlaylistId(playlistId, videoIds = videos.map { it.url!!.toID() })
token, return RetrofitInstance.authApi.addToPlaylist(token, playlist).message == "ok"
PlaylistId(
playlistId = playlistId,
videoIds = videos.toList().map { it.url!!.toID() }
)
).message == "ok"
} }
suspend fun renamePlaylist(playlistId: String, newName: String): Boolean { suspend fun renamePlaylist(playlistId: String, newName: String): Boolean {
@ -164,10 +156,7 @@ object PlaylistsHelper {
return RetrofitInstance.authApi.renamePlaylist( return RetrofitInstance.authApi.renamePlaylist(
token, token,
PlaylistId( PlaylistId(playlistId, newName = newName)
playlistId = playlistId,
newName = newName
)
).playlistId != null ).playlistId != null
} }
@ -251,8 +240,8 @@ object PlaylistsHelper {
name = list.name, name = list.name,
type = "playlist", type = "playlist",
visibility = "private", visibility = "private",
videos = list.relatedStreams.orEmpty().map { videos = list.relatedStreams.map {
YOUTUBE_FRONTEND_URL + "/watch?v=" + it.url!!.toID() "$YOUTUBE_FRONTEND_URL/watch?v=${it.url!!.toID()}"
} }
) )
) )
@ -278,19 +267,13 @@ object PlaylistsHelper {
val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext) val newPlaylist = createPlaylist(playlist.name ?: "Unknown name", appContext)
newPlaylist ?: return@launch newPlaylist ?: return@launch
addToPlaylist( addToPlaylist(newPlaylist, *playlist.relatedStreams.toTypedArray())
newPlaylist,
*playlist.relatedStreams.orEmpty().toTypedArray()
)
var nextPage = playlist.nextpage var nextPage = playlist.nextpage
while (nextPage != null) { while (nextPage != null) {
nextPage = try { nextPage = try {
RetrofitInstance.api.getPlaylistNextPage(playlistId, nextPage).apply { RetrofitInstance.api.getPlaylistNextPage(playlistId, nextPage).apply {
addToPlaylist( addToPlaylist(newPlaylist, *relatedStreams.toTypedArray())
newPlaylist,
*relatedStreams.orEmpty().toTypedArray()
)
}.nextpage }.nextpage
} catch (e: Exception) { } catch (e: Exception) {
return@launch return@launch

View File

@ -1,5 +1,6 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
data class Message( import kotlinx.serialization.Serializable
var message: String? = null
) @Serializable
data class Message(val message: String? = null)

View File

@ -1,16 +1,16 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true) @Serializable
data class Playlist( data class Playlist(
var name: String? = null, val name: String? = null,
var thumbnailUrl: String? = null, val thumbnailUrl: String? = null,
var bannerUrl: String? = null, val bannerUrl: String? = null,
var nextpage: String? = null, val nextpage: String? = null,
var uploader: String? = null, val uploader: String? = null,
var uploaderUrl: String? = null, val uploaderUrl: String? = null,
var uploaderAvatar: String? = null, val uploaderAvatar: String? = null,
var videos: Int? = 0, val videos: Int = 0,
var relatedStreams: List<StreamItem>? = null val relatedStreams: List<StreamItem> = emptyList()
) )

View File

@ -1,12 +1,12 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true) @Serializable
data class PlaylistId( data class PlaylistId(
var playlistId: String? = null, val playlistId: String? = null,
var videoId: String? = null, val videoId: String? = null,
var videoIds: List<String>? = null, val videoIds: List<String> = emptyList(),
var newName: String? = null, val newName: String? = null,
var index: Int = -1 val index: Int = -1
) )

View File

@ -1,12 +1,12 @@
package com.github.libretube.api.obj package com.github.libretube.api.obj
import com.fasterxml.jackson.annotation.JsonIgnoreProperties import kotlinx.serialization.Serializable
@JsonIgnoreProperties(ignoreUnknown = true) @Serializable
data class Playlists( data class Playlists(
var id: String? = null, val id: String? = null,
var name: String? = null, val name: String? = null,
var shortDescription: String? = null, val shortDescription: String? = null,
var thumbnail: String? = null, val thumbnail: String? = null,
var videos: Long? = null val videos: Long = 0
) )

View File

@ -109,7 +109,7 @@ class PlaylistFragment : BaseFragment() {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
return@launchWhenCreated return@launchWhenCreated
} }
playlistFeed = response.relatedStreams.orEmpty().toMutableList() playlistFeed = response.relatedStreams.toMutableList()
binding.playlistScrollview.visibility = View.VISIBLE binding.playlistScrollview.visibility = View.VISIBLE
nextPage = response.nextpage nextPage = response.nextpage
playlistName = response.name playlistName = response.name
@ -140,7 +140,7 @@ class PlaylistFragment : BaseFragment() {
if (playlistFeed.isEmpty()) return@setOnClickListener if (playlistFeed.isEmpty()) return@setOnClickListener
NavigationHelper.navigateVideo( NavigationHelper.navigateVideo(
requireContext(), requireContext(),
response.relatedStreams!!.first().url?.toID(), response.relatedStreams.first().url?.toID(),
playlistId playlistId
) )
} }
@ -279,15 +279,9 @@ class PlaylistFragment : BaseFragment() {
val response = try { val response = try {
// load locally stored playlists with the auth api // load locally stored playlists with the auth api
if (playlistType == PlaylistType.PRIVATE) { if (playlistType == PlaylistType.PRIVATE) {
RetrofitInstance.authApi.getPlaylistNextPage( RetrofitInstance.authApi.getPlaylistNextPage(playlistId!!, nextPage!!)
playlistId!!,
nextPage!!
)
} else { } else {
RetrofitInstance.api.getPlaylistNextPage( RetrofitInstance.api.getPlaylistNextPage(playlistId!!, nextPage!!)
playlistId!!,
nextPage!!
)
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG(), e.toString()) Log.e(TAG(), e.toString())
@ -295,7 +289,7 @@ class PlaylistFragment : BaseFragment() {
} }
nextPage = response.nextpage nextPage = response.nextpage
playlistAdapter?.updateItems(response.relatedStreams!!) playlistAdapter?.updateItems(response.relatedStreams)
isLoading = false isLoading = false
} }
} }