Skip to content

Commit

Permalink
Use copyFromRealm() for all MailboxLocalValues updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Oct 10, 2024
1 parent 44a7a32 commit 774e409
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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?.copyFromRealm()
val previousLocalValues = getMailbox(userId, remoteMailbox.mailboxId, realm = this)?.local

SentryLog.d(RealmDatabase.TAG, "Mailboxes: Save new data")
remoteMailbox.initLocalValues(userId, previousLocalValues)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package com.infomaniak.mail.data.models.mailbox

import androidx.core.app.NotificationManagerCompat
import com.infomaniak.mail.data.models.AppSettings
import com.infomaniak.mail.data.models.Quotas
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
import com.infomaniak.mail.data.models.signature.Signature
import com.infomaniak.mail.utils.UnreadDisplay
Expand Down Expand Up @@ -87,9 +88,32 @@ class Mailbox : RealmObject {
private fun createObjectId(userId: Int): String = "${userId}_${this.mailboxId}"

fun initLocalValues(userId: Int, localValues: MailboxLocalValues?) {
localValues?.let { mailboxLocalValues = it }

// If we have any backup from a previous Mailbox already stored in Realm, use it.
localValues?.let { mailboxLocalValues = it.copyFromRealm() }

objectId = createObjectId(userId)
local.userId = userId
mailboxLocalValues = local.setUserId(userId, bypassRealmCopy = true)
}

fun setUnreadCountLocal(unreadCount: Int) {
mailboxLocalValues = local.setUnreadCountLocal(unreadCount)
}

fun setQuotas(quotas: Quotas?) {
mailboxLocalValues = local.setQuotas(quotas)
}

fun setPermissions(permissions: MailboxPermissions?) {
mailboxLocalValues = local.setPermissions(permissions)
}

fun setSignatures(signatures: List<Signature>) {
mailboxLocalValues = local.setSignatures(signatures)
}

fun setExternalMailInfo(externalMailInfo: MailboxExternalMailInfo) {
mailboxLocalValues = local.setExternalMailInfo(externalMailInfo)
}

fun setFeatureFlags(featureFlags: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.infomaniak.mail.data.models.AppSettings
import com.infomaniak.mail.data.models.FeatureFlag
import com.infomaniak.mail.data.models.Quotas
import com.infomaniak.mail.data.models.signature.Signature
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.types.EmbeddedRealmObject
Expand All @@ -29,17 +30,70 @@ import io.realm.kotlin.types.annotations.Ignore
class MailboxLocalValues : EmbeddedRealmObject {

var userId: Int = AppSettings.DEFAULT_ID
var quotas: Quotas? = null
private set
var unreadCountLocal: Int = 0
private set
var quotas: Quotas? = null
private set
var permissions: MailboxPermissions? = null
private set
var signatures = realmListOf<Signature>()
private var _featureFlags = realmSetOf<String>()
private set
var externalMailFlagEnabled: Boolean = false
private set
var trustedDomains = realmListOf<String>()
private set
private var _featureFlags = realmSetOf<String>()

@Ignore
val featureFlags = FeatureFlags()

fun setUserId(userId: Int, bypassRealmCopy: Boolean = false): MailboxLocalValues {

fun updateUserId(local: MailboxLocalValues) {
local.userId = userId
}

return if (bypassRealmCopy)
this.also(::updateUserId)
else {
update(::updateUserId)
}
}

fun setUnreadCountLocal(unreadCount: Int): MailboxLocalValues {
return update { it.unreadCountLocal = unreadCount }
}

fun setQuotas(quotas: Quotas?): MailboxLocalValues {
return update { it.quotas = quotas }
}

fun setPermissions(permissions: MailboxPermissions?): MailboxLocalValues {
return update { it.permissions = permissions }
}

fun setSignatures(signatures: List<Signature>): MailboxLocalValues {
return update {
it.signatures.apply {
clear()
addAll(signatures)
}
}
}

fun setExternalMailInfo(externalMailInfo: MailboxExternalMailInfo): MailboxLocalValues {
return update {
it.externalMailFlagEnabled = externalMailInfo.externalMailFlagEnabled
it.trustedDomains.apply {
clear()
addAll(externalMailInfo.trustedDomains)
}
}
}

private fun update(onUpdate: (MailboxLocalValues) -> Unit): MailboxLocalValues = copyFromRealm().also(onUpdate)

inner class FeatureFlags {

fun contains(featureFlag: FeatureFlag): Boolean = _featureFlags.contains(featureFlag.apiName)
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import com.infomaniak.mail.views.itemViews.AvatarMergedContactData
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.kotlin.Realm
import io.realm.kotlin.ext.copyFromRealm
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.notifications.ResultsChange
import io.sentry.Attachment
import io.sentry.Sentry
Expand Down Expand Up @@ -346,7 +345,7 @@ class MainViewModel @Inject constructor(
if (mailbox.isLimited) with(ApiRepository.getQuotas(mailbox.hostingId, mailbox.mailboxName)) {
if (isSuccess()) {
mailboxController.updateMailbox(mailbox.objectId) {
it.local.quotas = data
it.setQuotas(data)
}
}
}
Expand All @@ -356,7 +355,7 @@ class MainViewModel @Inject constructor(
SentryLog.d(TAG, "Force refresh Permissions")
with(ApiRepository.getPermissions(mailbox.linkId, mailbox.hostingId)) {
if (isSuccess()) mailboxController.updateMailbox(mailbox.objectId) {
it.local.permissions = data
it.setPermissions(data)
}
}
}
Expand All @@ -377,8 +376,7 @@ class MainViewModel @Inject constructor(
if (!isSuccess()) return@launch
data?.let { externalMailInfo ->
mailboxController.updateMailbox(mailbox.objectId) {
it.local.externalMailFlagEnabled = externalMailInfo.externalMailFlagEnabled
it.local.trustedDomains = externalMailInfo.trustedDomains.toRealmList()
it.setExternalMailInfo(externalMailInfo)
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/infomaniak/mail/utils/SharedUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import com.infomaniak.mail.utils.extensions.getApiException
import com.infomaniak.mail.utils.extensions.getFoldersIds
import com.infomaniak.mail.utils.extensions.getUids
import io.realm.kotlin.Realm
import io.realm.kotlin.ext.toRealmList
import io.sentry.Sentry
import javax.inject.Inject

Expand Down Expand Up @@ -171,10 +170,12 @@ class SharedUtils @Inject constructor(
val signaturesResult = data!!
customRealm.write {
MailboxController.getMailbox(mailbox.objectId, realm = this)?.let { mailbox ->
mailbox.local.signatures = signaturesResult.signatures.toMutableList().apply {
firstOrNull { it.id == signaturesResult.defaultSignatureId }?.isDefault = true
firstOrNull { it.id == signaturesResult.defaultReplySignatureId }?.isDefaultReply = true
}.toRealmList()
mailbox.setSignatures(
signatures = signaturesResult.signatures.toMutableList().apply {
firstOrNull { it.id == signaturesResult.defaultSignatureId }?.isDefault = true
firstOrNull { it.id == signaturesResult.defaultReplySignatureId }?.isDefaultReply = true
},
)
}
}
null
Expand Down

0 comments on commit 774e409

Please sign in to comment.