Bearings of Platinum
02/28/2024, 12:44 AMvar isFetching by rememberSaveable { mutableStateOf(false) }
// some networking code that sets it to true whenever a network request starts,
// and back to false once the network request completes or fails
AnimatedContent(isFetching, ...) { ... }
It works fine but since the network requests usually complete before the AnimatedContent
composable even has a single frame to show content, is there a way to force it to show the content as if isFetching
were true for a bit longer, like 100ms?Alex Vanyo
02/28/2024, 1:16 AMvar isFetching by rememberSaveable { mutableStateOf(false) }
val shouldDisplayLoading by produceState(isFetching) {
@OptIn(FlowPreview::class)
snapshotFlow { isFetching }
.debounce(100)
.collect { value = it }
}
AnimatedContent(shouldDisplayLoading, ...) { ... }
This keeps the source of truth of each layer distinct and clear:
isFetching
still represents precisely if the request is happening.
AnimatedContent
still does its normal thing, based on the value of a Boolean
state.
shouldDisplayLoading
is a new piece of state introduced to represent precisely whether or not you want to show the content. And then shouldDisplayLoading
can be derived from isFetching
using the debouncing logic of your choice.myanmarking
02/28/2024, 9:25 AMStylianos Gakis
02/28/2024, 10:17 AMmyanmarking
02/28/2024, 12:13 PMStylianos Gakis
02/28/2024, 12:30 PMmyanmarking
02/28/2024, 12:41 PM