|
|
|
@ -119,7 +119,8 @@ class DownloadManager(
|
|
|
|
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
|
|
|
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
|
|
|
val pages = repo.getPages(chapter)
|
|
|
|
val pages = repo.getPages(chapter)
|
|
|
|
for ((pageIndex, page) in pages.withIndex()) {
|
|
|
|
for ((pageIndex, page) in pages.withIndex()) {
|
|
|
|
failsafe@ do {
|
|
|
|
var retryCounter = 0
|
|
|
|
|
|
|
|
failsafe@ while (true) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
val url = repo.getPageUrl(page)
|
|
|
|
val url = repo.getPageUrl(page)
|
|
|
|
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
|
|
|
val file = cache[url] ?: downloadFile(url, page.referer, destination, tempFileName)
|
|
|
|
@ -129,12 +130,18 @@ class DownloadManager(
|
|
|
|
pageNumber = pageIndex,
|
|
|
|
pageNumber = pageIndex,
|
|
|
|
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
|
|
|
ext = MimeTypeMap.getFileExtensionFromUrl(url),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
break@failsafe
|
|
|
|
} catch (e: IOException) {
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
|
|
|
if (retryCounter < MAX_DOWNLOAD_ATTEMPTS) {
|
|
|
|
outState.value = DownloadState.WaitingForNetwork(startId, data, cover)
|
|
|
|
outState.value = DownloadState.WaitingForNetwork(startId, data, cover)
|
|
|
|
|
|
|
|
delay(DOWNLOAD_ERROR_DELAY)
|
|
|
|
connectivityManager.waitForNetwork()
|
|
|
|
connectivityManager.waitForNetwork()
|
|
|
|
continue@failsafe
|
|
|
|
retryCounter++
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
outState.value = DownloadState.Progress(
|
|
|
|
outState.value = DownloadState.Progress(
|
|
|
|
startId, data, cover,
|
|
|
|
startId, data, cover,
|
|
|
|
@ -180,10 +187,7 @@ class DownloadManager(
|
|
|
|
.get()
|
|
|
|
.get()
|
|
|
|
.build()
|
|
|
|
.build()
|
|
|
|
val call = okHttp.newCall(request)
|
|
|
|
val call = okHttp.newCall(request)
|
|
|
|
var attempts = MAX_DOWNLOAD_ATTEMPTS
|
|
|
|
|
|
|
|
val file = File(destination, tempFileName)
|
|
|
|
val file = File(destination, tempFileName)
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
val response = call.clone().await()
|
|
|
|
val response = call.clone().await()
|
|
|
|
runInterruptible(Dispatchers.IO) {
|
|
|
|
runInterruptible(Dispatchers.IO) {
|
|
|
|
file.outputStream().use { out ->
|
|
|
|
file.outputStream().use { out ->
|
|
|
|
@ -191,15 +195,6 @@ class DownloadManager(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
return file
|
|
|
|
} catch (e: IOException) {
|
|
|
|
|
|
|
|
attempts--
|
|
|
|
|
|
|
|
if (attempts <= 0) {
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
delay(DOWNLOAD_ERROR_DELAY)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun errorStateHandler(outState: MutableStateFlow<DownloadState>) =
|
|
|
|
private fun errorStateHandler(outState: MutableStateFlow<DownloadState>) =
|
|
|
|
|