mirror of
https://github.com/mihonapp/mihon.git
synced 2026-10-16 13:48:09 -07:00
Look up queued and downloaded chapters once per chapter list
This commit is contained in:
@@ -124,6 +124,27 @@ class DownloadCache(
|
||||
.launchIn(scope)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ids of the downloaded chapters among the given chapters of one manga.
|
||||
*
|
||||
* @param chapters the chapters to query, all of the same manga.
|
||||
* @param mangaTitle the title of the manga to query.
|
||||
* @param sourceId the id of the source of the chapters.
|
||||
*/
|
||||
fun getDownloadedChapterIds(chapters: List<Chapter>, mangaTitle: String, sourceId: Long): Set<Long> {
|
||||
renewCache()
|
||||
|
||||
val mangaDir = rootDownloadsDir.sourceDirs[sourceId]?.mangaDirs?.get(provider.getMangaDirName(mangaTitle))
|
||||
if (mangaDir == null || mangaDir.chapterDirs.isEmpty()) return emptySet()
|
||||
|
||||
return chapters
|
||||
.filter { chapter ->
|
||||
provider.getValidChapterDirNames(chapter.name, chapter.scanlator, chapter.url)
|
||||
.any { it in mangaDir.chapterDirs }
|
||||
}
|
||||
.mapTo(HashSet()) { it.id }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the chapter is downloaded.
|
||||
*
|
||||
|
||||
@@ -93,6 +93,13 @@ class DownloadManager(
|
||||
return queueState.value.find { it.chapter.id == chapterId }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the queued downloads by chapter id, for looking up many chapters at once.
|
||||
*/
|
||||
fun getQueuedDownloadsByChapterId(): Map<Long, Download> {
|
||||
return queueState.value.associateBy { it.chapter.id }
|
||||
}
|
||||
|
||||
fun startDownloadNow(chapterId: Long) {
|
||||
val existingDownload = getQueuedDownloadOrNull(chapterId)
|
||||
// If not in queue try to start a new download
|
||||
@@ -172,6 +179,16 @@ class DownloadManager(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ids of the downloaded chapters among the given chapters of the manga.
|
||||
*
|
||||
* @param chapters the chapters to query, all of [manga].
|
||||
* @param manga the manga of the chapters.
|
||||
*/
|
||||
fun getDownloadedChapterIds(chapters: List<Chapter>, manga: Manga): Set<Long> {
|
||||
return cache.getDownloadedChapterIds(chapters, manga.title, manga.source)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the chapter is downloaded.
|
||||
*
|
||||
|
||||
@@ -543,23 +543,11 @@ class MangaViewModel(
|
||||
|
||||
private fun List<Chapter>.toChapterListItems(manga: Manga): List<ChapterList.Item> {
|
||||
val isLocal = manga.isLocal()
|
||||
val queuedDownloads = if (isLocal) emptyMap() else downloadManager.getQueuedDownloadsByChapterId()
|
||||
val downloadedChapterIds = if (isLocal) emptySet() else downloadManager.getDownloadedChapterIds(this, manga)
|
||||
return map { chapter ->
|
||||
val activeDownload = if (isLocal) {
|
||||
null
|
||||
} else {
|
||||
downloadManager.getQueuedDownloadOrNull(chapter.id)
|
||||
}
|
||||
val downloaded = if (isLocal) {
|
||||
true
|
||||
} else {
|
||||
downloadManager.isChapterDownloaded(
|
||||
chapter.name,
|
||||
chapter.scanlator,
|
||||
chapter.url,
|
||||
manga.title,
|
||||
manga.source,
|
||||
)
|
||||
}
|
||||
val activeDownload = queuedDownloads[chapter.id]
|
||||
val downloaded = isLocal || chapter.id in downloadedChapterIds
|
||||
val downloadState = when {
|
||||
activeDownload != null -> activeDownload.status
|
||||
downloaded -> Download.State.DOWNLOADED
|
||||
|
||||
@@ -211,9 +211,10 @@ class UpdatesViewModel(
|
||||
}
|
||||
|
||||
private fun List<UpdatesWithRelations>.toUpdateItems(): List<UpdatesItem> {
|
||||
val queuedDownloads = downloadManager.getQueuedDownloadsByChapterId()
|
||||
return this
|
||||
.map { update ->
|
||||
val activeDownload = downloadManager.getQueuedDownloadOrNull(update.chapterId)
|
||||
val activeDownload = queuedDownloads[update.chapterId]
|
||||
val downloaded = downloadManager.isChapterDownloaded(
|
||||
update.chapterName,
|
||||
update.scanlator,
|
||||
|
||||
Reference in New Issue
Block a user