representation of an asynchronous load of a data with support of kotlin flow
repositories {
jcenter()
}
dependencies {
implementation("at.florianschuster.data:lce-data:$version")
}
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*/ }
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)
}
}
visit my website.