Skip to content

Commit 8478e5f

Browse files
committed
feat: Keep attachment in cache
1 parent 0308a34 commit 8478e5f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Mail/MailApp.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import InfomaniakDI
2020
import MailCore
21+
import Nuke
2122
import OSLog
2223
import SwiftUI
2324
import UIKit
@@ -35,6 +36,7 @@ struct MailApp: App {
3536
Logging.resetAppForUITestsIfNeeded()
3637
Logger.general.info("Application starting in foreground ? \(UIApplication.shared.applicationState != .background)")
3738
refreshAppBackgroundTask.register()
39+
ImagePipeline.shared = ImagePipeline(configuration: .withDataCache)
3840
}
3941

4042
var body: some Scene {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Infomaniak Mail - iOS App
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+
19+
import Foundation
20+
import Nuke
21+
22+
struct AttachmentCacheHelper {
23+
func getCache(resource: String?) -> Data? {
24+
guard let resource, let resourceURL = URL(string: resource) else { return nil }
25+
let request = ImageRequest(url: resourceURL)
26+
return ImagePipeline.shared.cache.cachedData(for: request)
27+
}
28+
29+
func storeCache(resource: String?, data: Data) {
30+
guard let resource, let resourceURL = URL(string: resource) else { return }
31+
let request = ImageRequest(url: resourceURL)
32+
ImagePipeline.shared.cache.storeCachedData(data, for: request)
33+
}
34+
}

MailCore/Cache/MailboxManager/MailboxManager+Message.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ public extension MailboxManager {
4949
throw CancellationError()
5050
}
5151

52-
let data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
52+
let attachmentCacheHelper = AttachmentCacheHelper()
53+
let data: Data
54+
if let cachedData = attachmentCacheHelper.getCache(resource: attachment.resource) {
55+
data = cachedData
56+
} else {
57+
data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
58+
attachmentCacheHelper.storeCache(resource: attachment.resource, data: data)
59+
}
5360

5461
let safeAttachment = ThreadSafeReference(to: attachment)
5562
try? writeTransaction { writableRealm in

0 commit comments

Comments
 (0)