Reload metadata (#1058)

* Reload metadata

* Remove pages check and Refactor

* Check metadata stable

* Run in parallel

* Refactor postedTime

* Remove concurrency limit

* Update translation

* Update translate

Co-authored-by: FooIbar <118464521+FooIbar@users.noreply.github.com>

* `parseDate` return timestamps

* Remove unused import

* Show Snackbar after the update

* Revert EndLine

* use localContext

* Extend the range of `runSuspendCatching`

---------

Co-authored-by: FooIbar <118464521+FooIbar@users.noreply.github.com>
This commit is contained in:
dasoopsandFooIbar authored and GitHub committed 2024-04-26 21:05:52 +08:00
1 parent 481bb8e7d4
commit ea3aed8fae
4 files changed
+45

No files matched your search

@@ -15,6 +15,7 @@
*/
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.util.toEpochMillis
import com.hippo.ehviewer.util.toIntOrDefault
import com.hippo.ehviewer.util.toLocalDateTime
import com.hippo.ehviewer.util.toLongOrDefault
@@ -41,6 +42,10 @@ object ParserUtils {
return formatter.format(time.toLocalDateTime())
}
fun parseDate(str: String): Long {
return formatter.parse(str).toEpochMillis()
}
fun trim(str: String?): String {
return str?.unescapeXml()?.trim() ?: ""
}
@@ -28,6 +28,7 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import arrow.fx.coroutines.parMap
import arrow.fx.coroutines.parMapNotNull
import com.hippo.ehviewer.EhDB
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
@@ -36,7 +37,10 @@ import com.hippo.ehviewer.client.EhUrl
import com.hippo.ehviewer.client.data.BaseGalleryInfo
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.parser.GalleryDetailUrlParser
import com.hippo.ehviewer.client.parser.ParserUtils
import com.hippo.ehviewer.dao.DownloadInfo
import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.download.downloadDir
import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.spider.COMIC_INFO_FILE
import com.hippo.ehviewer.spider.SpiderDen
@@ -46,6 +50,7 @@ import com.hippo.ehviewer.spider.readCompatFromUniFile
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
import com.hippo.ehviewer.ui.tools.observed
import com.hippo.ehviewer.ui.tools.rememberedAccessor
import com.hippo.ehviewer.util.displayString
import com.hippo.unifile.UniFile
import com.hippo.unifile.asUniFile
import com.hippo.unifile.displayPath
@@ -157,6 +162,33 @@ fun DownloadScreen(navigator: DestinationsNavigator) {
summary = stringResource(id = R.string.settings_download_archive_metadata_summary),
value = Settings::archiveMetadata,
)
WorkPreference(
title = stringResource(id = R.string.settings_download_reload_metadata),
summary = stringResource(id = R.string.settings_download_reload_metadata_summary),
) {
fun DownloadInfo.isStable(): Boolean {
val downloadTime = downloadDir?.findFile(COMIC_INFO_FILE)?.lastModified() ?: return false
val postedTime = posted?.let { ParserUtils.parseDate(it) } ?: return false
// stable 30 days after posted
val stableTime = postedTime + 30L * 24L * 60L * 60L * 1000L
return downloadTime > stableTime
}
runSuspendCatching {
DownloadManager.downloadInfoList.parMapNotNull {
if (it.state == DownloadInfo.STATE_FINISH && !it.isStable()) it else null
}.apply {
fillGalleryListByApi(this, EhUrl.referer)
val toUpdate = parMap { di ->
di.galleryInfo.also { SpiderDen(it, di.dirname!!).writeComicInfo(false) }
}
EhDB.updateGalleryInfo(toUpdate)
launchSnackBar(context.getString(R.string.settings_download_reload_metadata_successfully, toUpdate.size))
}
}.onFailure {
launchSnackBar(context.getString(R.string.settings_download_reload_metadata_failed, it.displayString()))
}
}
WorkPreference(
title = stringResource(id = R.string.settings_download_restore_download_items),
summary = stringResource(id = R.string.settings_download_restore_download_items_summary),
@@ -329,6 +329,10 @@
<string name="settings_download_save_as_cbz">保存为 CBZ 压缩包</string>
<string name="settings_download_archive_metadata">压缩包元数据</string>
<string name="settings_download_archive_metadata_summary">下载压缩包时生成 ComicInfo.xml</string>
<string name="settings_download_reload_metadata">重新加载元数据</string>
<string name="settings_download_reload_metadata_summary">为标签可能发生变动的下载项更新 ComicInfo.xml</string>
<string name="settings_download_reload_metadata_successfully">成功加载 %d 项</string>
<string name="settings_download_reload_metadata_failed">加载元数据失败: %s</string>
<string name="settings_download_media_scan">允许媒体扫描</string>
<string name="settings_download_media_scan_summary_on">请避免他人翻看你的图库应用</string>
<string name="settings_download_media_scan_summary_off">大多数图库应用将不会显示下载目录中的图片</string>
+4
View File
@@ -375,6 +375,10 @@
<string name="settings_download_save_as_cbz">Save as CBZ archive</string>
<string name="settings_download_archive_metadata">Archive metadata</string>
<string name="settings_download_archive_metadata_summary">Generate ComicInfo.xml on archive download</string>
<string name="settings_download_reload_metadata">Reload metadata</string>
<string name="settings_download_reload_metadata_summary">Regenerate the ComicInfo.xml for download items whose tags may have changed</string>
<string name="settings_download_reload_metadata_successfully">Reload %d items successfully</string>
<string name="settings_download_reload_metadata_failed">Reload metadata failed: %s</string>
<string name="settings_download_restore_download_items">Restore download items</string>
<string name="settings_download_restore_download_items_summary">Restore all download items in download location</string>
<string name="settings_download_restore_not_found">Not found download items to restore</string>