mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 00:10:32 +05:30
fix: preserve the video order of imported playlists
This commit is contained in:
parent
bd6b9eb052
commit
c4cc2ca6ac
@ -11,6 +11,7 @@ import com.github.libretube.constants.YOUTUBE_FRONTEND_URL
|
||||
import com.github.libretube.db.DatabaseHolder
|
||||
import com.github.libretube.db.obj.LocalPlaylist
|
||||
import com.github.libretube.enums.PlaylistType
|
||||
import com.github.libretube.extensions.parallelMap
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
import com.github.libretube.helpers.ProxyHelper
|
||||
@ -188,20 +189,16 @@ object PlaylistsHelper {
|
||||
} else {
|
||||
// if not logged in, all video information needs to become fetched manually
|
||||
// Only do so with 20 videos at once to prevent performance issues
|
||||
playlist.videos.mapIndexed { index, id -> id to index }
|
||||
.groupBy { it.second % 20 }.forEach { (_, videos) ->
|
||||
videos.map {
|
||||
async {
|
||||
runCatching {
|
||||
val stream = RetrofitInstance.api.getStreams(it.first)
|
||||
.toStreamItem(
|
||||
it.first
|
||||
)
|
||||
addToPlaylist(playlistId, stream)
|
||||
}
|
||||
val streams = playlist.videos.mapIndexed { index, id -> id to index }
|
||||
.groupBy { it.second % 20 }.map { (_, videos) ->
|
||||
videos.parallelMap {
|
||||
runCatching {
|
||||
RetrofitInstance.api.getStreams(it.first)
|
||||
.toStreamItem(it.first)
|
||||
}
|
||||
}.awaitAll()
|
||||
}.mapNotNull { it.getOrNull() }
|
||||
}
|
||||
addToPlaylist(playlistId, *streams.flatten().toTypedArray())
|
||||
}
|
||||
}
|
||||
}.awaitAll()
|
||||
|
@ -0,0 +1,10 @@
|
||||
package com.github.libretube.extensions
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
|
||||
suspend fun <A, B> List<A>.parallelMap(f: suspend (A) -> B): List<B> = coroutineScope {
|
||||
map { async(Dispatchers.IO) { f(it) } }.awaitAll()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user