/lce-data

representation of an asynchronous load of a data with kotlin flow

Primary LanguageKotlinApache License 2.0Apache-2.0

d{}

version build license

representation of an asynchronous load of a data with support of kotlin flow

installation

repositories {
    jcenter()
}

dependencies {
    implementation("at.florianschuster.data:lce-data:$version")
}

concept

val uninitializedData = Data.Uninitialized
val loadingData = Data.Loading
val successData = Data.Success(/*some value*/)
val failureData = Data.Failure(/*some throwable*/)

val evaluatedData = Data { /*some operation*/ }
if (evaluatedData is Data.Success) {
    val dataValue = evaluatedData()
}

val suspendedData = dataOf { /*some suspending operation*/ }
val flowData = dataFlowOf { /*some suspending operation*/ }

possible use case

suspend fun loadBooks(): List<Book>

class Model {
    val bookList = dataFlowOf { loadBooks() }
}

class View {
    private val model = Model()
    
    init {
        model.bookList.onEach {
            loadingIndicator.isVisible = it is Data.Loading
            when(it) {
                is Data.Success -> { /*update ui*/ }
                is Data.Failure -> { /*show error*/ }
            }
        }.launchIn(someScope)
    }
}

author

visit my website.