Skip to content

feat: Keep attachment in cache #1740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions MailCore/Cache/Attachments/AttachmentCacheHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 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
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 <http://www.gnu.org/licenses/>.
*/

import Foundation
import Nuke

struct AttachmentCacheHelper {
let pipeline: ImagePipeline

func getCache(resource: String?) -> Data? {
guard let resource, let resourceURL = URL(string: resource) else { return nil }
let request = ImageRequest(url: resourceURL)
return pipeline.cache.cachedData(for: request)
}

func storeCache(resource: String?, data: Data) {
guard let resource, let resourceURL = URL(string: resource) else { return }
let request = ImageRequest(url: resourceURL)
pipeline.cache.storeCachedData(data, for: request)
}
}
23 changes: 14 additions & 9 deletions MailCore/Cache/MailboxManager/MailboxManager+Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,23 @@ public extension MailboxManager {
throw CancellationError()
}

let data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
@InjectService var cacheHelper: AttachmentCacheHelper
if let cachedData = cacheHelper.getCache(resource: attachment.resource) {
return cachedData
} else {
let data = try await apiFetcher.attachment(attachment: attachment, progressObserver: progressObserver)
cacheHelper.storeCache(resource: attachment.resource, data: data)

let safeAttachment = ThreadSafeReference(to: attachment)
try? writeTransaction { writableRealm in
guard let liveAttachment = writableRealm.resolve(safeAttachment) else {
return
}
let safeAttachment = ThreadSafeReference(to: attachment)
try? writeTransaction { writableRealm in
guard let liveAttachment = writableRealm.resolve(safeAttachment) else {
return
}

liveAttachment.saved = true
liveAttachment.saved = true
}
return data
}

return data
}

func saveAttachmentLocally(attachment: Attachment, progressObserver: ((Double) -> Void)?) async {
Expand Down
4 changes: 4 additions & 0 deletions MailCore/Utils/TargetAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import InfomaniakDI
import InfomaniakLogin
import InfomaniakNotifications
import MyKSuite
import Nuke
import OSLog

private let realmRootPath = "mailboxes"
Expand Down Expand Up @@ -126,6 +127,9 @@ open class TargetAssembly {
},
Factory(type: MyKSuiteStore.self) { _, _ in
MyKSuiteStore()
},
Factory(type: AttachmentCacheHelper.self) { _, _ in
AttachmentCacheHelper(pipeline: ImagePipeline(configuration: .withDataCache))
}
]
}
Expand Down
Loading