Skip to content

Commit

Permalink
Merge pull request #4641 from nextcloud/bugfix/4605/fixDuplicateMessages
Browse files Browse the repository at this point in the history
Bugfix/4605/fix duplicate messages
  • Loading branch information
rapterjet2004 authored Jan 22, 2025
2 parents 4f49dea + 9b6b012 commit 1ac537b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
11 changes: 4 additions & 7 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ class ChatActivity :

private val onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
chatViewModel.handleChatOnBackPress()
if (currentlyPlayedVoiceMessage != null) {
stopMediaPlayer(currentlyPlayedVoiceMessage!!)
}
Expand Down Expand Up @@ -674,12 +673,10 @@ class ChatActivity :
val urlForChatting =
ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)

if (adapter?.isEmpty == true) {
chatViewModel.loadMessages(
withCredentials = credentials!!,
withUrl = urlForChatting
)
}
chatViewModel.loadMessages(
withCredentials = credentials!!,
withUrl = urlForChatting
)
} else {
Log.w(
TAG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface ChatMessageRepository : LifecycleAwareManager {

fun setData(conversationModel: ConversationModel, credentials: String, urlForChatting: String)

fun loadInitialMessages(withNetworkParams: Bundle): Job
fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle)

/**
* Loads messages from local storage. If the messages are not found, then it
Expand Down Expand Up @@ -74,11 +74,6 @@ interface ChatMessageRepository : LifecycleAwareManager {
*/
suspend fun getMessage(messageId: Long, bundle: Bundle): Flow<ChatMessage>

/**
* Destroys unused resources.
*/
fun handleChatOnBackPress()

@Suppress("LongParameterList")
suspend fun sendChatMessage(
credentials: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.io.IOException
import javax.inject.Inject
Expand Down Expand Up @@ -111,7 +112,7 @@ class OfflineFirstChatRepository @Inject constructor(

private var newXChatLastCommonRead: Int? = null
private var itIsPaused = false
private val scope = CoroutineScope(Dispatchers.IO)
private lateinit var scope: CoroutineScope

lateinit var internalConversationId: String
private lateinit var conversationModel: ConversationModel
Expand All @@ -125,7 +126,12 @@ class OfflineFirstChatRepository @Inject constructor(
internalConversationId = conversationModel.internalId
}

override fun loadInitialMessages(withNetworkParams: Bundle): Job =
override fun initScopeAndLoadInitialMessages(withNetworkParams: Bundle) {
scope = CoroutineScope(Dispatchers.IO)
loadInitialMessages(withNetworkParams)
}

private fun loadInitialMessages(withNetworkParams: Bundle): Job =
scope.launch {
Log.d(TAG, "---- loadInitialMessages ------------")
newXChatLastCommonRead = conversationModel.lastCommonReadMessage
Expand Down Expand Up @@ -302,7 +308,7 @@ class OfflineFirstChatRepository @Inject constructor(

var showUnreadMessagesMarker = true

while (true) {
while (isActive) {
if (!networkMonitor.isOnline.value || itIsPaused) {
Thread.sleep(HALF_SECOND)
} else {
Expand All @@ -318,11 +324,15 @@ class OfflineFirstChatRepository @Inject constructor(
val weHaveMessagesFromOurself = chatMessages.any { it.actorId == currentUser.userId }
showUnreadMessagesMarker = showUnreadMessagesMarker && !weHaveMessagesFromOurself

handleNewAndTempMessages(
receivedChatMessages = chatMessages,
lookIntoFuture = true,
showUnreadMessagesMarker = showUnreadMessagesMarker
)
if (isActive) {
handleNewAndTempMessages(
receivedChatMessages = chatMessages,
lookIntoFuture = true,
showUnreadMessagesMarker = showUnreadMessagesMarker
)
} else {
Log.d(TAG, "scope was already canceled")
}
} else {
Log.d(TAG, "resultsFromSync are null or empty")
}
Expand Down Expand Up @@ -793,10 +803,6 @@ class OfflineFirstChatRepository @Inject constructor(
}

override fun handleOnStop() {
// unused atm
}

override fun handleChatOnBackPress() {
scope.cancel()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class ChatViewModel @Inject constructor(
val bundle = Bundle()
bundle.putString(BundleKeys.KEY_CHAT_URL, withUrl)
bundle.putString(BundleKeys.KEY_CREDENTIALS, withCredentials)
chatRepository.loadInitialMessages(
chatRepository.initScopeAndLoadInitialMessages(
withNetworkParams = bundle
)
}
Expand Down Expand Up @@ -647,10 +647,6 @@ class ChatViewModel @Inject constructor(
_getCapabilitiesViewState.value = GetCapabilitiesStartState
}

fun handleChatOnBackPress() {
chatRepository.handleChatOnBackPress()
}

fun getMessageById(url: String, conversationModel: ConversationModel, messageId: Long): Flow<ChatMessage> =
flow {
val bundle = Bundle()
Expand Down

0 comments on commit 1ac537b

Please sign in to comment.