diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/DelayApiCallManager.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/DelayApiCallManager.kt new file mode 100644 index 0000000000..0a6bb9236e --- /dev/null +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/DelayApiCallManager.kt @@ -0,0 +1,65 @@ +/* + * Infomaniak Mail - Android + * Copyright (C) 2024 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 . + */ +package com.infomaniak.mail.data.cache.mailboxContent + +import com.infomaniak.lib.core.models.ApiResponse +import com.infomaniak.mail.data.api.ApiRepository +import com.infomaniak.mail.data.models.getMessages.GetMessagesByUidsResult +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.ensureActive +import okhttp3.OkHttpClient +import javax.inject.Inject +import javax.inject.Singleton +import kotlin.math.abs + +@Singleton +class DelayApiCallManager @Inject constructor() { + + private var previousTime = 0L + + /** + * Realm really doesn't like to be written on too frequently. + * So we want to be sure that we don't write twice in less than 500 ms. + * Appreciable side effect: it will also reduce the stress on the API. + */ + suspend fun getMessagesByUids( + scope: CoroutineScope, + mailboxUuid: String, + folderId: String, + uids: List, + okHttpClient: OkHttpClient?, + ): ApiResponse { + + val duration = abs(System.currentTimeMillis() - previousTime) + val delay = MAX_DELAY_BETWEEN_API_CALLS - duration + + if (delay > 0L) { + delay(delay) + scope.ensureActive() + } + + previousTime = System.currentTimeMillis() + + return ApiRepository.getMessagesByUids(mailboxUuid, folderId, uids, okHttpClient) + } + + companion object { + private const val MAX_DELAY_BETWEEN_API_CALLS = 500L + } +} diff --git a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt index 9b83f5c896..397f98d14d 100644 --- a/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt +++ b/app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/RefreshController.kt @@ -58,6 +58,7 @@ import kotlin.math.max class RefreshController @Inject constructor( private val localSettings: LocalSettings, private val mailboxController: MailboxController, + private val delayApiCallManager: DelayApiCallManager, ) { private var refreshThreadsJob: Job? = null @@ -525,9 +526,7 @@ class RefreshController @Inject constructor( val impactedThreads = mutableSetOf() - val before = System.currentTimeMillis() - val apiResponse = ApiRepository.getMessagesByUids(mailbox.uuid, folder.id, uids, okHttpClient) - val after = System.currentTimeMillis() + val apiResponse = delayApiCallManager.getMessagesByUids(scope, mailbox.uuid, folder.id, uids, okHttpClient) if (!apiResponse.isSuccess()) apiResponse.throwErrorAsException() scope.ensureActive() @@ -552,18 +551,6 @@ class RefreshController @Inject constructor( impactedThreads += allImpactedThreads.filter { it.folderId == folder.id } } } - - /** - * Realm really doesn't like to be written on too frequently. - * So we want to be sure that we don't write twice in less than 500 ms. - * Appreciable side effect: it will also reduce the stress on the API. - */ - val delay = Utils.MAX_DELAY_BETWEEN_API_CALLS - (after - before) - if (delay > 0L) { - delay(delay) - scope.ensureActive() - } - } return impactedThreads diff --git a/app/src/main/java/com/infomaniak/mail/utils/Utils.kt b/app/src/main/java/com/infomaniak/mail/utils/Utils.kt index 3c99c75d62..d010ffbf32 100644 --- a/app/src/main/java/com/infomaniak/mail/utils/Utils.kt +++ b/app/src/main/java/com/infomaniak/mail/utils/Utils.kt @@ -38,7 +38,6 @@ object Utils { const val NUMBER_OF_OLD_MESSAGES_TO_FETCH = 500 /** Beware: the API refuses a PAGE_SIZE bigger than 200. */ const val PAGE_SIZE: Int = 50 - const val MAX_DELAY_BETWEEN_API_CALLS = 500L const val DELAY_BEFORE_FETCHING_ACTIVITIES_AGAIN = 500L const val TAG_SEPARATOR = " "