commonMain.com.eygraber.vice.loadable.ViceLoadable.kt Maven / Gradle / Ivy
package com.eygraber.vice.loadable
import androidx.compose.runtime.Immutable
@Immutable
public sealed interface ViceLoadable {
public val value: T
@Immutable
public data class Loading(override val value: T) : ViceLoadable
@Immutable
public data class Loaded(override val value: T) : ViceLoadable
}
public inline val ViceLoadable<*>.isLoading: Boolean get() = this is ViceLoadable.Loading
public inline val ViceLoadable<*>.isNotLoading: Boolean get() = this !is ViceLoadable.Loading
public inline val ViceLoadable<*>.isLoaded: Boolean get() = this is ViceLoadable.Loaded
public inline val ViceLoadable<*>.isNotLoaded: Boolean get() = this !is ViceLoadable.Loaded
public inline val ViceLoadable.loadedValueOrNull: T? get() = when(this) {
is ViceLoadable.Loading -> null
is ViceLoadable.Loaded -> value
}
public inline fun ViceLoadable.map(
mapper: (T) -> ViceLoadable,
): ViceLoadable = mapper(value)
public inline fun ViceLoadable.mapValue(
mapper: (T) -> R,
): ViceLoadable = when(this) {
is ViceLoadable.Loading -> ViceLoadable.Loading(mapper(value))
is ViceLoadable.Loaded -> ViceLoadable.Loaded(mapper(value))
}