Skip to content

Commit e5642df

Browse files
refactor: Extract each RefreshStrategy to its own file
1 parent ccbcd1e commit e5642df

File tree

7 files changed

+146
-59
lines changed

7 files changed

+146
-59
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.infomaniak.mail.data.LocalSettings
2323
import com.infomaniak.mail.data.LocalSettings.ThreadMode
2424
import com.infomaniak.mail.data.api.ApiRepository
2525
import com.infomaniak.mail.data.cache.mailboxContent.RefreshController.RefreshMode.*
26+
import com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies.RefreshStrategy
2627
import com.infomaniak.mail.data.cache.mailboxInfo.MailboxController
2728
import com.infomaniak.mail.data.models.Folder
2829
import com.infomaniak.mail.data.models.Folder.FolderRole

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshStrategies.kt app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/refreshStrategies/DefaultRefreshStrategy.kt

+7-38
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package com.infomaniak.mail.data.cache.mailboxContent
18+
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies
1919

2020
import android.content.Context
21+
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
22+
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
23+
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
2124
import com.infomaniak.mail.data.models.Folder
2225
import com.infomaniak.mail.data.models.mailbox.Mailbox
2326
import com.infomaniak.mail.data.models.message.Message
@@ -29,50 +32,15 @@ import io.realm.kotlin.types.RealmSet
2932
import kotlinx.coroutines.CoroutineScope
3033
import kotlinx.coroutines.ensureActive
3134

32-
interface RefreshStrategy {
33-
fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread>
34-
fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole>
35-
fun shouldHideEmptyFolder(): Boolean
36-
37-
fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message?
38-
39-
fun processDeletedMessage(
40-
scope: CoroutineScope,
41-
managedMessage: Message,
42-
context: Context,
43-
mailbox: Mailbox,
44-
realm: MutableRealm,
45-
)
46-
47-
/**
48-
* If an extra folder needs its unread count updated but no thread has that extra folder as [Thread.folderId], you can add the
49-
* extra folder inside this method as they will be inserted inside the list of impacted folders.
50-
*/
51-
fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders)
52-
fun processDeletedThread(thread: Thread, realm: MutableRealm)
53-
fun shouldQueryFolderThreadsOnDeletedUid(): Boolean
54-
55-
/**
56-
* About the `impactedThreadsManaged`:
57-
* This set will be updated throughout the whole process of handling added Messages.
58-
* It represents all the Threads that have been concerned by adding new Messages in the current Folder.
59-
* It should be the full set given everywhere, because sometimes we are adding Threads to it, and sometimes we are removing.
60-
*/
61-
fun handleAddedMessages(
62-
scope: CoroutineScope,
63-
remoteMessage: Message,
64-
isConversationMode: Boolean,
65-
impactedThreadsManaged: MutableSet<Thread>,
66-
realm: MutableRealm,
67-
)
68-
}
35+
val defaultRefreshStrategy = object : DefaultRefreshStrategy {}
6936

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

7542
override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = emptyList()
43+
7644
override fun shouldHideEmptyFolder(): Boolean = false
7745

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

10674
private fun String.toLongUid(folderId: String) = "${this}@${folderId}"
75+
10776
private fun Thread.getNumberOfMessagesInFolder() = messages.count { message -> message.folderId == folderId }
10877

