Removed network call now copies from page image

This commit is contained in:
Bram van de Kerkhof 2016-10-01 16:32:52 +02:00
parent 2991906a85
commit 2a4527a8d6

View File

@ -27,6 +27,7 @@ import eu.kanade.tachiyomi.util.RetryWithDelay
import eu.kanade.tachiyomi.util.SharedData import eu.kanade.tachiyomi.util.SharedData
import eu.kanade.tachiyomi.util.saveTo import eu.kanade.tachiyomi.util.saveTo
import eu.kanade.tachiyomi.util.toast import eu.kanade.tachiyomi.util.toast
import okio.BufferedSource
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -34,6 +35,7 @@ import rx.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.File import java.io.File
import java.io.FileInputStream
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.* import java.util.*
@ -587,6 +589,8 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
@Throws(IOException::class) @Throws(IOException::class)
internal fun savePage() { internal fun savePage() {
chapter.pages?.get(chapter.last_page_read)?.let { page -> chapter.pages?.get(chapter.last_page_read)?.let { page ->
val inputFile = File(page.imagePath)
// File where the image will be saved // File where the image will be saved
val destFile = File(pictureDirectory, manga.title + " - " + chapter.name + val destFile = File(pictureDirectory, manga.title + " - " + chapter.name +
" - " + downloadManager.getImageFilename(page)) " - " + downloadManager.getImageFilename(page))
@ -594,38 +598,16 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
if (destFile.exists()) { if (destFile.exists()) {
imageNotifier.onComplete(destFile) imageNotifier.onComplete(destFile)
} else { } else {
// Progress of the download if (inputFile.exists()) {
var savedProgress = 0 Observable.fromCallable { inputFile.copyTo(destFile) }
.observeOn(AndroidSchedulers.mainThread())
val progressListener = object : ProgressListener { .subscribeOn(Schedulers.io())
override fun update(bytesRead: Long, contentLength: Long, done: Boolean) { .subscribe(
val progress = (100 * bytesRead / contentLength).toInt() { imageNotifier.onComplete(it) }, { error ->
if (progress > savedProgress) { Timber.e(error.message)
savedProgress = progress imageNotifier.onError(error.message)
imageNotifier.onProgressChange(progress) })
}
}
} }
// Download and save the image.
Observable.fromCallable { ->
network.client.newCallWithProgress(GET(page.imageUrl!!), progressListener).execute()
}.map {
response ->
if (response.isSuccessful) {
response.body().source().saveTo(destFile)
imageNotifier.onComplete(destFile)
} else {
response.close()
throw Exception("Unsuccessful response")
}
}
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe({}, { error ->
Timber.e(error.message)
imageNotifier.onError(error.message)
})
} }
} }
} }