Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.79 KB | None | 0 0
  1. package com.hello247.android.child.chat.oneononeconversation.viewmodel
  2.  
  3. import androidx.lifecycle.*
  4. import com.hello247.android.child.chat.oneononeconversation.ChatMessageBubble
  5. import com.hello247.android.child.chat.oneononeconversation.viewmodel.OneOnOneChatConversationViewModel.*
  6. import com.hello247.android.child.chat.oneononeconversation.viewmodel.OneOnOneChatConversationViewModel.State as ViewModelState
  7. import com.hello247.app.usecases.common.ViewFullNameOfUserUseCase
  8. import com.hello247.app.usecases.common.ViewProfilePictureOfUserUseCase
  9. import com.hello247.app.usecases.communitysupporter.chat.ReadChatConversationOfCommunitySupporterUseCase
  10. import com.hello247.app.usecases.communitysupporter.chat.ReadChatConversationOfCommunitySupporterUseCase.ChatMessage
  11. import com.hello247.app.usecases.communitysupporter.chat.SendChatImageMessageToCommunitySupporterUseCase
  12. import com.hello247.app.usecases.communitysupporter.chat.SendChatTextMessageToCommunitySupporterUseCase
  13. import com.hello247.utility.Result
  14. import com.hello247.utility.Image
  15. import com.hello247.utility.State
  16. import kotlinx.collections.immutable.*
  17. import kotlinx.coroutines.launch
  18. import kotlinx.coroutines.ExperimentalCoroutinesApi
  19. import kotlinx.coroutines.FlowPreview
  20. import kotlinx.coroutines.channels.Channel
  21. import kotlinx.coroutines.channels.ConflatedBroadcastChannel
  22. import kotlinx.coroutines.flow.*
  23.  
  24. @FlowPreview
  25. @ExperimentalCoroutinesApi
  26. class OneOnOneChatConversationViewModelUsingUseCases(
  27.     private val opponentCommunitySupporterId: Int,
  28.     private val viewProfilePictureOfUserUseCase: ViewProfilePictureOfUserUseCase,
  29.     private val viewFullNameOfUserUseCase: ViewFullNameOfUserUseCase,
  30.     private val sendChatTextMessageToCommunitySupporterUseCase: SendChatTextMessageToCommunitySupporterUseCase,
  31.     private val sendChatImageMessageToCommunitySupporterUseCase: SendChatImageMessageToCommunitySupporterUseCase,
  32.     private val readChatChatConversationOfCommunitySupporterUseCase: ReadChatConversationOfCommunitySupporterUseCase
  33. ) : ViewModel(), OneOnOneChatConversationViewModel {
  34.  
  35.     private val chatScreenToolbarPicture: LiveData<Image> = liveData {
  36.         emit(viewProfilePictureOfUserUseCase(opponentCommunitySupporterId))
  37.     }
  38.  
  39.     private val chatScreenToolbarTitle: LiveData<String> = liveData {
  40.         emit(viewFullNameOfUserUseCase(opponentCommunitySupporterId))
  41.     }
  42.  
  43.     private val chatMessagesStates: Flow<State<ImmutableList<ChatMessage<ChatMessage.Content>>>> =
  44.         readChatChatConversationOfCommunitySupporterUseCase(opponentCommunitySupporterId)
  45.             .catch { exception ->
  46.                 println("ERROR VIEWMODEL")
  47.                 exception.printStackTrace()
  48.                 _errorMessage.offer("Something went wrong: $exception")
  49.             }// RELEVANT
  50.  
  51.     private val chatMessagesErrorStates: Flow<State.Error> = chatMessagesStates.filterIsInstance<State.Error>()// RELEVANT
  52.  
  53.     private val chatMessagesNonErrorStates: Flow<State<ImmutableList<ChatMessage<ChatMessage.Content>>>> =
  54.         chatMessagesStates.filterNot { it is State.Error }// RELEVANT
  55.  
  56.     private val chatScreenChatMessageBubbles: LiveData<List<ChatMessageBubble<ChatMessageBubble.Type>>> =
  57.         chatMessagesNonErrorStates.map { chatMessagesNonErrorState ->
  58.             when (chatMessagesNonErrorState) {
  59.                 is State.Loading -> persistentListOf()
  60.                 is State.Data -> chatMessagesNonErrorState
  61.                     .data
  62.                     .map { it.asChatMessageBubble() }
  63.                     .toImmutableList()
  64.                 is State.Error -> throw IllegalStateException(
  65.                     "It is illegal for State.Error to be found in chatMessagesNonErrorStates."
  66.                 )
  67.             }
  68.         }.asLiveData()// RELEVANT
  69.  
  70.     private val _errorMessage = ConflatedBroadcastChannel<String>()
  71.     private val errorMessage: Flow<String> = _errorMessage.asFlow()
  72.  
  73.     private val isLoadingChatMessages: Flow<Boolean> = chatMessagesStates.map { it is State.Loading } // RELEVANT
  74.  
  75.     private val chatScreenTextMessageField = MutableLiveData<String>("")
  76.  
  77.     private val chatScreenNavigateUpEvents: Channel<ViewModelState.ChatScreenShown.NavigateUpEvent> = Channel()
  78.  
  79.     private fun onChatScreenSendButtonClick() {
  80.         viewModelScope.launch {
  81.             val textMessage = chatScreenTextMessageField.value!!
  82.             emptyChatScreenTextMessageInput()
  83.             val result = sendChatTextMessageToCommunitySupporterUseCase(opponentCommunitySupporterId, textMessage)
  84.             if (result is Result.Failure) _errorMessage.offer("Something went wrong: ${result.exception}")
  85.         }
  86.     }
  87.  
  88.     private fun onChatScreenUpSectionClick() {
  89.         chatScreenNavigateUpEvents.offer(ViewModelState.ChatScreenShown.NavigateUpEvent)
  90.     }
  91.  
  92.     private fun emptyChatScreenTextMessageInput() {
  93.         chatScreenTextMessageField.value = ""
  94.     }
  95.  
  96.     private fun onChatScreenAttachmentButtonClick() {
  97.         _state.value = selectMediaScreenState
  98.     }
  99.  
  100.     private fun onSelectMediaScreenImageSelect(selectedImage: Image) {
  101.         viewModelScope.launch {
  102.             val result = sendChatImageMessageToCommunitySupporterUseCase(opponentCommunitySupporterId, selectedImage)
  103.             if (result is Result.Failure) _errorMessage.offer("Something went wrong: ${result.exception}")
  104.         }
  105.     }
  106.  
  107.     private val chatScreenState: ViewModelState.ChatScreenShown = object : ViewModelState.ChatScreenShown() {
  108.         override val toolbarPicture: LiveData<Image> = chatScreenToolbarPicture
  109.         override val toolbarTitle: LiveData<String> = chatScreenToolbarTitle
  110.         override val chatMessageBubbles: LiveData<List<ChatMessageBubble<ChatMessageBubble.Type>>> =
  111.             chatScreenChatMessageBubbles
  112.  
  113.         override val textMessageField: MutableLiveData<String> = chatScreenTextMessageField
  114.  
  115.         override val isCircularSpinnerVisible: LiveData<Boolean> = isLoadingChatMessages.asLiveData() // RELEVANT
  116.  
  117.         override val navigateUpEvents: Flow<NavigateUpEvent> = chatScreenNavigateUpEvents.consumeAsFlow()
  118.  
  119.         override val showShowToastEvents: Flow<ShowToastEvent> = errorMessage
  120.             .map { ShowToastEvent(it) }
  121.  
  122.         override fun onAttachmentButtonClick() = onChatScreenAttachmentButtonClick()
  123.  
  124.         override fun onUpSectionClick() = onChatScreenUpSectionClick()
  125.  
  126.         override fun onSendButtonClick() = onChatScreenSendButtonClick()
  127.  
  128.     }
  129.  
  130.     private val selectMediaScreenState: ViewModelState.SelectImageScreenShown =
  131.         object : ViewModelState.SelectImageScreenShown() {
  132.             override fun onImageSelect(selectedImage: Image) = onSelectMediaScreenImageSelect(selectedImage)
  133.         }
  134.  
  135.     private val _state = MutableLiveData<ViewModelState>(chatScreenState)
  136.     override val state: LiveData<ViewModelState> = _state
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement