DownloadQueueOrder.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/download/DownloadQueueOrder.kt
1package eu.friedcutlet.yokan.services.download23import eu.friedcutlet.yokan.database.DbDownload45/**6 * Arranges queued downloads by the user's manual priority order, matching7 * Mangayomi's `DownloadQueueOrder`.8 *9 * Downloads present in [order] come first in their recorded order. Downloads10 * absent from [order] come after, maintaining their natural incoming order.11 */12object DownloadQueueOrder {13 fun sorted(items: List<DbDownload>, order: List<Long>): List<DbDownload> {14 if (order.isEmpty()) return items15 val rank = order.withIndex().associate { it.value to it.index }16 val indexed = items.withIndex().toList()17 return indexed.sortedWith { a, b ->18 val ra = rank[a.value.id] ?: (order.size + a.index)19 val rb = rank[b.value.id] ?: (order.size + b.index)20 ra.compareTo(rb)21 }.map { it.value }22 }23}24