10978
override fun handleAddedMessages(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Infomaniak Mail - Android
3+
* Copyright (C) 2025 Infomaniak Network SA
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies
19+
20+
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
21+
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
22+
import com.infomaniak.mail.data.models.Folder
23+
import com.infomaniak.mail.data.models.thread.Thread
24+
import io.realm.kotlin.TypedRealm
25+
26+
val inboxRefreshStrategy = object : DefaultRefreshStrategy {
27+
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
28+
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = false, realm = realm)
29+
}
30+
31+
override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.SNOOZED)
32+
33+
override fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders) {
34+
impactedFolders += folderId
35+
impactedFolders += Folder.FolderRole.SNOOZED
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Infomaniak Mail - Android
3+
* Copyright (C) 2025 Infomaniak Network SA
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies
19+
20+
import android.content.Context
21+
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
22+
import com.infomaniak.mail.data.models.Folder
23+
import com.infomaniak.mail.data.models.mailbox.Mailbox
24+
import com.infomaniak.mail.data.models.message.Message
25+
import com.infomaniak.mail.data.models.thread.Thread
26+
import io.realm.kotlin.MutableRealm
27+
import io.realm.kotlin.TypedRealm
28+
import kotlinx.coroutines.CoroutineScope
29+
30+
interface RefreshStrategy {
31+
fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread>
32+
33+
fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole>
34+
35+
fun shouldHideEmptyFolder(): Boolean
36+
37+
fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message?
38+
39+
fun processDeletedMessage(
40+
scope: CoroutineScope,
41+
managedMessage: Message,
42+
context: Context,
43+
mailbox: Mailbox,
44+
realm: MutableRealm,
45+
)
46+
47+
/**
48+
* If an extra folder needs its unread count updated but no thread has that extra folder as [Thread.folderId], you can add the
49+
* extra folder inside this method as they will be inserted inside the list of impacted folders.
50+
*/
51+
fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders)
52+
53+
fun processDeletedThread(thread: Thread, realm: MutableRealm)
54+
55+
fun shouldQueryFolderThreadsOnDeletedUid(): Boolean
56+
57+
/**
58+
* About the `impactedThreadsManaged`:
59+
* This set will be updated throughout the whole process of handling added Messages.
60+
* It represents all the Threads that have been concerned by adding new Messages in the current Folder.
61+
* It should be the full set given everywhere, because sometimes we are adding Threads to it, and sometimes we are removing.
62+
*/
63+
fun handleAddedMessages(
64+
scope: CoroutineScope,
65+
remoteMessage: Message,
66+
isConversationMode: Boolean,
67+
impactedThreadsManaged: MutableSet<Thread>,
68+
realm: MutableRealm,
69+
)
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Infomaniak Mail - Android
3+
* Copyright (C) 2025 Infomaniak Network SA
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies
19+
20+
val scheduledDraftRefreshStrategy = object : DefaultRefreshStrategy {
21+
override fun shouldHideEmptyFolder(): Boolean = true
22+
}

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/CustomRefreshStrategies.kt app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/refreshStrategies/SnoozeRefreshStrategy.kt

+7-20
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
* You should have received a copy of the GNU General Public License
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
package com.infomaniak.mail.data.cache.mailboxContent
18+
package com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies
1919

2020
import android.content.Context
21+
import com.infomaniak.mail.data.cache.mailboxContent.FolderController
22+
import com.infomaniak.mail.data.cache.mailboxContent.ImpactedFolders
23+
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
24+
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
2125
import com.infomaniak.mail.data.models.Folder
2226
import com.infomaniak.mail.data.models.mailbox.Mailbox
2327
import com.infomaniak.mail.data.models.message.Message
@@ -26,31 +30,13 @@ import io.realm.kotlin.MutableRealm
2630
import io.realm.kotlin.TypedRealm
2731
import kotlinx.coroutines.CoroutineScope
2832

29-
val defaultRefreshStrategy = object : DefaultRefreshStrategy {}
30-
31-
val inboxRefreshStrategy = object : DefaultRefreshStrategy {
32-
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
33-
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = false, realm = realm)
34-
}
35-
36-
override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.SNOOZED)
37-
38-
override fun addFolderToImpactedFolders(folderId: String, impactedFolders: ImpactedFolders) {
39-
impactedFolders += folderId
40-
impactedFolders += Folder.FolderRole.SNOOZED
41-
}
42-
}
43-
44-
val scheduledDraftRefreshStrategy = object : DefaultRefreshStrategy {
45-
override fun shouldHideEmptyFolder(): Boolean = true
46-
}
47-
4833
val snoozeRefreshStrategy = object : DefaultRefreshStrategy {
4934
override fun queryFolderThreads(folderId: String, realm: TypedRealm): List<Thread> {
5035
return ThreadController.getInboxThreadsWithSnoozeFilter(withSnooze = true, realm = realm)
5136
}
5237

5338
override fun otherFolderRolesToQueryThreads(): List<Folder.FolderRole> = listOf(Folder.FolderRole.INBOX)
39+
5440
override fun shouldHideEmptyFolder(): Boolean = true
5541

5642
override fun getMessageFromShortUid(shortUid: String, folderId: String, realm: TypedRealm): Message? {
@@ -78,6 +64,7 @@ val snoozeRefreshStrategy = object : DefaultRefreshStrategy {
7864
}
7965

8066
override fun processDeletedThread(thread: Thread, realm: MutableRealm) = thread.recomputeThread()
67+
8168
override fun shouldQueryFolderThreadsOnDeletedUid(): Boolean = true
8269

8370
override fun handleAddedMessages(

app/src/main/java/com/infomaniak/mail/data/models/Folder.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import androidx.annotation.StringRes
2525
import com.infomaniak.lib.core.utils.Utils.enumValueOfOrNull
2626
import com.infomaniak.lib.core.utils.removeAccents
2727
import com.infomaniak.mail.R
28-
import com.infomaniak.mail.data.cache.mailboxContent.*
28+
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
29+
import com.infomaniak.mail.data.cache.mailboxContent.refreshStrategies.*
2930
import com.infomaniak.mail.data.models.message.Message
3031
import com.infomaniak.mail.data.models.thread.Thread
3132
import com.infomaniak.mail.utils.SentryDebug

0 commit comments

Comments
 (0)