Kill GalleryDetailScene downloadArchive callback

This commit is contained in:
tarsin committed 2023-04-22 12:31:49 +08:00
1 parent 7d74bdc3bd
commit 32038531e1
3 files changed
+29 -55

No files matched your search

@@ -125,14 +125,6 @@ object EhClient {
params[2] as String?,
)
METHOD_DOWNLOAD_ARCHIVE -> EhEngine.downloadArchive(
params[0] as Long,
params[1] as String?,
params[2] as String?,
params[3] as String?,
params[4] as Boolean,
)
else -> throw IllegalStateException("Can't detect method $method")
}
}
@@ -156,5 +148,4 @@ object EhClient {
const val METHOD_VOTE_COMMENT = 15
const val METHOD_IMAGE_SEARCH = 16
const val METHOD_ARCHIVE_LIST = 17
const val METHOD_DOWNLOAD_ARCHIVE = 18
}
@@ -170,6 +170,7 @@ import com.hippo.util.ExceptionUtils
import com.hippo.util.ReadableTime
import com.hippo.util.addTextToClipboard
import com.hippo.util.getParcelableCompat
import com.hippo.util.getSystemService
import com.hippo.widget.ObservedTextView
import com.hippo.widget.recyclerview.getSpanCountForSuitableSize
import com.hippo.yorozuya.FileUtils
@@ -1543,41 +1544,6 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
}
}
private class DownloadArchiveListener(
context: Context,
private val mGalleryInfo: GalleryInfo?,
) : EhCallback<GalleryDetailScene?, String?>(context) {
override fun onSuccess(result: String?) {
// TODO: Don't use buggy system download service
val r = DownloadManager.Request(Uri.parse(result))
val name =
mGalleryInfo!!.gid.toString() + "-" + EhUtils.getSuitableTitle(mGalleryInfo) + ".zip"
r.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
FileUtils.sanitizeFilename(name),
)
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val dm = application.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
try {
dm.enqueue(r)
} catch (e: Throwable) {
e.printStackTrace()
ExceptionUtils.throwIfFatal(e)
}
showTip(R.string.download_archive_started, LENGTH_SHORT)
}
override fun onFailure(e: Exception) {
if (e is NoHAtHClientException) {
showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG)
} else {
showTip(R.string.download_archive_failure, LENGTH_LONG)
}
}
override fun onCancel() {}
}
private inner class DeleteDialogHelper(
private val mDownloadManager: com.hippo.ehviewer.download.DownloadManager?,
private val mGalleryInfo: GalleryInfo,
@@ -1713,17 +1679,30 @@ class GalleryDetailScene : BaseScene(), DownloadInfoListener {
}
val res = mArchiveList!![position].res
val isHAtH = mArchiveList!![position].isHAtH
val request = EhRequest()
request.setMethod(EhClient.METHOD_DOWNLOAD_ARCHIVE)
request.setArgs(
composeBindingGD!!.gid,
composeBindingGD!!.token!!,
mArchiveFormParamOr!!,
res,
isHAtH,
)
request.setCallback(DownloadArchiveListener(context, composeBindingGD))
request.enqueue(this@GalleryDetailScene)
composeBindingGD?.run {
lifecycleScope.launchIO {
runSuspendCatching {
EhEngine.downloadArchive(gid, token, mArchiveFormParamOr, res, isHAtH)
}.onSuccess { result ->
val r = DownloadManager.Request(Uri.parse(result))
val name = gid.toString() + "-" + EhUtils.getSuitableTitle(this@run) + ".zip"
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, FileUtils.sanitizeFilename(name))
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
runCatching {
requireContext().getSystemService<DownloadManager>()!!.enqueue(r)
}.onFailure {
it.printStackTrace()
}
showTip(R.string.download_archive_started, LENGTH_SHORT)
}.onFailure {
if (it is NoHAtHClientException) {
showTip(R.string.download_archive_failure_no_hath, LENGTH_LONG)
} else {
showTip(R.string.download_archive_failure, LENGTH_LONG)
}
}
}
}
}
if (mDialog != null) {
mDialog!!.dismiss()
@@ -16,8 +16,10 @@
package com.hippo.util
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.hippo.ehviewer.R
object AppHelper {
@@ -37,3 +39,5 @@ object AppHelper {
}
}
}
inline fun <reified T> Context.getSystemService(): T? = ContextCompat.getSystemService(this, T::class.java)