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

View File

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

View File

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

View File

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

View File

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

View File

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