Fix app freezes when queueing many chapters with SAF. Closes #817

pull/1024/head
len 7 years ago
parent f648940388
commit 5c662b1ae1

@ -91,7 +91,6 @@ class Downloader(private val context: Context, private val provider: DownloadPro
init { init {
Observable.fromCallable { store.restore() } Observable.fromCallable { store.restore() }
.map { downloads -> downloads.filter { isDownloadAllowed(it) } }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ downloads -> queue.addAll(downloads) .subscribe({ downloads -> queue.addAll(downloads)
@ -213,8 +212,7 @@ class Downloader(private val context: Context, private val provider: DownloadPro
} }
/** /**
* Creates a download object for every chapter and adds them to the downloads queue. This method * Creates a download object for every chapter and adds them to the downloads queue.
* must be called in the main thread.
* *
* @param manga the manga of the chapters to download. * @param manga the manga of the chapters to download.
* @param chapters the list of chapters to download. * @param chapters the list of chapters to download.
@ -222,52 +220,48 @@ class Downloader(private val context: Context, private val provider: DownloadPro
fun queueChapters(manga: Manga, chapters: List<Chapter>) { fun queueChapters(manga: Manga, chapters: List<Chapter>) {
val source = sourceManager.get(manga.source) as? HttpSource ?: return val source = sourceManager.get(manga.source) as? HttpSource ?: return
val chaptersToQueue = chapters Observable
// Avoid downloading chapters with the same name. // Background, long running checks
.distinctBy { it.name } .fromCallable {
// Add chapters to queue from the start. val mangaDir = provider.findMangaDir(source, manga)
.sortedByDescending { it.source_order }
// Create a downloader for each one. chapters
.map { Download(source, manga, it) } // Avoid downloading chapters with the same name.
// Filter out those already queued or downloaded. .distinctBy { it.name }
.filter { isDownloadAllowed(it) } // Filter out those already downloaded.
.filter { mangaDir?.findFile(provider.getChapterDirName(it)) == null }
// Return if there's nothing to queue. // Add chapters to queue from the start.
if (chaptersToQueue.isEmpty()) .sortedByDescending { it.source_order }
return }
.subscribeOn(Schedulers.io())
queue.addAll(chaptersToQueue) .observeOn(AndroidSchedulers.mainThread())
// Main thread, quick checks
// Initialize queue size. .map { chaptersToQueue ->
notifier.initialQueueSize = queue.size chaptersToQueue
// Filter out those already enqueued.
// Initial multi-thread .filter { chapter -> queue.none { it.chapter.id == chapter.id } }
notifier.multipleDownloadThreads = preferences.downloadThreads().getOrDefault() > 1 // Create a download for each one.
.map { Download(source, manga, it) }
if (isRunning) { }
// Send the list of downloads to the downloader. .subscribe { chaptersToQueue ->
downloadsRelay.call(chaptersToQueue) if (chaptersToQueue.isNotEmpty()) {
} else { queue.addAll(chaptersToQueue)
// Show initial notification.
notifier.onProgressChange(queue) // Initialize queue size.
} notifier.initialQueueSize = queue.size
}
// Initial multi-thread
/** notifier.multipleDownloadThreads = preferences.downloadThreads().getOrDefault() > 1
* Returns true if the given download can be queued and downloaded.
* if (isRunning) {
* @param download the download to be checked. // Send the list of downloads to the downloader.
*/ downloadsRelay.call(chaptersToQueue)
private fun isDownloadAllowed(download: Download): Boolean { } else {
// If the chapter is already queued, don't add it again // Show initial notification.
if (queue.any { it.chapter.id == download.chapter.id }) notifier.onProgressChange(queue)
return false }
}
val dir = provider.findChapterDir(download.source, download.manga, download.chapter) }
if (dir != null && dir.exists())
return false
return true
} }
/** /**

Loading…
Cancel
Save