Skip to content

Commit

Permalink
refactor: Avoid duplicating values when defining what folders need to…
Browse files Browse the repository at this point in the history
… refresh together
  • Loading branch information
LunarX committed Feb 11, 2025
1 parent 890bf16 commit 1507fb3
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2023-2024 Infomaniak Network SA
* Copyright (C) 2023-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
Expand Down Expand Up @@ -188,27 +188,32 @@ class RefreshController @Inject constructor(
}
}

private val FOLDER_ROLES_TO_REFRESH_TOGETHER = setOf(
FolderRole.INBOX,
FolderRole.SENT,
FolderRole.DRAFT,
FolderRole.SCHEDULED_DRAFTS,
)

private suspend fun Realm.refreshWithRoleConsideration(scope: CoroutineScope): Set<Thread> {

val impactedThreads = refresh(scope, initialFolder)
onStop?.invoke()
clearCallbacks()

when (initialFolder.role) {
FolderRole.INBOX -> listOf(FolderRole.SENT, FolderRole.DRAFT, FolderRole.SCHEDULED_DRAFTS)
FolderRole.SENT -> listOf(FolderRole.INBOX, FolderRole.DRAFT, FolderRole.SCHEDULED_DRAFTS)
FolderRole.DRAFT -> listOf(FolderRole.INBOX, FolderRole.SENT, FolderRole.SCHEDULED_DRAFTS)
FolderRole.SCHEDULED_DRAFTS -> listOf(FolderRole.INBOX, FolderRole.SENT, FolderRole.DRAFT)
else -> emptyList()
}.forEach { role ->
scope.ensureActive()
if (initialFolder.role in FOLDER_ROLES_TO_REFRESH_TOGETHER) {
for (role in FOLDER_ROLES_TO_REFRESH_TOGETHER) {
scope.ensureActive()

if (initialFolder.role == role) continue

runCatching {
FolderController.getFolder(role, realm = this)?.let {
refresh(scope, folder = it)
runCatching {
FolderController.getFolder(role, realm = this)?.let {
refresh(scope, folder = it)
}
}.onFailure {
throw ReturnThreadsException(impactedThreads, exception = it)
}
}.onFailure {
throw ReturnThreadsException(impactedThreads, exception = it)
}
}

Expand Down

0 comments on commit 1507fb3

Please sign in to comment.