Merge pull request #2913 from Isira-Seneviratne/awaitAll

Use awaitAll() extension.
This commit is contained in:
Bnyro 2023-01-30 16:20:41 +01:00 committed by GitHub
commit 8c6d20d64d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,8 +24,9 @@ import java.io.IOException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext
import retrofit2.HttpException import retrofit2.HttpException
object PlaylistsHelper { object PlaylistsHelper {
@ -228,31 +229,17 @@ object PlaylistsHelper {
} }
} }
suspend fun exportPlaylists(): List<ImportPlaylist> { suspend fun exportPlaylists(): List<ImportPlaylist> = withContext(Dispatchers.IO) {
val playlists = getPlaylists() getPlaylists()
val importLists = mutableListOf<ImportPlaylist>() .map { async { getPlaylist(it.id!!) } }
runBlocking { .awaitAll()
val tasks = playlists.map { .map {
async { val videos = it.relatedStreams.map { item ->
val list = getPlaylist(it.id!!) "$YOUTUBE_FRONTEND_URL/watch?v=${item.url!!.toID()}"
importLists.add(
ImportPlaylist(
name = list.name,
type = "playlist",
visibility = "private",
videos = list.relatedStreams.map {
"$YOUTUBE_FRONTEND_URL/watch?v=${it.url!!.toID()}"
} }
) ImportPlaylist(it.name, "playlist", "private", videos)
)
} }
} }
tasks.forEach {
it.await()
}
}
return importLists
}
fun clonePlaylist(context: Context, playlistId: String) { fun clonePlaylist(context: Context, playlistId: String) {
val appContext = context.applicationContext val appContext = context.applicationContext