-
-
Save skydoves/18485962eab8d7bac108579aff8062c3 to your computer and use it in GitHub Desktop.
channel_viewmodel
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 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