use the new route

This commit is contained in:
Bnyro 2022-12-01 15:17:46 +01:00
parent 7c775f4937
commit 53ebd3363f
3 changed files with 28 additions and 25 deletions

View File

@ -107,8 +107,12 @@ object PlaylistsHelper {
return null return null
} }
suspend fun addToPlaylist(playlistId: String, videoId: String): Boolean { suspend fun addToPlaylist(playlistId: String, vararg videoIds: String): Boolean {
if (!loggedIn()) { if (!loggedIn()) {
val localPlaylist = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId }
for (videoId in videoIds) {
val localPlaylistItem = RetrofitInstance.api.getStreams(videoId).toLocalPlaylistItem(playlistId, videoId) val localPlaylistItem = RetrofitInstance.api.getStreams(videoId).toLocalPlaylistItem(playlistId, videoId)
awaitQuery { awaitQuery {
// avoid duplicated videos in a playlist // avoid duplicated videos in a playlist
@ -116,8 +120,6 @@ object PlaylistsHelper {
// add the new video to the database // add the new video to the database
DatabaseHolder.Database.localPlaylistsDao().addPlaylistVideo(localPlaylistItem) DatabaseHolder.Database.localPlaylistsDao().addPlaylistVideo(localPlaylistItem)
val localPlaylist = DatabaseHolder.Database.localPlaylistsDao().getAll()
.first { it.playlist.id.toString() == playlistId }
if (localPlaylist.playlist.thumbnailUrl == "") { if (localPlaylist.playlist.thumbnailUrl == "") {
// set the new playlist thumbnail URL // set the new playlist thumbnail URL
@ -127,12 +129,16 @@ object PlaylistsHelper {
} }
} }
} }
}
return true return true
} }
return RetrofitInstance.authApi.addToPlaylist( return RetrofitInstance.authApi.addToPlaylist(
token, token,
PlaylistId(playlistId, videoId) PlaylistId(
playlistId = playlistId,
videoIds = videoIds.toList()
)
).message == "ok" ).message == "ok"
} }
@ -185,16 +191,13 @@ object PlaylistsHelper {
suspend fun importPlaylists(appContext: Context, playlists: List<ImportPlaylist>) { suspend fun importPlaylists(appContext: Context, playlists: List<ImportPlaylist>) {
for (playlist in playlists) { for (playlist in playlists) {
Log.e("playlist", playlist.toString())
val playlistId = createPlaylist(playlist.name!!, appContext) ?: continue val playlistId = createPlaylist(playlist.name!!, appContext) ?: continue
runBlocking { addToPlaylist(
val tasks = playlist.videos.map { videoId -> playlistId,
async { addToPlaylist(playlistId, videoId.substringAfter("=")) } *playlist.videos.map {
} it.substringAfter("=")
tasks.forEach { }.toTypedArray()
it.await() )
}
}
} }
} }

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
data class PlaylistId( data class PlaylistId(
var playlistId: String? = null, var playlistId: String? = null,
var videoId: String? = null, var videoId: String? = null,
var videoIds: List<String>? = null,
var newName: String? = null, var newName: String? = null,
var index: Int = -1 var index: Int = -1
) )

View File

@ -17,7 +17,6 @@ import com.github.libretube.extensions.toID
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.views.TimePickerPreference import com.github.libretube.ui.views.TimePickerPreference
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.time.LocalTime import java.time.LocalTime