Closed xVemu closed 8 months ago
Hey there, yes Store supports that. Also check out the current site for an overview of Fetcher
, SourceOfTruth
, and StoreBuilder
. For now, here's a simple getting started:
Fetcher
suspend fun makeNetworkRequest(args: Args): Data {
TODO()
}
val fetcher = Fetcher.of<Args, Data> { args ->
flowOf(makeNetworkRequest(args))
}
SourceOfTruth
val sourceOfTruth = SourceOfTruth.of<Args, Data, Data>(
reader = {args: Args ->
flowOf(db.get(args))
},
writer = { (args: Args, data: Data) ->
db.put(args, data)
}
)
StoreBuilder
val storeBuilder = StoreBuilder.from(
fetcher = fetcher,
sourceOfTruth = sourceOfTruth
)
val store = storeBuilder.build()
// In your ViewModel
val request = StoreReadRequest.fresh(args)
val stream = store.stream(request).collect { response: StoreReadResponse ->
when (response) {
is StoreReadResponse.Loading -> setState { Loading }
is StoreReadResponse.Data -> setState { Data(response.data) }
is StoreReadResponse.Error -> setState { Error }
}
}
// In your View
val state = viewModel.collectAsState()
when (state.value) {
is Loading -> displayLoadingView()
is Data -> displayLoadedView()
is Error -> displayErrorView()
}
Display Refresh Indicator When Loading
// In your View val state = viewModel.collectAsState() when (state.value) { is Loading -> displayLoadingView() is Data -> displayLoadedView() is Error -> displayErrorView() }
But when state
is a type of Loading
, I lose access to data, right?
Store's loading response doesn't include data. If I were solving that, my view model would be responsible for merging isRefreshing
and lastData
into one view state.
Is your feature request related to a problem? Please describe. I'm trying to:
Does this package support such a feature?