Use awaitAll() extension.

This commit is contained in:
Isira Seneviratne 2023-01-30 20:14:20 +05:30
parent 259a1c1400
commit 825fc7bda5

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,30 +229,16 @@ 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) {