mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Import playlists asynchronously
This commit is contained in:
parent
f274dd205b
commit
a096247c94
@ -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 {
|
||||||
|
val playlistId = createPlaylist(playlist.name!!, null) ?: return@async
|
||||||
// if logged in, add the playlists by their ID via an api call
|
// if logged in, add the playlists by their ID via an api call
|
||||||
if (loggedIn) {
|
if (loggedIn) {
|
||||||
addToPlaylist(playlistId, *playlist.videos.map {
|
addToPlaylist(
|
||||||
|
playlistId,
|
||||||
|
*playlist.videos.map {
|
||||||
StreamItem(url = it)
|
StreamItem(url = it)
|
||||||
}.toTypedArray())
|
}.toTypedArray()
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
// if not logged in, all video information needs to become fetched manually
|
// if not logged in, all video information needs to become fetched manually
|
||||||
runCatching {
|
runCatching {
|
||||||
val streamItems = playlist.videos.map {
|
val streamItems = playlist.videos.map {
|
||||||
RetrofitInstance.api.getStreams(it).toStreamItem(it)
|
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) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user