Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created September 19, 2024 11:32
Show Gist options
  • Save skydoves/212d081125a3cfac501c83abecec3063 to your computer and use it in GitHub Desktop.
Save skydoves/212d081125a3cfac501c83abecec3063 to your computer and use it in GitHub Desktop.
restartableStateIn
@HiltViewModel
class MainViewModel @Inject constructor(
repository: TimelineRepository
): ViewModel() {
val timelineUi: RestartableStateFlow<ScreenUi?> = repository.fetchTimelineUi()
.flatMapLatest { response -> flowOf(response.getOrNull()) }
.restartableStateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = null
)
// This can be launched from UI side, such as LaunchedEffect or anywhere.
fun restartTimeline() {
timelineUi.restart()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment