Skip to content

Commit

Permalink
refactor: Extract each RefreshStrategy to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Mar 7, 2025
1 parent 612359e commit 106bce9
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.data.LocalSettings.ThreadMode
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshMode.*
import com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies.RefreshStrategy
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.Folder.FolderRole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies

import android.content.Context
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.message.Message
Expand All @@ -29,50 +32,15 @@ import io.realm.kotlin.types.RealmSet
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ensureActive

interface RefreshStrategy {
fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread>
fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole>
fun shouldHideEmptyFolder(): Boolean

fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message?

fun processDeletedMessage(
scope: CoroutineScope,
managedMessage: Message,
context: Context,
mailbox: Mailbox,
realm: MutableRealm,
)

/**
* If an extra folder needs its unread count updated but no thread has that extra folder as [Thread.folderId], you can add the
* extra folder inside this method as they will be inserted inside the list of impacted folders.
*/
fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders)
fun processDeletedThread(thread: Thread, realm: MutableRealm)
fun shouldQueryFolderThreadsOnDeletedUid(): Boolean

/**
* About the [impactedThreadsManaged]:
* This set will be updated throughout the whole process of handling added Messages.
* It represents all the Threads that will need to be recomputed to reflect the changes of the newly added Messages.
* We need to pass down a reference to the MutableSet to enable both addition and removal of Threads in it.
*/
fun handleAddedMessages(
scope: CoroutineScope,
remoteMessage: Message,
isConversationMode: Boolean,
impactedThreadsManaged: MutableSet<Thread>,
realm: MutableRealm,
)
}
val defaultRefreshStrategy = object : DefaultRefreshStrategy {}

interface DefaultRefreshStrategy : RefreshStrategy {
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
return ThreadController.getThreadsByFolderId(folderId, realm)
}

override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = emptyList()

override fun shouldHideEmptyFolder(): Boolean = false

override fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message? {
Expand Down Expand Up @@ -104,6 +72,7 @@ interface DefaultRefreshStrategy : RefreshStrategy {
override fun shouldQueryFolderThreadsOnDeletedUid(): Boolean = false

private fun String.toLongUid(folderId: String) = "${this}@${folderId}"

private fun Thread.getNumberOfMessagesInFolder() = messages.count { message -> message.folderId == folderId }

override fun handleAddedMessages(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies

import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.thread.Thread
import io.realm.kotlin.TypedRealm

val inboxRefreshStrategy = object : DefaultRefreshStrategy {
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = false, realm = realm)
}

override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.SNOOZED)

override fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders) {
impactedFolders += folderId
impactedFolders += Folder.FolderRole.SNOOZED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies

import android.content.Context
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.data.models.thread.Thread
import io.realm.kotlin.MutableRealm
import io.realm.kotlin.TypedRealm
import kotlinx.coroutines.CoroutineScope

interface RefreshStrategy {
fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread>

fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole>

fun shouldHideEmptyFolder(): Boolean

fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message?

fun processDeletedMessage(
scope: CoroutineScope,
managedMessage: Message,
context: Context,
mailbox: Mailbox,
realm: MutableRealm,
)

/**
* If an extra folder needs its unread count updated but no thread has that extra folder as [Thread.folderId], you can add the
* extra folder inside this method as they will be inserted inside the list of impacted folders.
*/
fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders)

fun processDeletedThread(thread: Thread, realm: MutableRealm)

fun shouldQueryFolderThreadsOnDeletedUid(): Boolean

/**
* About the [impactedThreadsManaged]:
* This set will be updated throughout the whole process of handling added Messages.
* It represents all the Threads that will need to be recomputed to reflect the changes of the newly added Messages.
* We need to pass down a reference to the MutableSet to enable both addition and removal of Threads in it.
*/
fun handleAddedMessages(
scope: CoroutineScope,
remoteMessage: Message,
isConversationMode: Boolean,
impactedThreadsManaged: MutableSet<Thread>,
realm: MutableRealm,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies

val scheduledDraftRefreshStrategy = object : DefaultRefreshStrategy {
override fun shouldHideEmptyFolder(): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.data.cache.mailboxContent
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies

import android.content.Context
import com.infomaniak.mail.data.cache.mailboxContent.FolderController
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.message.Message
Expand All @@ -27,31 +31,13 @@ import io.realm.kotlin.MutableRealm
import io.realm.kotlin.TypedRealm
import kotlinx.coroutines.CoroutineScope

val defaultRefreshStrategy = object : DefaultRefreshStrategy {}

val inboxRefreshStrategy = object : DefaultRefreshStrategy {
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = false, realm = realm)
}

override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.SNOOZED)

override fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders) {
impactedFolders += folderId
impactedFolders += Folder.FolderRole.SNOOZED
}
}

val scheduledDraftRefreshStrategy = object : DefaultRefreshStrategy {
override fun shouldHideEmptyFolder(): Boolean = true
}

val snoozeRefreshStrategy = object : DefaultRefreshStrategy {
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = true, realm = realm)
}

override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.INBOX)

override fun shouldHideEmptyFolder(): Boolean = true

override fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message? {
Expand Down Expand Up @@ -79,6 +65,7 @@ val snoozeRefreshStrategy = object : DefaultRefreshStrategy {
}

override fun processDeletedThread(thread: Thread, realm: MutableRealm) = thread.recomputeThread()

override fun shouldQueryFolderThreadsOnDeletedUid(): Boolean = true

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/infomaniak/mail/data/models/Folder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import androidx.annotation.StringRes
import com.infomaniak.lib.core.utils.Utils.enumValueOfOrNull
import com.infomaniak.lib.core.utils.removeAccents
import com.infomaniak.mail.R
import com.infomaniak.mail.data.cache.mailboxContent.*
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies.*
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.data.models.thread.Thread
import com.infomaniak.mail.utils.SentryDebug
Expand Down

0 comments on commit 106bce9

Please sign in to comment.