Prepare to save downloaded gallery as cbz archive

This commit is contained in:
FooIbar committed 2024-01-30 19:15:40 +08:00
1 parent 8502671203
commit b88c96894d
2 files changed
+14 -13

No files matched your search

@@ -15,8 +15,6 @@
*/
package com.hippo.ehviewer.gallery
import android.content.Context
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.util.Log
import com.hippo.ehviewer.Settings.archivePasswds
@@ -49,17 +47,17 @@ import moe.tarsin.coroutines.withLock
typealias PasswdInvalidator = (String) -> Boolean
typealias PasswdProvider = suspend (PasswdInvalidator) -> String
class ArchivePageLoader(context: Context, private val uri: Uri, passwdProvider: PasswdProvider) : PageLoader2(), CoroutineScope {
class ArchivePageLoader(private val file: UniFile, passwdProvider: PasswdProvider? = null) : PageLoader2(), CoroutineScope {
override val coroutineContext = Dispatchers.IO + Job()
private lateinit var pfd: ParcelFileDescriptor
private val hostJob = launch(start = CoroutineStart.LAZY) {
Log.d(DEBUG_TAG, "Open archive $uri")
pfd = context.contentResolver.openFileDescriptor(uri, "r")!!
Log.d(DEBUG_TAG, "Open archive ${file.uri}")
pfd = file.openFileDescriptor("r")
size = openArchive(pfd.fd, pfd.statSize)
if (size == 0) {
return@launch
}
if (needPassword()) {
if (passwdProvider != null && needPassword()) {
archivePasswds?.forEach {
it ?: return@forEach
if (providePassword(it)) return@launch
@@ -80,7 +78,7 @@ class ArchivePageLoader(context: Context, private val uri: Uri, passwdProvider:
cancel()
closeArchive()
pfd.close()
Log.d(DEBUG_TAG, "Close archive $uri successfully!")
Log.d(DEBUG_TAG, "Close archive ${file.uri} successfully!")
super.stop()
}
@@ -72,6 +72,8 @@ import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.collectAsState
import com.hippo.ehviewer.databinding.ReaderActivityBinding
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.download.downloadDir
import com.hippo.ehviewer.gallery.ArchivePageLoader
import com.hippo.ehviewer.gallery.EhPageLoader
import com.hippo.ehviewer.gallery.PageLoader2
@@ -118,7 +120,6 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
import splitties.init.appCtx
import splitties.systemservices.clipboardManager
class GalleryModel : ViewModel() {
@@ -166,7 +167,12 @@ class ReaderActivity : EhActivity() {
}
if (ACTION_EH == mAction) {
mGalleryInfo?.let { mGalleryProvider = EhPageLoader(it) }
mGalleryInfo?.let {
mGalleryProvider = DownloadManager.getDownloadInfo(it.gid)?.downloadDir
?.findFile("${it.gid}.cbz")
?.let { file -> ArchivePageLoader(file) }
?: EhPageLoader(it)
}
} else if (Intent.ACTION_VIEW == mAction) {
if (mUri != null) {
try {
@@ -179,10 +185,7 @@ class ReaderActivity : EhActivity() {
Toast.makeText(this, R.string.error_reading_failed, Toast.LENGTH_SHORT).show()
}
mGalleryProvider = ArchivePageLoader(
appCtx,
mUri!!,
) { invalidator ->
mGalleryProvider = ArchivePageLoader(mUri!!.asUniFile()) { invalidator ->
runCatching {
dialogState.awaitInputText(
title = getString(R.string.archive_need_passwd),