fix: preserve the video order of imported playlists

This commit is contained in:
Bnyro 2023-07-31 15:13:49 +02:00
parent bd6b9eb052
commit c4cc2ca6ac
2 changed files with 19 additions and 12 deletions

View File

@ -11,6 +11,7 @@ import com.github.libretube.constants.YOUTUBE_FRONTEND_URL
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.LocalPlaylist import com.github.libretube.db.obj.LocalPlaylist
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.parallelMap
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper import com.github.libretube.helpers.ProxyHelper
@ -188,20 +189,16 @@ object PlaylistsHelper {
} 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
// Only do so with 20 videos at once to prevent performance issues // Only do so with 20 videos at once to prevent performance issues
playlist.videos.mapIndexed { index, id -> id to index } val streams = playlist.videos.mapIndexed { index, id -> id to index }
.groupBy { it.second % 20 }.forEach { (_, videos) -> .groupBy { it.second % 20 }.map { (_, videos) ->
videos.map { videos.parallelMap {
async { runCatching {
runCatching { RetrofitInstance.api.getStreams(it.first)
val stream = RetrofitInstance.api.getStreams(it.first) .toStreamItem(it.first)
.toStreamItem(
it.first
)
addToPlaylist(playlistId, stream)
}
} }
}.awaitAll() }.mapNotNull { it.getOrNull() }
} }
addToPlaylist(playlistId, *streams.flatten().toTypedArray())
} }
} }
}.awaitAll() }.awaitAll()

View File

@ -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()
}