Skip to content

Commit

Permalink
refactor: Now, Folder.threads is a realmSet instead of a realmList
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Dec 12, 2024
1 parent 50b64d8 commit 858eca1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ object RealmDatabase {
//region Configurations versions
const val USER_INFO_SCHEMA_VERSION = 2L
const val MAILBOX_INFO_SCHEMA_VERSION = 7L
const val MAILBOX_CONTENT_SCHEMA_VERSION = 19L
const val MAILBOX_CONTENT_SCHEMA_VERSION = 20L
//endregion

//region Configurations names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.infomaniak.mail.utils.SentryDebug
import io.realm.kotlin.dynamic.DynamicMutableRealmObject
import io.realm.kotlin.dynamic.DynamicRealmObject
import io.realm.kotlin.dynamic.getValue
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.migration.AutomaticSchemaMigration
import io.realm.kotlin.migration.AutomaticSchemaMigration.MigrationContext

Expand All @@ -38,6 +39,7 @@ val MAILBOX_INFO_MIGRATION = AutomaticSchemaMigration { migrationContext ->
val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
SentryDebug.addMigrationBreadcrumb(migrationContext)
migrationContext.deleteRealmFromFirstMigration()
migrationContext.keepThreadsInFolderAfterNineteenthMigration()
}

// Migrate to version #1
Expand Down Expand Up @@ -73,3 +75,20 @@ private fun MigrationContext.keepDefaultValuesAfterSixthMigration() {
}
}
}

/**
* Migrate from version #19
*/
private fun MigrationContext.keepThreadsInFolderAfterNineteenthMigration() {
if (oldRealm.schemaVersion() <= 19L) {
enumerate(className = "Folder") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
newObject?.apply {
// Backup previous value
set(
propertyName = "threads",
value = oldObject.getObjectList(propertyName = "threads").mapTo(realmSetOf(), newRealm::findLatest),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.realm.kotlin.MutableRealm
import io.realm.kotlin.Realm
import io.realm.kotlin.TypedRealm
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.notifications.ResultsChange
import io.realm.kotlin.query.RealmQuery
import io.realm.kotlin.query.RealmResults
Expand Down Expand Up @@ -213,7 +213,7 @@ class FolderController @Inject constructor(
}

fun deleteSearchFolderData(realm: MutableRealm) = with(getOrCreateSearchFolder(realm)) {
threads = realmListOf()
threads = realmSetOf()
}
//endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.isManaged
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.ext.toRealmSet
import io.realm.kotlin.notifications.ResultsChange
import io.realm.kotlin.notifications.SingleQueryChange
import io.realm.kotlin.query.*
Expand Down Expand Up @@ -169,7 +169,7 @@ class ThreadController @Inject constructor(
suspend fun saveThreads(searchMessages: List<Message>) {
mailboxContentRealm().write {
FolderController.getOrCreateSearchFolder(realm = this).apply {
threads = searchMessages.convertToSearchThreads().toRealmList()
threads = searchMessages.convertToSearchThreads().toRealmSet()
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/infomaniak/mail/data/models/Folder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import com.infomaniak.mail.utils.Utils
import io.realm.kotlin.TypedRealm
import io.realm.kotlin.ext.backlinks
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.serializers.RealmListKSerializer
import io.realm.kotlin.types.RealmInstant
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.RealmSet
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down Expand Up @@ -70,7 +72,7 @@ class Folder : RealmObject, Cloneable {
@Transient
var unreadCountLocal: Int = 0
@Transient
var threads = realmListOf<Thread>()
var threads = realmSetOf<Thread>()

/**
* List of old Messages UIDs of this Folder that we need to fetch.
Expand Down Expand Up @@ -119,7 +121,7 @@ class Folder : RealmObject, Cloneable {
lastUpdatedAt: RealmInstant?,
cursor: String?,
unreadCount: Int,
threads: RealmList<Thread>,
threads: RealmSet<Thread>,
oldMessagesUidsToFetch: RealmList<Int>,
newMessagesUidsToFetch: RealmList<Int>,
remainingOldMessagesToFetch: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Thread : RealmObject {
var isLocallyMovedOut: Boolean = false
//endregion

// TODO: Remove this `runCatching / getOrElse` when the issue is fixed
private val _folders by backlinks(Folder::threads)
val folder
get() = runCatching {
Expand Down

0 comments on commit 858eca1

Please sign in to comment.