Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created February 1, 2024 06:20
Show Gist options
  • Save skydoves/18485962eab8d7bac108579aff8062c3 to your computer and use it in GitHub Desktop.
Save skydoves/18485962eab8d7bac108579aff8062c3 to your computer and use it in GitHub Desktop.
channel_viewmodel
@HiltViewModel
class ChannelViewModel @Inject constructor(
private val repository: ChannelRepository
) : ViewModel() {
private val userFlow = repository.streamUserFlow()
private val channelEvent: MutableSharedFlow<ChannelEvent> = publishedFlow()
internal val channelUiState: SharedFlow<ChannelUiState> =
combine(channelEvent, userFlow) { event, user ->
event to user
}.flatMapLatest { pair ->
val (event, user) = pair
when (event) {
is ChannelEvent.JoinDefaultChannels -> {
val response = repository.joinDefaultChannels(user = user)
if (response.isSuccess) {
flowOf(ChannelUiState.JoinSuccess)
} else {
flowOf(ChannelUiState.Error(response.messageOrNull))
}
}
..
}.asStateFlow(ChannelUiState.Idle)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment