@@ -108,8 +108,7 @@ class ConversationsListViewModel @Inject constructor(
108108
109109 sealed class ConversationReadUnreadUiState {
110110 data object None : ConversationReadUnreadUiState ()
111- data class Success (val conversationDisplayName : String , val isMarkedRead : Boolean ) :
112- ConversationReadUnreadUiState ()
111+ data object Success : ConversationReadUnreadUiState ()
113112 data object Error : ConversationReadUnreadUiState ()
114113 }
115114
@@ -582,38 +581,54 @@ class ConversationsListViewModel @Inject constructor(
582581
583582 @Suppress(" Detekt.TooGenericExceptionCaught" )
584583 fun markConversationAsRead (conversation : ConversationModel ) {
584+ val original = conversation.copy()
585+ val optimistic = conversation.copy(unreadMessages = 0 , unreadMention = false , unreadMentionDirect = false )
585586 val messageId = if (conversation.remoteServer.isNullOrEmpty()) conversation.lastMessage?.id?.toInt() else null
586587 val apiVersion = ApiUtils .getChatApiVersion(
587588 currentUser.capabilities?.spreedCapability!! ,
588589 intArrayOf(ApiUtils .API_V1 )
589590 )
590591 val url = ApiUtils .getUrlForChatReadMarker(apiVersion, currentUser.baseUrl, conversation.token)
591592 viewModelScope.launch {
593+ withContext(Dispatchers .IO ) {
594+ repository.updateConversationLocallyAndEmit(currentUser, optimistic)
595+ }
592596 try {
593597 withContext(Dispatchers .IO ) {
594598 withRetry(1 ) { conversationsRepository.markConversationAsRead(credentials, url, messageId) }
595599 }
596- _readUnreadState .value = ConversationReadUnreadUiState .Success (conversation.displayName, true )
600+ _readUnreadState .value = ConversationReadUnreadUiState .Success
597601 } catch (e: Exception ) {
602+ withContext(Dispatchers .IO ) {
603+ repository.updateConversationLocallyAndEmit(currentUser, original)
604+ }
598605 _readUnreadState .value = ConversationReadUnreadUiState .Error
599606 }
600607 }
601608 }
602609
603610 @Suppress(" Detekt.TooGenericExceptionCaught" )
604611 fun markConversationAsUnread (conversation : ConversationModel ) {
612+ val original = conversation.copy()
613+ val optimistic = conversation.copy(unreadMessages = 1 )
605614 val apiVersion = ApiUtils .getChatApiVersion(
606615 currentUser.capabilities?.spreedCapability!! ,
607616 intArrayOf(ApiUtils .API_V1 )
608617 )
609618 val url = ApiUtils .getUrlForChatReadMarker(apiVersion, currentUser.baseUrl, conversation.token)
610619 viewModelScope.launch {
620+ withContext(Dispatchers .IO ) {
621+ repository.updateConversationLocallyAndEmit(currentUser, optimistic)
622+ }
611623 try {
612624 withContext(Dispatchers .IO ) {
613625 withRetry(1 ) { conversationsRepository.markConversationAsUnread(credentials, url) }
614626 }
615- _readUnreadState .value = ConversationReadUnreadUiState .Success (conversation.displayName, false )
627+ _readUnreadState .value = ConversationReadUnreadUiState .Success
616628 } catch (e: Exception ) {
629+ withContext(Dispatchers .IO ) {
630+ repository.updateConversationLocallyAndEmit(currentUser, original)
631+ }
617632 _readUnreadState .value = ConversationReadUnreadUiState .Error
618633 }
619634 }
0 commit comments