From d05c1e4d08e2efa267acecb610b61e92e7f155a6 Mon Sep 17 00:00:00 2001 From: Pupil Date: Thu, 13 Feb 2020 20:14:26 +0900 Subject: [PATCH] Improved galleryBlock loading logic --- .../xyz/quaver/pupil/util/download/Cache.kt | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt b/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt index 6c75b55d..f7a659f6 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt @@ -102,21 +102,27 @@ class Cache(context: Context) : ContextWrapper(context) { suspend fun getGalleryBlock(galleryID: Int): GalleryBlock? { val metadata = Cache(this).getCachedMetadata(galleryID) - val source = mapOf( + val sources = mapOf( Code.HITOMI to { xyz.quaver.hitomi.getGalleryBlock(galleryID) }, Code.HIYOBI to { xyz.quaver.hiyobi.getGalleryBlock(galleryID) } ) - val galleryBlock = if (metadata?.galleryBlock == null) - source.entries.map { - CoroutineScope(Dispatchers.IO).async { - kotlin.runCatching { - it.value.invoke() - }.getOrNull() - } - }.firstOrNull { - it.await() != null - }?.await() + val galleryBlock = if (metadata?.galleryBlock == null) { + CoroutineScope(Dispatchers.IO).async { + var galleryBlock: GalleryBlock? = null + + for (source in sources) { + galleryBlock = kotlin.runCatching { + source.value.invoke() + }.getOrNull() + + if (galleryBlock != null) + break + } + + galleryBlock + }.await() ?: return null + } else metadata.galleryBlock @@ -164,15 +170,14 @@ class Cache(context: Context) : ContextWrapper(context) { } retval - }.await() + }.await() ?: return null } else metadata.reader - if (reader != null) - setCachedMetadata( - galleryID, - Metadata(Cache(this).getCachedMetadata(galleryID), readers = reader) - ) + setCachedMetadata( + galleryID, + Metadata(Cache(this).getCachedMetadata(galleryID), readers = reader) + ) return reader }