Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hello247.android.child.chat.oneononeconversation.viewmodel
- import androidx.lifecycle.*
- import com.hello247.android.child.chat.oneononeconversation.ChatMessageBubble
- import com.hello247.android.child.chat.oneononeconversation.viewmodel.OneOnOneChatConversationViewModel.*
- import com.hello247.android.child.chat.oneononeconversation.viewmodel.OneOnOneChatConversationViewModel.State as ViewModelState
- import com.hello247.app.usecases.common.ViewFullNameOfUserUseCase
- import com.hello247.app.usecases.common.ViewProfilePictureOfUserUseCase
- import com.hello247.app.usecases.communitysupporter.chat.ReadChatConversationOfCommunitySupporterUseCase
- import com.hello247.app.usecases.communitysupporter.chat.ReadChatConversationOfCommunitySupporterUseCase.ChatMessage
- import com.hello247.app.usecases.communitysupporter.chat.SendChatImageMessageToCommunitySupporterUseCase
- import com.hello247.app.usecases.communitysupporter.chat.SendChatTextMessageToCommunitySupporterUseCase
- import com.hello247.utility.Result
- import com.hello247.utility.Image
- import com.hello247.utility.State
- import kotlinx.collections.immutable.*
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.ExperimentalCoroutinesApi
- import kotlinx.coroutines.FlowPreview
- import kotlinx.coroutines.channels.Channel
- import kotlinx.coroutines.channels.ConflatedBroadcastChannel
- import kotlinx.coroutines.flow.*
- @FlowPreview
- @ExperimentalCoroutinesApi
- class OneOnOneChatConversationViewModelUsingUseCases(
- private val opponentCommunitySupporterId: Int,
- private val viewProfilePictureOfUserUseCase: ViewProfilePictureOfUserUseCase,
- private val viewFullNameOfUserUseCase: ViewFullNameOfUserUseCase,
- private val sendChatTextMessageToCommunitySupporterUseCase: SendChatTextMessageToCommunitySupporterUseCase,
- private val sendChatImageMessageToCommunitySupporterUseCase: SendChatImageMessageToCommunitySupporterUseCase,
- private val readChatChatConversationOfCommunitySupporterUseCase: ReadChatConversationOfCommunitySupporterUseCase
- ) : ViewModel(), OneOnOneChatConversationViewModel {
- private val chatScreenToolbarPicture: LiveData<Image> = liveData {
- emit(viewProfilePictureOfUserUseCase(opponentCommunitySupporterId))
- }
- private val chatScreenToolbarTitle: LiveData<String> = liveData {
- emit(viewFullNameOfUserUseCase(opponentCommunitySupporterId))
- }
- private val chatMessagesStates: Flow<State<ImmutableList<ChatMessage<ChatMessage.Content>>>> =
- readChatChatConversationOfCommunitySupporterUseCase(opponentCommunitySupporterId)
- .catch { exception ->
- println("ERROR VIEWMODEL")
- exception.printStackTrace()
- _errorMessage.offer("Something went wrong: $exception")
- }// RELEVANT
- private val chatMessagesErrorStates: Flow<State.Error> = chatMessagesStates.filterIsInstance<State.Error>()// RELEVANT
- private val chatMessagesNonErrorStates: Flow<State<ImmutableList<ChatMessage<ChatMessage.Content>>>> =
- chatMessagesStates.filterNot { it is State.Error }// RELEVANT
- private val chatScreenChatMessageBubbles: LiveData<List<ChatMessageBubble<ChatMessageBubble.Type>>> =
- chatMessagesNonErrorStates.map { chatMessagesNonErrorState ->
- when (chatMessagesNonErrorState) {
- is State.Loading -> persistentListOf()
- is State.Data -> chatMessagesNonErrorState
- .data
- .map { it.asChatMessageBubble() }
- .toImmutableList()
- is State.Error -> throw IllegalStateException(
- "It is illegal for State.Error to be found in chatMessagesNonErrorStates."
- )
- }
- }.asLiveData()// RELEVANT
- private val _errorMessage = ConflatedBroadcastChannel<String>()
- private val errorMessage: Flow<String> = _errorMessage.asFlow()
- private val isLoadingChatMessages: Flow<Boolean> = chatMessagesStates.map { it is State.Loading } // RELEVANT
- private val chatScreenTextMessageField = MutableLiveData<String>("")
- private val chatScreenNavigateUpEvents: Channel<ViewModelState.ChatScreenShown.NavigateUpEvent> = Channel()
- private fun onChatScreenSendButtonClick() {
- viewModelScope.launch {
- val textMessage = chatScreenTextMessageField.value!!
- emptyChatScreenTextMessageInput()
- val result = sendChatTextMessageToCommunitySupporterUseCase(opponentCommunitySupporterId, textMessage)
- if (result is Result.Failure) _errorMessage.offer("Something went wrong: ${result.exception}")
- }
- }
- private fun onChatScreenUpSectionClick() {
- chatScreenNavigateUpEvents.offer(ViewModelState.ChatScreenShown.NavigateUpEvent)
- }
- private fun emptyChatScreenTextMessageInput() {
- chatScreenTextMessageField.value = ""
- }
- private fun onChatScreenAttachmentButtonClick() {
- _state.value = selectMediaScreenState
- }
- private fun onSelectMediaScreenImageSelect(selectedImage: Image) {
- viewModelScope.launch {
- val result = sendChatImageMessageToCommunitySupporterUseCase(opponentCommunitySupporterId, selectedImage)
- if (result is Result.Failure) _errorMessage.offer("Something went wrong: ${result.exception}")
- }
- }
- private val chatScreenState: ViewModelState.ChatScreenShown = object : ViewModelState.ChatScreenShown() {
- override val toolbarPicture: LiveData<Image> = chatScreenToolbarPicture
- override val toolbarTitle: LiveData<String> = chatScreenToolbarTitle
- override val chatMessageBubbles: LiveData<List<ChatMessageBubble<ChatMessageBubble.Type>>> =
- chatScreenChatMessageBubbles
- override val textMessageField: MutableLiveData<String> = chatScreenTextMessageField
- override val isCircularSpinnerVisible: LiveData<Boolean> = isLoadingChatMessages.asLiveData() // RELEVANT
- override val navigateUpEvents: Flow<NavigateUpEvent> = chatScreenNavigateUpEvents.consumeAsFlow()
- override val showShowToastEvents: Flow<ShowToastEvent> = errorMessage
- .map { ShowToastEvent(it) }
- override fun onAttachmentButtonClick() = onChatScreenAttachmentButtonClick()
- override fun onUpSectionClick() = onChatScreenUpSectionClick()
- override fun onSendButtonClick() = onChatScreenSendButtonClick()
- }
- private val selectMediaScreenState: ViewModelState.SelectImageScreenShown =
- object : ViewModelState.SelectImageScreenShown() {
- override fun onImageSelect(selectedImage: Image) = onSelectMediaScreenImageSelect(selectedImage)
- }
- private val _state = MutableLiveData<ViewModelState>(chatScreenState)
- override val state: LiveData<ViewModelState> = _state
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement