Skip to content

Instantly share code, notes, and snippets.

@skydoves
Created August 1, 2022 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skydoves/9ae30516dfe0ee27f40ef73b5c4e6d77 to your computer and use it in GitHub Desktop.
Save skydoves/9ae30516dfe0ee27f40ef73b5c4e6d77 to your computer and use it in GitHub Desktop.
messages_viewmodel
@HiltViewModel
class WhatsAppMessagesViewModel @Inject constructor(
@Dispatcher(WhatsAppDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
private val chatClient: ChatClient
) : ViewModel() {
private val messageMutableUiState =
MutableStateFlow<WhatsAppMessageUiState>(WhatsAppMessageUiState.Loading)
val messageUiSate: StateFlow<WhatsAppMessageUiState> = messageMutableUiState
private fun fetchChannel(channelId: String) {
viewModelScope.launch(ioDispatcher) {
val result = chatClient.channel(channelId).watch().await()
result.onSuccess {
messageMutableUiState.value = WhatsAppMessageUiState.Success(result.data())
}.onError {
messageMutableUiState.value = WhatsAppMessageUiState.Error
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment