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 { var galleryBlock: GalleryBlock? = null
kotlin.runCatching {
it.value.invoke() for (source in sources) {
}.getOrNull() galleryBlock = kotlin.runCatching {
} source.value.invoke()
}.firstOrNull { }.getOrNull()
it.await() != null
}?.await() if (galleryBlock != null)
break
}
galleryBlock
}.await() ?: return null
}
else else
metadata.galleryBlock metadata.galleryBlock
@@ -164,15 +170,14 @@ 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) )
)
return reader return reader
} }