Skip to content

Commit 501855f

Browse files
feat: Keep attachment in cache (#1740)
2 parents fa45a9f + 3786ce6 commit 501855f

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
let pipeline: ImagePipeline
24+
25+
func getCache(resource: String?) -> Data? {
26+
guard let resource, let resourceURL = URL(string: resource) else { return nil }
27+
let request = ImageRequest(url: resourceURL)
28+
return pipeline.cache.cachedData(for: request)
29+
}
30+
31+
func storeCache(resource: String?, data: Data) {
32+
guard let resource, let resourceURL = URL(string: resource) else { return }
33+
let request = ImageRequest(url: resourceURL)
34+
pipeline.cache.storeCachedData(data, for: request)
35+
}
36+
}

MailCore/Cache/MailboxManager/MailboxManager+Message.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,23 @@ public extension MailboxManager {
4949
throw CancellationError()
5050
}
5151

52-
let data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
52+
@InjectService var cacheHelper: AttachmentCacheHelper
53+
if let cachedData = cacheHelper.getCache(resource: attachment.resource) {
54+
return cachedData
55+
} else {
56+
let data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
57+
cacheHelper.storeCache(resource: attachment.resource, data: data)
5358

54-
let safeAttachment = ThreadSafeReference(to: attachment)
55-
try? writeTransaction { writableRealm in
56-
guard let liveAttachment = writableRealm.resolve(safeAttachment) else {
57-
return
58-
}
59+
let safeAttachment = ThreadSafeReference(to: attachment)
60+
try? writeTransaction { writableRealm in
61+
guard let liveAttachment = writableRealm.resolve(safeAttachment) else {
62+
return
63+
}
5964

60-
liveAttachment.saved = true
65+
liveAttachment.saved = true
66+
}
67+
return data
6168
}
62-
63-
return data
6469
}
6570

6671
func saveAttachmentLocally(attachment: Attachment, progressObserver: ((Double) -> Void)?) async {

MailCore/Utils/TargetAssembly.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import InfomaniakDI
2424
import InfomaniakLogin
2525
import InfomaniakNotifications
2626
import MyKSuite
27+
import Nuke
2728
import OSLog
2829

2930
private let realmRootPath = "mailboxes"
@@ -126,6 +127,9 @@ open class TargetAssembly {
126127
},
127128
Factory(type: MyKSuiteStore.self) { _, _ in
128129
MyKSuiteStore()
130+
},
131+
Factory(type: AttachmentCacheHelper.self) { _, _ in
132+
AttachmentCacheHelper(pipeline: ImagePipeline(configuration: .withDataCache))
129133
}
130134
]
131135
}

0 commit comments

Comments
 (0)