Refine saveFromHttpResponse
This commit is contained in:
1 parent
29fb40eecc
commit
3a2c18d8a9
2 files changed
+33
-19
No files matched your search
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import android.util.Log
|
||||
import com.hippo.ehviewer.EhApplication.Companion.nonCacheOkHttpClient
|
||||
import com.hippo.ehviewer.EhApplication.Companion.okHttpClient
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
@@ -57,17 +58,39 @@ fun jsonArrayOf(vararg element: Any?) = JSONArray().apply { element.forEach { pu
|
||||
inline fun Request.Builder.jsonBody(builder: JSONObject.() -> Unit) = post(JSONObject().apply(builder).toString().toRequestBody(MEDIA_TYPE_JSON))
|
||||
|
||||
suspend inline fun <R> Call.usingCancellable(crossinline block: Response.() -> R): R = executeAsync().use {
|
||||
val call = this
|
||||
Log.d("usingCancellable", "Dispatching call$call")
|
||||
coroutineScope {
|
||||
suspendCancellableCoroutine { cont ->
|
||||
suspendCancellableCoroutine<R> { cont ->
|
||||
launch {
|
||||
val r = runCatching { block(it) }
|
||||
val r = runCatching {
|
||||
Log.d("usingCancellable", "Reading call$call")
|
||||
block(it)
|
||||
}
|
||||
if (!isCanceled() && cont.isActive) {
|
||||
r.exceptionOrNull()?.let { cont.resumeWithException(it) }
|
||||
r.getOrNull()?.let { cont.resume(it) }
|
||||
r.exceptionOrNull()?.let {
|
||||
Log.e("usingCancellable", "Processing call$call failed!")
|
||||
cont.resumeWithException(it)
|
||||
}
|
||||
r.getOrNull()?.let {
|
||||
Log.d("usingCancellable", "Processing call$call succeed!")
|
||||
cont.resume(it)
|
||||
}
|
||||
} else if (isCanceled() && cont.isActive) {
|
||||
Log.e("usingCancellable", "call$call cancelled but coroutine is Active!")
|
||||
} else if (!isCanceled() && !cont.isActive) {
|
||||
Log.e("usingCancellable", "call$call not cancelled but coroutine is Dead!")
|
||||
} else {
|
||||
Log.d("usingCancellable", "Cancelled reading call$call")
|
||||
}
|
||||
}
|
||||
cont.invokeOnCancellation { cancel() }
|
||||
cont.invokeOnCancellation {
|
||||
Log.d("usingCancellable", "Cancelling call$call")
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}.apply {
|
||||
Log.d("usingCancellable", "Processing call$call finished!")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,6 @@ import com.hippo.ehviewer.util.sendTo
|
||||
import com.hippo.ehviewer.yorozuya.FileUtils
|
||||
import com.hippo.unifile.UniFile
|
||||
import com.hippo.unifile.openOutputStream
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import okhttp3.Response
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
@@ -166,27 +165,19 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
|
||||
return ret
|
||||
}
|
||||
|
||||
findDownloadFileForIndex(index, extension)?.runSuspendCatching {
|
||||
findDownloadFileForIndex(index, extension)?.run {
|
||||
return doSave(this) == length
|
||||
}?.onFailure {
|
||||
it.printStackTrace()
|
||||
return false
|
||||
}
|
||||
|
||||
// Read Mode, allow save to cache
|
||||
if (mode == SpiderQueen.MODE_READ) {
|
||||
val key = getImageKey(mGid, index)
|
||||
var received: Long = 0
|
||||
runSuspendCatching {
|
||||
sCache.edit(key) {
|
||||
metadata.toFile().writeText(extension)
|
||||
received = doSave(UniFile.fromFile(data.toFile())!!)
|
||||
}
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}.onSuccess {
|
||||
return received == length
|
||||
sCache.edit(key) {
|
||||
metadata.toFile().writeText(extension)
|
||||
received = doSave(UniFile.fromFile(data.toFile())!!)
|
||||
}
|
||||
return received == length
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
Reference in new issue
Block a user