Fixed formatting of downloader

pull/3372/head
arkon 4 years ago committed by Jay
parent 6e8c285646
commit f96f25b1f6

@ -85,7 +85,8 @@ class Downloader(
/**
* Whether the downloader is running.
*/
@Volatile private var isRunning: Boolean = false
@Volatile
private var isRunning: Boolean = false
init {
launchNow {
@ -102,11 +103,9 @@ class Downloader(
* @return true if the downloader is started, false otherwise.
*/
fun start(): Boolean {
if (isRunning || queue.isEmpty())
return false
if (isRunning || queue.isEmpty()) return false
notifier.paused = false
if (!subscriptions.hasSubscriptions())
initializeSubscriptions()
if (!subscriptions.hasSubscriptions()) initializeSubscriptions()
val pending = queue.filter { it.status != Download.DOWNLOADED }
pending.forEach { if (it.status != Download.QUEUE) it.status = Download.QUEUE }
@ -120,9 +119,7 @@ class Downloader(
*/
fun stop(reason: String? = null) {
destroySubscriptions()
queue
.filter { it.status == Download.DOWNLOADING }
.forEach { it.status = Download.ERROR }
queue.filter { it.status == Download.DOWNLOADING }.forEach { it.status = Download.ERROR }
if (reason != null) {
notifier.onWarning(reason)
@ -145,9 +142,7 @@ class Downloader(
*/
fun pause() {
destroySubscriptions()
queue
.filter { it.status == Download.DOWNLOADING }
.forEach { it.status = Download.QUEUE }
queue.filter { it.status == Download.DOWNLOADING }.forEach { it.status = Download.QUEUE }
notifier.paused = true
}
@ -166,8 +161,7 @@ class Downloader(
// Needed to update the chapter view
if (isNotification) {
queue
.filter { it.status == Download.QUEUE }
queue.filter { it.status == Download.QUEUE }
.forEach { it.status = Download.NOT_DOWNLOADED }
}
queue.clear()
@ -182,8 +176,7 @@ class Downloader(
fun clearQueue(manga: Manga, isNotification: Boolean = false) {
// Needed to update the chapter view
if (isNotification) {
queue
.filter { it.status == Download.QUEUE && it.manga.id == manga.id }
queue.filter { it.status == Download.QUEUE && it.manga.id == manga.id }
.forEach { it.status = Download.NOT_DOWNLOADED }
}
queue.remove(manga)
@ -204,10 +197,9 @@ class Downloader(
subscriptions.clear()
subscriptions += downloadsRelay.concatMapIterable { it }
.concatMap { downloadChapter(it).subscribeOn(Schedulers.io()) }
.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ completeDownload(it)
.concatMap { downloadChapter(it).subscribeOn(Schedulers.io()) }.onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread()).subscribe({
completeDownload(it)
}, { error ->
DownloadService.stop(context)
Timber.e(error)
@ -233,8 +225,7 @@ class Downloader(
* @param chapters the list of chapters to download.
* @param autoStart whether to start the downloader after enqueing the chapters.
*/
fun queueChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean) =
launchUI {
fun queueChapters(manga: Manga, chapters: List<Chapter>, autoStart: Boolean) = launchUI {
val source = sourceManager.get(manga.source) as? HttpSource ?: return@launchUI
val wasEmpty = queue.isEmpty()
// Called in background thread, the operation can be slow with SAF.
@ -282,8 +273,7 @@ class Downloader(
val pageListObservable = if (download.pages == null) {
// Pull page list from network and add them to download object
download.source.fetchPageList(download.chapter)
.doOnNext { pages ->
download.source.fetchPageList(download.chapter).doOnNext { pages ->
if (pages.isEmpty()) {
throw Exception("Page list is empty")
}
@ -294,12 +284,9 @@ class Downloader(
Observable.just(download.pages!!)
}
pageListObservable
.doOnNext { _ ->
pageListObservable.doOnNext { _ ->
// Delete all temporary (unfinished) files
tmpDir.listFiles()
?.filter { it.name!!.endsWith(".tmp") }
?.forEach { it.delete() }
tmpDir.listFiles()?.filter { it.name!!.endsWith(".tmp") }?.forEach { it.delete() }
download.downloadedImages = 0
download.status = Download.DOWNLOADING
@ -309,9 +296,7 @@ class Downloader(
// Start downloading images, consider we can have downloaded images already
.concatMap { page -> getOrDownloadImage(page, download, tmpDir) }
// Do when page is downloaded.
.doOnNext { notifier.onProgressChange(download) }
.toList()
.map { _ -> download }
.doOnNext { notifier.onProgressChange(download) }.toList().map { _ -> download }
// Do after download completes
.doOnNext { ensureSuccessfulDownload(download, mangaDir, tmpDir, chapterDirname) }
// If the page list threw, it will resume here
@ -330,10 +315,13 @@ class Downloader(
* @param download the download of the page.
* @param tmpDir the temporary directory of the download.
*/
private fun getOrDownloadImage(page: Page, download: Download, tmpDir: UniFile): Observable<Page> {
private fun getOrDownloadImage(
page: Page,
download: Download,
tmpDir: UniFile
): Observable<Page> {
// If the image URL is empty, do nothing
if (page.imageUrl == null)
return Observable.just(page)
if (page.imageUrl == null) return Observable.just(page)
val filename = String.format("%03d", page.number)
val tmpFile = tmpDir.findFile("$filename.tmp")
@ -347,8 +335,12 @@ class Downloader(
// If the image is already downloaded, do nothing. Otherwise download from network
val pageObservable = when {
imageFile != null -> Observable.just(imageFile)
cache.isImageInCache(page.imageUrl!!) ->
moveFromCache(page, cache.getImageFile(page.imageUrl!!), tmpDir, filename)
cache.isImageInCache(page.imageUrl!!) -> moveFromCache(
page,
cache.getImageFile(page.imageUrl!!),
tmpDir,
filename
)
else -> downloadImage(page, download.source, tmpDir, filename)
}
@ -359,8 +351,7 @@ class Downloader(
page.progress = 100
download.downloadedImages++
page.status = Page.READY
}
.map { page }
}.map { page }
// Mark this page as error and allow to download the remaining
.onErrorReturn {
page.progress = 0
@ -377,8 +368,12 @@ class Downloader(
* @param tmpDir the temporary directory of the download.
* @param filename the filename of the image.
*/
private fun moveFromCache(page: Page, file: File, tmpDir: UniFile, filename: String):
Observable<UniFile> {
private fun moveFromCache(
page: Page,
file: File,
tmpDir: UniFile,
filename: String
): Observable<UniFile> {
return Observable.just(file).map {
val tmpFile = tmpDir.createFile("$filename.tmp")
val inputStream = file.inputStream()
@ -402,11 +397,15 @@ class Downloader(
* @param tmpDir the temporary directory of the download.
* @param filename the filename of the image.
*/
private fun downloadImage(page: Page, source: HttpSource, tmpDir: UniFile, filename: String): Observable<UniFile> {
private fun downloadImage(
page: Page,
source: HttpSource,
tmpDir: UniFile,
filename: String
): Observable<UniFile> {
page.status = Page.DOWNLOAD_IMAGE
page.progress = 0
return source.fetchImage(page)
.map { response ->
return source.fetchImage(page).map { response ->
val file = tmpDir.createFile("$filename.tmp")
try {
response.body!!.source().saveTo(file.openOutputStream())
@ -450,10 +449,7 @@ class Downloader(
* @param dirname the real (non temporary) directory name of the download.
*/
private fun ensureSuccessfulDownload(
download: Download,
mangaDir: UniFile,
tmpDir: UniFile,
dirname: String
download: Download, mangaDir: UniFile, tmpDir: UniFile, dirname: String
) {
// Ensure that the chapter folder has all the images.

Loading…
Cancel
Save