Show notification while download cache is renewing

Since users seem to be confused now that the library loads before download info is shown...
This commit is contained in:
arkon 2022-10-29 11:39:04 -04:00
parent 69635ee66a
commit fde7bfa3d1
4 changed files with 82 additions and 44 deletions

View File

@ -48,6 +48,8 @@ class DownloadCache(
private val scope = CoroutineScope(Dispatchers.IO) private val scope = CoroutineScope(Dispatchers.IO)
private val notifier by lazy { DownloadNotifier(context) }
/** /**
* The interval after which this cache should be invalidated. 1 hour shouldn't cause major * The interval after which this cache should be invalidated. 1 hour shouldn't cause major
* issues, as the cache is only used for UI feedback. * issues, as the cache is only used for UI feedback.
@ -241,56 +243,62 @@ class DownloadCache(
} }
renewalJob = scope.launchIO { renewalJob = scope.launchIO {
var sources = getSources() try {
notifier.onCacheProgress()
// Try to wait until extensions and sources have loaded var sources = getSources()
withTimeout(30.seconds) {
while (!extensionManager.isInitialized) {
delay(2.seconds)
}
while (sources.isEmpty()) { // Try to wait until extensions and sources have loaded
delay(2.seconds) withTimeout(30.seconds) {
sources = getSources() while (!extensionManager.isInitialized) {
} delay(2.seconds)
} }
val sourceDirs = rootDownloadsDir.dir.listFiles().orEmpty() while (sources.isEmpty()) {
.associate { it.name to SourceDirectory(it) } delay(2.seconds)
.mapNotNullKeys { entry -> sources = getSources()
sources.find {
provider.getSourceDirName(it).equals(entry.key, ignoreCase = true)
}?.id
}
rootDownloadsDir.sourceDirs = sourceDirs
sourceDirs.values
.map { sourceDir ->
async {
val mangaDirs = sourceDir.dir.listFiles().orEmpty()
.filterNot { it.name.isNullOrBlank() }
.associate { it.name!! to MangaDirectory(it) }
sourceDir.mangaDirs = ConcurrentHashMap(mangaDirs)
mangaDirs.values.forEach { mangaDir ->
val chapterDirs = mangaDir.dir.listFiles().orEmpty()
.mapNotNull { chapterDir ->
chapterDir.name
?.replace(".cbz", "")
?.takeUnless { it.endsWith(Downloader.TMP_DIR_SUFFIX) }
}
.toMutableSet()
mangaDir.chapterDirs = chapterDirs
}
} }
} }
.awaitAll()
lastRenew = System.currentTimeMillis() val sourceDirs = rootDownloadsDir.dir.listFiles().orEmpty()
notifyChanges() .associate { it.name to SourceDirectory(it) }
.mapNotNullKeys { entry ->
sources.find {
provider.getSourceDirName(it).equals(entry.key, ignoreCase = true)
}?.id
}
rootDownloadsDir.sourceDirs = sourceDirs
sourceDirs.values
.map { sourceDir ->
async {
val mangaDirs = sourceDir.dir.listFiles().orEmpty()
.filterNot { it.name.isNullOrBlank() }
.associate { it.name!! to MangaDirectory(it) }
sourceDir.mangaDirs = ConcurrentHashMap(mangaDirs)
mangaDirs.values.forEach { mangaDir ->
val chapterDirs = mangaDir.dir.listFiles().orEmpty()
.mapNotNull { chapterDir ->
chapterDir.name
?.replace(".cbz", "")
?.takeUnless { it.endsWith(Downloader.TMP_DIR_SUFFIX) }
}
.toMutableSet()
mangaDir.chapterDirs = chapterDirs
}
}
}
.awaitAll()
lastRenew = System.currentTimeMillis()
notifyChanges()
} finally {
notifier.dismissCacheProgress()
}
} }
} }

View File

@ -45,6 +45,17 @@ internal class DownloadNotifier(private val context: Context) {
} }
} }
private val cacheNotificationBuilder by lazy {
context.notificationBuilder(Notifications.CHANNEL_DOWNLOADER_CACHE) {
setSmallIcon(R.drawable.ic_tachi)
setContentTitle(context.getString(R.string.download_notifier_cache_renewal))
setProgress(100, 100, true)
setOngoing(true)
setAutoCancel(false)
setOnlyAlertOnce(true)
}
}
/** /**
* Status of download. Used for correct notification icon. * Status of download. Used for correct notification icon.
*/ */
@ -233,4 +244,14 @@ internal class DownloadNotifier(private val context: Context) {
errorThrown = true errorThrown = true
isDownloading = false isDownloading = false
} }
fun onCacheProgress() {
with(cacheNotificationBuilder) {
show(Notifications.ID_DOWNLOAD_CACHE)
}
}
fun dismissCacheProgress() {
context.notificationManager.cancel(Notifications.ID_DOWNLOAD_CACHE)
}
} }

