-
-
Save skydoves/212d081125a3cfac501c83abecec3063 to your computer and use it in GitHub Desktop.
restartableStateIn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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