Skip to content

Commit

Permalink
refactor: Upsert the remoteMessage with the localMessage local values…
Browse files Browse the repository at this point in the history
…, instead of updating the localMessage with the remoteMessage snooze state
  • Loading branch information
KevinBoulongne committed Mar 7, 2025
1 parent a5a9bde commit 38f558f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.infomaniak.mail.data.cache.mailboxContent

import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.data.models.message.Message.MessageInitialState
import com.infomaniak.mail.data.models.thread.Thread
import io.realm.kotlin.MutableRealm
import io.realm.kotlin.TypedRealm
Expand All @@ -36,20 +37,38 @@ val snoozeRefreshStrategy = object : DefaultRefreshStrategy {
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = true, realm = realm)
}

/**
* In the case of the Snooze refresh strategy, the Message could already exist (because it comes from the INBOX).
* In this situation, we don't want to loose its data (for example the body).
* So we take the [remoteMessage] (because it contains the up-to-date data about the snooze state),
* we give it the localMessage local values, then we upsert it into Realm.
*/
override fun handleAddedMessages(
scope: CoroutineScope,
remoteMessage: Message,
isConversationMode: Boolean,
impactedThreadsManaged: MutableSet<Thread>,
realm: MutableRealm,
) {
impactedThreadsManaged += buildSet {
MessageController.updateMessage(remoteMessage.uid, realm) { localMessage ->
localMessage?.snoozeState = remoteMessage.snoozeState
localMessage?.snoozeEndDate = remoteMessage.snoozeEndDate
localMessage?.snoozeAction = remoteMessage.snoozeAction
localMessage?.threads?.let(::addAll)
}

MessageController.getMessage(remoteMessage.uid, realm)?.let { localMessage ->
remoteMessage.initLocalValues(
messageInitialState = MessageInitialState(
date = localMessage.date,
isFullyDownloaded = localMessage.isFullyDownloaded(),
isTrashed = localMessage.isTrashed,
isFromSearch = localMessage.isFromSearch,
draftLocalUuid = localMessage.draftLocalUuid,
),
latestCalendarEventResponse = localMessage.latestCalendarEventResponse,
messageIds = localMessage.messageIds,
swissTransferFiles = localMessage.swissTransferFiles,
)
remoteMessage.keepHeavyData(localMessage)
}

val updatedMessage = MessageController.upsertMessage(remoteMessage, realm)

impactedThreadsManaged += updatedMessage.threads
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ class MessageController @Inject constructor(private val mailboxContentRealm: Rea
//endregion

//region Edit data
fun upsertMessage(message: Message, realm: MutableRealm) {
realm.copyToRealm(message, UpdatePolicy.ALL)
}
fun upsertMessage(message: Message, realm: MutableRealm): Message = realm.copyToRealm(message, UpdatePolicy.ALL)

fun updateMessage(messageUid: String, realm: MutableRealm, onUpdate: (Message?) -> Unit) {
onUpdate(getMessage(messageUid, realm))
Expand Down

0 comments on commit 38f558f

Please sign in to comment.