-
-
Save skydoves/9ae30516dfe0ee27f40ef73b5c4e6d77 to your computer and use it in GitHub Desktop.
messages_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 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