Skip to content

Commit 3786ce6

Browse files
committed
fix: Inject AttachmentCacheHelper
1 parent f5ccaf9 commit 3786ce6

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

Mail/MailApp.swift

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

1919
import InfomaniakDI
2020
import MailCore
21-
import Nuke
2221
import OSLog
2322
import SwiftUI
2423
import UIKit
@@ -36,7 +35,6 @@ struct MailApp: App {
3635
Logging.resetAppForUITestsIfNeeded()
3736
Logger.general.info("Application starting in foreground ? \(UIApplication.shared.applicationState != .background)")
3837
refreshAppBackgroundTask.register()
39-
ImagePipeline.shared = ImagePipeline(configuration: .withDataCache)
4038
}
4139

4240
var body: some Scene {

MailCore/Cache/Attachments/AttachmentCacheHelper.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ import Foundation
2020
import Nuke
2121

2222
struct AttachmentCacheHelper {
23+
let pipeline: ImagePipeline
24+
2325
func getCache(resource: String?) -> Data? {
2426
guard let resource, let resourceURL = URL(string: resource) else { return nil }
2527
let request = ImageRequest(url: resourceURL)
26-
return ImagePipeline.shared.cache.cachedData(for: request)
28+
return pipeline.cache.cachedData(for: request)
2729
}
2830

2931
func storeCache(resource: String?, data: Data) {
3032
guard let resource, let resourceURL = URL(string: resource) else { return }
3133
let request = ImageRequest(url: resourceURL)
32-
ImagePipeline.shared.cache.storeCachedData(data, for: request)
34+
pipeline.cache.storeCachedData(data, for: request)
3335
}
3436
}

MailCore/Cache/MailboxManager/MailboxManager+Message.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ public extension MailboxManager {
4949
throw CancellationError()
5050
}
5151

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

6059
let safeAttachment = ThreadSafeReference(to: attachment)
6160
try? writeTransaction { writableRealm in
@@ -65,9 +64,8 @@ public extension MailboxManager {
6564

6665
liveAttachment.saved = true
6766
}
67+
return data
6868
}
69-
70-
return data
7169
}
7270

7371
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)