package eu.friedcutlet.yokan.services.download import eu.friedcutlet.yokan.database.DbDownload /** * Arranges queued downloads by the user's manual priority order, matching * Mangayomi's `DownloadQueueOrder`. * * Downloads present in [order] come first in their recorded order. Downloads * absent from [order] come after, maintaining their natural incoming order. */ object DownloadQueueOrder { fun sorted(items: List, order: List): List { if (order.isEmpty()) return items val rank = order.withIndex().associate { it.value to it.index } val indexed = items.withIndex().toList() return indexed.sortedWith { a, b -> val ra = rank[a.value.id] ?: (order.size + a.index) val rb = rank[b.value.id] ?: (order.size + b.index) ra.compareTo(rb) }.map { it.value } } }