Suspend DownloadDirnameDao update insert

This commit is contained in:
tarsin committed 2023-07-12 09:46:09 +08:00
1 parent 62c3e9290e
commit 02489893ec
5 files changed
+48 -44

No files matched your search

+1 -2
View File
@@ -76,8 +76,7 @@ object EhDB {
return raw?.dirname
}
@Synchronized
fun putDownloadDirname(gid: Long, dirname: String) {
suspend fun putDownloadDirname(gid: Long, dirname: String) {
val dao = db.downloadDirnameDao()
var raw = dao.load(gid)
if (raw != null) {
@@ -11,10 +11,10 @@ interface DownloadDirnameDao {
fun load(gid: Long): DownloadDirname?
@Update
fun update(downloadDirname: DownloadDirname)
suspend fun update(downloadDirname: DownloadDirname)
@Insert
fun insert(t: DownloadDirname): Long
suspend fun insert(t: DownloadDirname): Long
@Query("DELETE FROM DOWNLOAD_DIRNAME WHERE GID = :gid")
suspend fun deleteByKey(gid: Long)
@@ -51,13 +51,15 @@ class SpiderDen(private val mGalleryInfo: GalleryInfo) {
@Volatile
@SpiderQueen.Mode
var mode = SpiderQueen.MODE_READ
set(value) {
field = value
if (field == SpiderQueen.MODE_DOWNLOAD && downloadDir == null) {
mGalleryInfo.putToDownloadDir()
downloadDir = getGalleryDownloadDir(mGid)!!.apply { check(ensureDir()) { "Download directory $uri is not valid directory!" } }
}
private set
suspend fun setMode(value: Int) {
mode = value
if (mode == SpiderQueen.MODE_DOWNLOAD && downloadDir == null) {
mGalleryInfo.putToDownloadDir()
downloadDir = getGalleryDownloadDir(mGid)!!.apply { check(ensureDir()) { "Download directory $uri is not valid directory!" } }
}
}
private fun containInCache(index: Int): Boolean {
val key = getImageKey(mGid, index)
@@ -288,7 +290,7 @@ private fun findImageFile(dir: UniFile, index: Int): UniFile? {
return COMPAT_IMAGE_EXTENSIONS.firstNotNullOfOrNull { dir.findFile(perFilename(index, it)) }
}
fun GalleryInfo.putToDownloadDir() {
suspend fun GalleryInfo.putToDownloadDir() {
val title = getSuitableTitle(this)
val dirname = FileUtils.sanitizeFilename("$gid-$title")
EhDB.putDownloadDirname(gid, dirname)
@@ -167,39 +167,42 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
val isReady
get() = this::mSpiderInfo.isInitialized && this::mPageStateArray.isInitialized
@Synchronized
private fun updateMode() {
if (!isReady) return
val mode: Int = if (mDownloadReference > 0) {
MODE_DOWNLOAD
} else {
MODE_READ
}
mSpiderDen.mode = mode
private val updateLock = Mutex()
// Update download page
val intoDownloadMode = mode == MODE_DOWNLOAD
if (intoDownloadMode && !downloadMode) {
// Clear download state
synchronized(mPageStateLock) {
val temp: IntArray = mPageStateArray
var i = 0
val n = temp.size
while (i < n) {
val oldState = temp[i]
if (STATE_DOWNLOADING != oldState) {
temp[i] = STATE_NONE
}
i++
}
mDownloadedPages.lazySet(0)
mFinishedPages.lazySet(0)
mPageErrorMap.clear()
mPagePercentMap.clear()
private suspend fun updateMode() {
if (!awaitReady()) return
updateLock.withLock {
val mode: Int = if (mDownloadReference > 0) {
MODE_DOWNLOAD
} else {
MODE_READ
}
mWorkerScope.enterDownloadMode()
mSpiderDen.setMode(mode)
// Update download page
val intoDownloadMode = mode == MODE_DOWNLOAD
if (intoDownloadMode && !downloadMode) {
// Clear download state
synchronized(mPageStateLock) {
val temp: IntArray = mPageStateArray
var i = 0
val n = temp.size
while (i < n) {
val oldState = temp[i]
if (STATE_DOWNLOADING != oldState) {
temp[i] = STATE_NONE
}
i++
}
mDownloadedPages.lazySet(0)
mFinishedPages.lazySet(0)
mPageErrorMap.clear()
mPagePercentMap.clear()
}
mWorkerScope.enterDownloadMode()
}
downloadMode = intoDownloadMode
}
downloadMode = intoDownloadMode
}
private fun setMode(@Mode mode: Int) {
@@ -505,7 +508,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
val gid = galleryInfo.gid
return (sQueenMap[gid] ?: SpiderQueen(galleryInfo).also { sQueenMap[gid] = it }).apply {
setMode(mode)
launchIO { if (awaitReady()) updateMode() }
launchIO { updateMode() }
}
}
@@ -516,7 +519,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
stop()
sQueenMap.remove(galleryInfo.gid)
} else {
launchIO { if (awaitReady()) updateMode() }
launchIO { updateMode() }
}
}
}
@@ -155,7 +155,7 @@ fun DownloadScreen() {
summary = stringResource(id = R.string.settings_download_restore_download_items_summary),
) {
var restoreDirCount = 0
fun getRestoreItem(file: UniFile): RestoreItem? {
suspend fun getRestoreItem(file: UniFile): RestoreItem? {
if (!file.isDirectory) return null
val siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: return null
return runCatching {