SourceListingLoader.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/browse/SourceListingLoader.kt
1package eu.friedcutlet.yokan.ui.screens.browse23import eu.friedcutlet.yokan.domain.Source4import eu.friedcutlet.yokan.services.extensions.ExtensionLogger5import eu.friedcutlet.yokan.services.extensions.ExtensionRuntimeService6import eu.friedcutlet.yokan.services.extensions.SourceFilter7import eu.friedcutlet.yokan.services.extensions.SourceListing8import kotlinx.coroutines.CancellationException9import eu.friedcutlet.yokan.services.catchingNonCancellation10import eu.friedcutlet.yokan.services.extensions.withSourceRuntime1112internal enum class SourceTab(val label: String) {13 Popular("Popular"),14 Latest("Latest"),15 Search("Search"),16}1718internal data class RuntimeLoad<T>(val value: T, val warning: String? = null)1920internal class SourceListingLoader(private val services: eu.friedcutlet.yokan.app.YokanServices) {21internal suspend fun loadSourceListing(22 source: Source,23 tab: SourceTab,24 query: String,25 page: Int,26 filters: List<SourceFilter>,27): RuntimeLoad<SourceListing> = withRuntimeFallback(source) { runtime ->28 when (tab) {29 SourceTab.Popular -> runtime.popular(page)30 SourceTab.Latest -> runtime.latest(page)31 SourceTab.Search -> runtime.search(query, page, filters)32 }33}3435internal suspend fun loadSourceFilters(source: Source): List<SourceFilter> {36 return catchingNonCancellation {37 services.extensionRuntimeFactory.withSourceRuntime(38 database = services.database,39 sourceId = source.id,40 onFallback = { ExtensionLogger.error(source.name, "filterList", it) },41 ) { it.filterList() }42 }43 .onFailure { fallbackError -> ExtensionLogger.error(source.name, "filterList fallback", fallbackError) }44 .getOrThrow()45}4647/** Whether the runtime offers a Latest listing; falls back to the stored flag when the runtime fails. */48internal suspend fun loadSupportsLatest(source: Source): Boolean {49 return catchingNonCancellation {50 services.extensionRuntimeFactory.withSourceRuntime(services.database, source.id) { it.supportsLatest() }51 }52 .onFailure { ExtensionLogger.error(source.name, "supportsLatest", it) }53 .getOrDefault(source.supportLatest)54}5556private suspend fun <T> withRuntimeFallback(source: Source, block: suspend (ExtensionRuntimeService) -> T): RuntimeLoad<T> {57 var warning: String? = null58 return try {59 val value = services.extensionRuntimeFactory.withSourceRuntime(60 database = services.database,61 sourceId = source.id,62 onFallback = { error ->63 ExtensionLogger.error(source.name, "runtime call", error)64 warning = "Runtime fallback for ${source.name}: ${error.message.orEmpty().ifBlank { error::class.simpleName.orEmpty() }}"65 },66 block = block,67 )68 RuntimeLoad(value, warning)69 } catch (fallbackError: Throwable) {70 val fallbackWarning = warning71 if (fallbackError is CancellationException || fallbackWarning == null) throw fallbackError72 ExtensionLogger.error(source.name, "runtime fallback", fallbackError)73 throw IllegalStateException(fallbackWarning, fallbackError)74 }75}7677}78