Improved galleryBlock loading logic

This commit is contained in:
Pupil
2020-02-13 20:14:26 +09:00
parent bb63959678
commit d05c1e4d08

View File

@@ -102,21 +102,27 @@ class Cache(context: Context) : ContextWrapper(context) {
suspend fun getGalleryBlock(galleryID: Int): GalleryBlock? { suspend fun getGalleryBlock(galleryID: Int): GalleryBlock? {
val metadata = Cache(this).getCachedMetadata(galleryID) val metadata = Cache(this).getCachedMetadata(galleryID)
val source = mapOf( val sources = mapOf(
Code.HITOMI to { xyz.quaver.hitomi.getGalleryBlock(galleryID) }, Code.HITOMI to { xyz.quaver.hitomi.getGalleryBlock(galleryID) },
Code.HIYOBI to { xyz.quaver.hiyobi.getGalleryBlock(galleryID) } Code.HIYOBI to { xyz.quaver.hiyobi.getGalleryBlock(galleryID) }
) )
val galleryBlock = if (metadata?.galleryBlock == null) val galleryBlock = if (metadata?.galleryBlock == null) {
source.entries.map {
CoroutineScope(Dispatchers.IO).async { CoroutineScope(Dispatchers.IO).async {
kotlin.runCatching { var galleryBlock: GalleryBlock? = null
it.value.invoke()
for (source in sources) {
galleryBlock = kotlin.runCatching {
source.value.invoke()
}.getOrNull() }.getOrNull()
if (galleryBlock != null)
break
}
galleryBlock
}.await() ?: return null
} }
}.firstOrNull {
it.await() != null
}?.await()
else else
metadata.galleryBlock metadata.galleryBlock
@@ -164,11 +170,10 @@ class Cache(context: Context) : ContextWrapper(context) {
} }
retval retval
}.await() }.await() ?: return null
} else } else
metadata.reader metadata.reader
if (reader != null)
setCachedMetadata( setCachedMetadata(
galleryID, galleryID,
Metadata(Cache(this).getCachedMetadata(galleryID), readers = reader) Metadata(Cache(this).getCachedMetadata(galleryID), readers = reader)