All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.seiko.imageloader.component.ComponentRegistry.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
package com.seiko.imageloader.component

import com.seiko.imageloader.component.decoder.Decoder
import com.seiko.imageloader.component.fetcher.Fetcher
import com.seiko.imageloader.component.keyer.Keyer
import com.seiko.imageloader.component.mapper.Mapper
import com.seiko.imageloader.request.Options
import com.seiko.imageloader.request.SourceResult
import com.seiko.imageloader.util.forEachIndices

class ComponentRegistry internal constructor(
    private val mappers: List>,
    private val keyers: List,
    private val fetcherFactories: List,
    private val decoderFactories: List,
) {
    fun map(data: Any, options: Options): Any {
        var mappedData = data
        mappers.forEachIndices { mapper ->
            mapper.map(mappedData, options)?.let { mappedData = it }
        }
        return mappedData
    }

    fun key(data: Any, options: Options): String? {
        keyers.forEachIndices { keyer ->
            keyer.key(data, options)?.let { return it }
        }
        return null
    }

    fun fetch(
        data: Any,
        options: Options,
        startIndex: Int = 0,
    ): Pair {
        for (index in startIndex until fetcherFactories.size) {
            val factory = fetcherFactories[index]
            factory.create(data, options)?.let { return it to index }
        }
        throw RuntimeException("Unable to create a fetcher that supports: $data")
    }

    suspend fun decode(
        source: SourceResult,
        options: Options,
        startIndex: Int = 0,
    ): Pair {
        for (index in startIndex until decoderFactories.size) {
            val factory = decoderFactories[index]
            factory.create(source, options)?.let { return it to index }
        }
        throw RuntimeException("Unable to create a decoder that supports: $source")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy