Skip to content

Commit

Permalink
Correctly copyFromRealm() the mailboxLocalValues when we want to …
Browse files Browse the repository at this point in the history
…update them
  • Loading branch information
KevinBoulongne committed Sep 30, 2024
1 parent 56f1a67 commit 8e192fd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import io.realm.kotlin.MutableRealm
import io.realm.kotlin.Realm
import io.realm.kotlin.TypedRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.query
import io.realm.kotlin.notifications.SingleQueryChange
import io.realm.kotlin.query.*
Expand Down Expand Up @@ -100,13 +101,10 @@ class MailboxController @Inject constructor(
val remoteMailboxesIds = remoteMailboxes.map { remoteMailbox ->

SentryLog.d(RealmDatabase.TAG, "Mailboxes: Get current data")
val previousLocalValues = getMailbox(userId, remoteMailbox.mailboxId, realm = this)?.local
val previousLocalValues = getMailbox(userId, remoteMailbox.mailboxId, realm = this)?.local?.copyFromRealm()

SentryLog.d(RealmDatabase.TAG, "Mailboxes: Save new data")
remoteMailbox.initLocalValues(
userId = userId,
localValues = previousLocalValues,
)
remoteMailbox.initLocalValues(userId, previousLocalValues)
copyToRealm(remoteMailbox, UpdatePolicy.ALL)

return@map remoteMailbox.mailboxId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.infomaniak.mail.data.models.draft.Draft.DraftMode
import com.infomaniak.mail.data.models.signature.Signature
import com.infomaniak.mail.utils.UnreadDisplay
import com.infomaniak.mail.utils.extensions.getDefault
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.serializers.RealmListKSerializer
import io.realm.kotlin.types.RealmObject
Expand Down Expand Up @@ -91,6 +92,12 @@ class Mailbox : RealmObject {
local.userId = userId
}

fun setFeatureFlags(featureFlags: List<String>) {
mailboxLocalValues = local.copyFromRealm().also {
it.featureFlags.setFeatureFlags(featureFlags)
}
}

fun getDefaultSignatureWithFallback(draftMode: DraftMode? = null): Signature = with(local) {
return signatures.getDefault(draftMode) ?: signatures.first()
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/mail/utils/SharedUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class SharedUtils @Inject constructor(
fun updateFeatureFlags(mailboxObjectId: String, mailboxUuid: String) {
with(ApiRepository.getFeatureFlags(mailboxUuid)) {
if (isSuccess()) {
mailboxController.updateMailbox(mailboxObjectId) { mailbox ->
mailbox.local.featureFlags.setFeatureFlags(featureFlags = data ?: emptyList())
mailboxController.updateMailbox(mailboxObjectId) {
it.setFeatureFlags(featureFlags = data ?: emptyList())
}
}
}
Expand Down

0 comments on commit 8e192fd

Please sign in to comment.