Partly revert 0d666a6 to reduce network requests

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-22 06:27:09 +08:00
1 parent 0d666a633f
commit 1a6d839ca0
2 files changed
+21 -12

No files matched your search

@@ -369,12 +369,6 @@ object DownloadManager : OnSpiderListener {
}
suspend fun restoreDownload(galleryInfo: BaseGalleryInfo, dirname: String) {
if (containDownloadInfo(galleryInfo.gid)) {
// Contain
return
}
// It is new download info
val info = DownloadInfo(galleryInfo, dirname)
info.state = DownloadInfo.STATE_NONE
info.position = allInfoList.size
@@ -158,12 +158,23 @@ fun DownloadScreen() {
title = stringResource(id = R.string.settings_download_restore_download_items),
summary = stringResource(id = R.string.settings_download_restore_download_items_summary),
) {
fun getRestoreItem(file: UniFile): RestoreItem? {
var restoreDirCount = 0
suspend fun getRestoreItem(file: UniFile): RestoreItem? {
if (!file.isDirectory) return null
val siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME) ?: return null
return runCatching {
val spiderInfo = readCompatFromUniFile(siFile) ?: return null
val gid = spiderInfo.gid
val dirname = file.name ?: return null
if (DownloadManager.containDownloadInfo(gid)) {
// Restore download dir to avoid redownload
val dbdirname = EhDB.getDownloadDirname(gid)
if (null == dbdirname || dirname != dbdirname) {
EhDB.putDownloadDirname(gid, dirname)
restoreDirCount++
}
return null
}
RestoreItem(dirname).also {
it.gid = spiderInfo.gid
it.token = spiderInfo.token
@@ -183,11 +194,15 @@ fun DownloadScreen() {
if (result == null) {
launchSnackBar(restoreFailed)
} else {
val count = result.filterNot { it.title.isNullOrBlank() }.map {
EhDB.putDownloadDirname(it.gid, it.dirname)
DownloadManager.restoreDownload(it, it.dirname)
}.size
launchSnackBar(RESTORE_COUNT_MSG(count))
if (result.isEmpty()) {
launchSnackBar(RESTORE_COUNT_MSG(restoreDirCount))
} else {
val count = result.filterNot { it.title.isNullOrBlank() }.map {
EhDB.putDownloadDirname(it.gid, it.dirname)
DownloadManager.restoreDownload(it, it.dirname)
}.size
launchSnackBar(RESTORE_COUNT_MSG(count + restoreDirCount))
}
}
}.onFailure {
it.printStackTrace()