View File

@ -42,6 +42,8 @@ object Notifications {
const val ID_DOWNLOAD_CHAPTER_COMPLETE = -203 const val ID_DOWNLOAD_CHAPTER_COMPLETE = -203
const val CHANNEL_DOWNLOADER_ERROR = "downloader_error_channel" const val CHANNEL_DOWNLOADER_ERROR = "downloader_error_channel"
const val ID_DOWNLOAD_CHAPTER_ERROR = -202 const val ID_DOWNLOAD_CHAPTER_ERROR = -202
const val CHANNEL_DOWNLOADER_CACHE = "downloader_cache_renewal"
const val ID_DOWNLOAD_CACHE = -204
/** /**
* Notification channel and ids used by the library updater. * Notification channel and ids used by the library updater.
@ -159,6 +161,11 @@ object Notifications {
setGroup(GROUP_DOWNLOADER) setGroup(GROUP_DOWNLOADER)
setShowBadge(false) setShowBadge(false)
}, },
buildNotificationChannel(CHANNEL_DOWNLOADER_CACHE, IMPORTANCE_LOW) {
setName(context.getString(R.string.channel_downloader_cache))
setGroup(GROUP_DOWNLOADER)
setShowBadge(false)
},
buildNotificationChannel(CHANNEL_BACKUP_RESTORE_PROGRESS, IMPORTANCE_LOW) { buildNotificationChannel(CHANNEL_BACKUP_RESTORE_PROGRESS, IMPORTANCE_LOW) {
setName(context.getString(R.string.channel_progress)) setName(context.getString(R.string.channel_progress))
setGroup(GROUP_BACKUP_RESTORE) setGroup(GROUP_BACKUP_RESTORE)

View File

@ -863,6 +863,7 @@
<string name="download_notifier_split_page_not_found">Page %d not found while splitting</string> <string name="download_notifier_split_page_not_found">Page %d not found while splitting</string>
<string name="download_notifier_split_page_path_not_found">Couldn\'t find file path of page %d</string> <string name="download_notifier_split_page_path_not_found">Couldn\'t find file path of page %d</string>
<string name="download_notifier_split_failed">Couldn\'t split downloaded image</string> <string name="download_notifier_split_failed">Couldn\'t split downloaded image</string>
<string name="download_notifier_cache_renewal">Indexing downloads</string>
<!-- Notification channels --> <!-- Notification channels -->
<string name="channel_common">Common</string> <string name="channel_common">Common</string>
@ -873,6 +874,7 @@
<string name="channel_new_chapters">Chapter updates</string> <string name="channel_new_chapters">Chapter updates</string>
<string name="channel_app_updates">App updates</string> <string name="channel_app_updates">App updates</string>
<string name="channel_ext_updates">Extension updates</string> <string name="channel_ext_updates">Extension updates</string>
<string name="channel_downloader_cache">Download cache</string>
<string name="channel_crash_logs">Crash logs</string> <string name="channel_crash_logs">Crash logs</string>
<!-- S Pen actions --> <!-- S Pen actions -->