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

This commit is contained in:
len 2017-10-05 10:15:02 +02:00
parent f648940388
commit 5c662b1ae1

View file

@ -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,20 +220,31 @@ 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
// Background, long running checks
.fromCallable {
val mangaDir = provider.findMangaDir(source, manga)
chapters
// Avoid downloading chapters with the same name. // Avoid downloading chapters with the same name.
.distinctBy { it.name } .distinctBy { it.name }
// Filter out those already downloaded.
.filter { mangaDir?.findFile(provider.getChapterDirName(it)) == null }
// Add chapters to queue from the start. // Add chapters to queue from the start.
.sortedByDescending { it.source_order } .sortedByDescending { it.source_order }
// Create a downloader for each one. }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
// Main thread, quick checks
.map { chaptersToQueue ->
chaptersToQueue
// Filter out those already enqueued.
.filter { chapter -> queue.none { it.chapter.id == chapter.id } }
// Create a download for each one.
.map { Download(source, manga, it) } .map { Download(source, manga, it) }
// Filter out those already queued or downloaded. }
.filter { isDownloadAllowed(it) } .subscribe { chaptersToQueue ->
if (chaptersToQueue.isNotEmpty()) {
// Return if there's nothing to queue.
if (chaptersToQueue.isEmpty())
return
queue.addAll(chaptersToQueue) queue.addAll(chaptersToQueue)
// Initialize queue size. // Initialize queue size.
@ -252,22 +261,7 @@ class Downloader(private val context: Context, private val provider: DownloadPro
notifier.onProgressChange(queue) notifier.onProgressChange(queue)
} }
} }
}
/**
* Returns true if the given download can be queued and downloaded.
*
* @param download the download to be checked.
*/
private fun isDownloadAllowed(download: Download): Boolean {
// If the chapter is already queued, don't add it again
if (queue.any { it.chapter.id == download.chapter.id })
return false
val dir = provider.findChapterDir(download.source, download.manga, download.chapter)
if (dir != null && dir.exists())
return false
return true
} }
/** /**