Import playlists asynchronously

This commit is contained in:
Bnyro 2023-01-31 16:29:13 +01:00
parent f274dd205b
commit a096247c94

View File

@ -19,6 +19,7 @@ import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.obj.ImportPlaylist import com.github.libretube.obj.ImportPlaylist
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ProxyHelper import com.github.libretube.util.ProxyHelper
import gen._base._base_java__assetres.srcjar.R.id.async
import java.io.IOException import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.async
@ -154,24 +155,29 @@ object PlaylistsHelper {
} }
} }
suspend fun importPlaylists(playlists: List<ImportPlaylist>) { suspend fun importPlaylists(playlists: List<ImportPlaylist>) = withContext(Dispatchers.IO) {
for (playlist in playlists) { playlists.map { playlist ->
val playlistId = createPlaylist(playlist.name!!, null) ?: continue async {
// if logged in, add the playlists by their ID via an api call val playlistId = createPlaylist(playlist.name!!, null) ?: return@async
if (loggedIn) { // if logged in, add the playlists by their ID via an api call
addToPlaylist(playlistId, *playlist.videos.map { if (loggedIn) {
StreamItem(url = it) addToPlaylist(
}.toTypedArray()) playlistId,
} else { *playlist.videos.map {
// if not logged in, all video information needs to become fetched manually StreamItem(url = it)
runCatching { }.toTypedArray()
val streamItems = playlist.videos.map { )
RetrofitInstance.api.getStreams(it).toStreamItem(it) } else {
// if not logged in, all video information needs to become fetched manually
runCatching {
val streamItems = playlist.videos.map {
async { RetrofitInstance.api.getStreams(it).toStreamItem(it) }
}.awaitAll()
addToPlaylist(playlistId, *streamItems.toTypedArray())
} }
addToPlaylist(playlistId, *streamItems.toTypedArray())
} }
} }
} }.awaitAll()
} }
suspend fun exportPlaylists(): List<ImportPlaylist> = withContext(Dispatchers.IO) { suspend fun exportPlaylists(): List<ImportPlaylist> = withContext(Dispatchers.IO) {