Suspend DownloadDirnameDao load

This commit is contained in:
tarsin committed 2023-07-12 09:58:31 +08:00
1 parent 02489893ec
commit 4441ea2c5d
7 files changed
+18 -20

No files matched your search

+1 -2
View File
@@ -69,8 +69,7 @@ object EhDB {
db.downloadsDao().delete(downloadInfo)
}
@Synchronized
fun getDownloadDirname(gid: Long): String? {
suspend fun getDownloadDirname(gid: Long): String? {
val dao = db.downloadDirnameDao()
val raw = dao.load(gid)
return raw?.dirname
@@ -8,7 +8,7 @@ import androidx.room.Update
@Dao
interface DownloadDirnameDao {
@Query("SELECT * FROM DOWNLOAD_DIRNAME WHERE GID = :gid")
fun load(gid: Long): DownloadDirname?
suspend fun load(gid: Long): DownloadDirname?
@Update
suspend fun update(downloadDirname: DownloadDirname)
@@ -25,9 +25,9 @@ import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.dao.DownloadLabel
import com.hippo.ehviewer.image.Image
import com.hippo.ehviewer.spider.SpiderDen
import com.hippo.ehviewer.spider.SpiderQueen
import com.hippo.ehviewer.spider.SpiderQueen.OnSpiderListener
import com.hippo.ehviewer.spider.getGalleryDownloadDir
import com.hippo.ehviewer.spider.readCompatFromUniFile
import com.hippo.ehviewer.spider.write
import com.hippo.ehviewer.util.AppConfig
@@ -589,8 +589,8 @@ object DownloadManager : OnSpiderListener {
}
}
private fun resetReadingProgress(gid: Long) {
val downloadDir = SpiderDen.getGalleryDownloadDir(gid) ?: return
private suspend fun resetReadingProgress(gid: Long) {
val downloadDir = getGalleryDownloadDir(gid) ?: return
val file = downloadDir.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: return
val spiderInfo = readCompatFromUniFile(file) ?: return
spiderInfo.startPage = 0
@@ -46,7 +46,7 @@ import com.hippo.ehviewer.EhApplication.Companion.imageCache as sCache
class SpiderDen(private val mGalleryInfo: GalleryInfo) {
private val mGid = mGalleryInfo.gid
var downloadDir: UniFile? = getGalleryDownloadDir(mGid)?.takeIf { it.isDirectory }
var downloadDir: UniFile? = null
@Volatile
@SpiderQueen.Mode
@@ -261,13 +261,6 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
override fun close() {}
}
}
companion object {
fun getGalleryDownloadDir(gid: Long): UniFile? {
val dirname = EhDB.getDownloadDirname(gid) ?: return null
return downloadLocation.subFile(dirname)
}
}
}
private val COMPAT_IMAGE_EXTENSIONS = SUPPORT_IMAGE_EXTENSIONS + ".jpeg"
@@ -295,3 +288,8 @@ suspend fun GalleryInfo.putToDownloadDir() {
val dirname = FileUtils.sanitizeFilename("$gid-$title")
EhDB.putDownloadDirname(gid, dirname)
}
suspend fun getGalleryDownloadDir(gid: Long): UniFile? {
val dirname = EhDB.getDownloadDirname(gid) ?: return null
return downloadLocation.subFile(dirname)
}
@@ -224,6 +224,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
private val prepareJob = launchIO { doPrepare() }
private suspend fun doPrepare() {
mSpiderDen.downloadDir = getGalleryDownloadDir(galleryInfo.gid)?.takeIf { it.isDirectory }
mSpiderInfo = readSpiderInfoFromLocal() ?: readSpiderInfoFromInternet() ?: return
mPageStateArray = IntArray(mSpiderInfo.pages)
notifyGetPages(mSpiderInfo.pages)
@@ -74,7 +74,7 @@ import com.hippo.ehviewer.download.DownloadManager.DownloadInfoListener
import com.hippo.ehviewer.download.DownloadService
import com.hippo.ehviewer.download.DownloadService.Companion.clear
import com.hippo.ehviewer.ktbuilder.imageRequest
import com.hippo.ehviewer.spider.SpiderDen
import com.hippo.ehviewer.spider.getGalleryDownloadDir
import com.hippo.ehviewer.spider.putToDownloadDir
import com.hippo.ehviewer.ui.legacy.AutoStaggeredGridLayoutManager
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
@@ -778,7 +778,7 @@ class DownloadsScene :
val files = arrayOfNulls<UniFile>(mDownloadInfoList.size)
for ((i, info) in mDownloadInfoList.withIndex()) {
// Put file
files[i] = SpiderDen.getGalleryDownloadDir(info.gid)
files[i] = getGalleryDownloadDir(info.gid)
// Remove download path
EhDB.removeDownloadDirname(info.gid)
}
@@ -864,9 +864,9 @@ class DownloadsScene :
Spacer(modifier = Modifier.height(height).fillMaxWidth())
}
lifecycleScope.launchIO {
val downloadDir = SpiderDen.getGalleryDownloadDir(info.gid) ?: run {
val downloadDir = getGalleryDownloadDir(info.gid) ?: run {
info.putToDownloadDir()
SpiderDen.getGalleryDownloadDir(info.gid)!!
getGalleryDownloadDir(info.gid)!!
}
downloadDir.ensureDir()
val thumbLocation = downloadDir.subFile(".thumb")!!
@@ -130,9 +130,9 @@ import com.hippo.ehviewer.databinding.DialogRateBinding
import com.hippo.ehviewer.databinding.DialogTorrentListBinding
import com.hippo.ehviewer.databinding.ItemGalleryCommentBinding
import com.hippo.ehviewer.ktbuilder.imageRequest
import com.hippo.ehviewer.spider.SpiderDen
import com.hippo.ehviewer.spider.SpiderQueen
import com.hippo.ehviewer.spider.SpiderQueen.Companion.MODE_READ
import com.hippo.ehviewer.spider.getGalleryDownloadDir
import com.hippo.ehviewer.ui.CommonOperations
import com.hippo.ehviewer.ui.GalleryInfoBottomSheet
import com.hippo.ehviewer.ui.MainActivity
@@ -1273,7 +1273,7 @@ class GalleryDetailScene : BaseScene() {
if (checked) {
lifecycleScope.launchIO {
runCatching {
val file = SpiderDen.getGalleryDownloadDir(mGalleryInfo.gid)
val file = getGalleryDownloadDir(mGalleryInfo.gid)
EhDB.removeDownloadDirname(mGalleryInfo.gid)
file?.delete()
}