commonMain.org.kodein.emoji.compose.EmojiService.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of emoji-compose Show documentation
Show all versions of emoji-compose Show documentation
Emoji support for Compose/Multiplatform
package org.kodein.emoji.compose
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.produceState
import kotlinx.coroutines.*
import org.kodein.emoji.Emoji
import org.kodein.emoji.EmojiFinder
import org.kodein.emoji.EmojiTemplateCatalog
import org.kodein.emoji.all
@OptIn(ExperimentalCoroutinesApi::class)
@Composable
internal fun Deferred.consumeAsState(initialValue: T) =
produceState(
initialValue = if (isCompleted) getCompleted() else initialValue,
producer = { value = await() }
)
public class EmojiService private constructor(
public val catalog: EmojiTemplateCatalog,
public val finder: EmojiFinder,
) {
public companion object {
private lateinit var deferred: Deferred
public var catalogBuilder: EmojiTemplateCatalog.Builder.() -> Unit = {}
set(value) {
if (::deferred.isInitialized) error("Cannot set catalogBuilder after Service has been initialized or accessed.")
field = value
}
public fun initialize() {
if (!::deferred.isInitialized) {
@OptIn(DelicateCoroutinesApi::class)
deferred = GlobalScope.async {
val catalog = async(Dispatchers.Default) { EmojiTemplateCatalog(Emoji.all(), catalogBuilder) }
val finder = async(Dispatchers.Default) { EmojiFinder() }
EmojiService(catalog.await(), finder.await())
}
}
}
@Composable
public fun get(): EmojiService? {
initialize()
val service: EmojiService? by deferred.consumeAsState(null)
return service
}
public suspend fun await(): EmojiService {
initialize()
return deferred.await()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy