Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit aaba16e3 authored by Rafael Tonholo's avatar Rafael Tonholo
Browse files

chore(di): add koin multibinding function for lists

parent 1f608c3a
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ package net.thunderbird.app.common.core
import android.content.Context
import kotlin.time.ExperimentalTime
import net.thunderbird.app.common.core.logging.DefaultLogLevelManager
import net.thunderbird.core.common.inject.getList
import net.thunderbird.core.common.inject.singleListOf
import net.thunderbird.core.logging.DefaultLogger
import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogLevelManager
@@ -23,16 +25,14 @@ val appCommonCoreModule: Module = module {
        DefaultLogLevelManager()
    }.bind<LogLevelProvider>()

    single<List<LogSink>> {
        listOf(
            ConsoleLogSink(level = LogLevel.VERBOSE),
    singleListOf<LogSink>(
        { ConsoleLogSink(level = LogLevel.VERBOSE) },
    )
    }

    single<CompositeLogSink> {
        CompositeLogSink(
            logLevelProvider = get(),
            sinks = get(),
            sinks = getList(),
        )
    }

@@ -46,7 +46,7 @@ val appCommonCoreModule: Module = module {
    single<CompositeLogSink>(named(SYNC_DEBUG_LOG)) {
        CompositeLogSink(
            logLevelProvider = get(),
            sinks = get(),
            sinks = getList(),
        )
    }

+64 −0
Original line number Diff line number Diff line
package net.thunderbird.core.common.inject

import org.koin.core.definition.Definition
import org.koin.core.module.KoinDslMarker
import org.koin.core.module.Module
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.Qualifier
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope

// This file must be deleted once https://github.com/InsertKoinIO/koin/pull/1951 is merged to
// Koin and released on 4.2.0

/**
 * Defines a singleton list of elements of type [T].
 *
 * This function creates a singleton definition for a mutable list of elements.
 * Each element in the list is resolved from the provided [items] definitions.
 *
 * @param T The type of elements in the list.
 * @param items Vararg of [Definition]s that will be resolved and added to the list.
 * @param qualifier Optional [Qualifier] to distinguish this list from others of the same type.
 *                  If null, a default qualifier based on the type [T] will be used.
 */
@KoinDslMarker
inline fun <reified T> Module.singleListOf(vararg items: Definition<T>, qualifier: Qualifier? = null) {
    single(qualifier ?: defaultListQualifier<T>(), createdAtStart = true) {
        items.map { definition -> definition(this, parametersOf()) }
    }
}

/**
 * Resolves a [List] of instances of type [T].
 * This is a helper function for Koin's multibinding feature.
 *
 * It uses the [defaultListQualifier] if no [qualifier] is provided.
 *
 * @param T The type of instances in the list.
 * @param qualifier An optional [Qualifier] to distinguish between different lists of the same type.
 * @return The resolved [MutableList] of instances of type [T].
 */
inline fun <reified T> Scope.getList(qualifier: Qualifier? = null) =
    get<List<T>>(qualifier ?: defaultListQualifier<T>())

/**
 * Creates a qualifier for a set of a specific type.
 *
 * This is used to differentiate between different sets of the same type when injecting dependencies.
 *
 * @param T The type of the elements in the set.
 * @return A qualifier for the set.
 */
inline fun <reified T> defaultListQualifier() =
    defaultCollectionQualifier<List<T>, T>()

/**
 * Creates a default [Qualifier] for a collection binding.
 *
 * @param TCollection The type of the collection (e.g., `List`, `List`).
 * @param T The type of the elements in the collection.
 * @return A [Qualifier] that can be used to identify the specific collection binding.
 */
inline fun <reified TCollection : Collection<T>, reified T> defaultCollectionQualifier() =
    named("${TCollection::class.qualifiedName}<${T::class.qualifiedName}>")