diff --git a/Shared/Images/AuthorAvatarDownloader.swift b/Shared/Images/AuthorAvatarDownloader.swift index d5c6fe528..db637e31a 100644 --- a/Shared/Images/AuthorAvatarDownloader.swift +++ b/Shared/Images/AuthorAvatarDownloader.swift @@ -46,19 +46,13 @@ extension Notification.Name { return cachedImage } - if let imageData = imageDownloader.image(for: avatarURL) { + // `ImageDownloader.image(for:)` handles the activity log itself — it + // fires the activity only if the fetch goes to network. Disk-cached + // fetches stay silent. + let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL) + if let imageData = imageDownloader.image(for: avatarURL, activityOwner: .avatarDownloader, activityKind: kind) { scaleAndCacheImageData(imageData, avatarURL) } else { - // Multiple authors can share the same avatar URL. Only produce the - // activity once per URL — the imageDidBecomeAvailable notification - // will complete the single in-flight activity. - if !waitingForAvatarURLs.contains(avatarURL) { - let activityLog = ActivityLog.shared - let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL) - activityLog.createActivity(owner: .avatarDownloader, kind: kind) - activityLog.didStart(.avatarDownloader, kind: kind) - } - waitingForAvatarURLs.insert(avatarURL) } @@ -85,9 +79,6 @@ extension Notification.Name { return } scaleAndCacheImageData(imageData, avatarURL) - - let kind = ActivityKind.downloadAvatar(avatarURL: avatarURL) - ActivityLog.shared.didComplete(.avatarDownloader, kind: kind) } } diff --git a/Shared/Images/FeedIconDownloader.swift b/Shared/Images/FeedIconDownloader.swift index 1eeb03936..d9563f0f6 100644 --- a/Shared/Images/FeedIconDownloader.swift +++ b/Shared/Images/FeedIconDownloader.swift @@ -140,10 +140,6 @@ extension Notification.Name { return } waitingForFeedURLs[url] = nil - - let kind = ActivityKind.downloadFeedImage(feedURL: url) - ActivityLog.shared.didComplete(.feedImageDownloader, kind: kind) - _ = icon(for: feed) } @@ -222,24 +218,16 @@ private extension FeedIconDownloader { let url = Self.sanitizedIconURL(url) - // If the ImageDownloader already has it in memory, process inline and - // skip activity logging — no fetch is happening. - if let imageData = imageDownloader.image(for: url) { + // `ImageDownloader.image(for:)` handles the activity log itself — it + // fires the activity only if the fetch goes to network. Disk-cached + // fetches stay silent. + let kind = ActivityKind.downloadFeedImage(feedURL: url) + if let imageData = imageDownloader.image(for: url, activityOwner: .feedImageDownloader, activityKind: kind, activityDetail: feed.nameForDisplay) { RSImage.image(with: imageData, imageResultBlock: imageResultBlock) return } - // Data not in memory — `imageDownloader.image(for:)` has dispatched an - // async findImage (disk or network). Completion arrives via - // imageDidBecomeAvailable. Multiple feeds can share the same icon URL; - // only produce one activity per URL. - if waitingForFeedURLs[url] == nil { - let kind = ActivityKind.downloadFeedImage(feedURL: url) - let activityLog = ActivityLog.shared - activityLog.createActivity(owner: .feedImageDownloader, kind: kind, detail: feed.nameForDisplay) - activityLog.didStart(.feedImageDownloader, kind: kind) - } - + // Async fetch dispatched — completion arrives via imageDidBecomeAvailable. waitingForFeedURLs[url] = feed imageResultBlock(nil) } diff --git a/Shared/Images/ImageDownloader.swift b/Shared/Images/ImageDownloader.swift index ad314e266..66593d7f7 100644 --- a/Shared/Images/ImageDownloader.swift +++ b/Shared/Images/ImageDownloader.swift @@ -10,6 +10,7 @@ import Foundation import os import RSCore import RSWeb +import ActivityLog extension Notification.Name { static let imageDidBecomeAvailable = Notification.Name("ImageDidBecomeAvailableNotification") // UserInfoKey.url @@ -43,15 +44,22 @@ extension Notification.Name { imageCache.removeAll() } + /// Returns the image data if it's already in the in-memory cache. Otherwise + /// dispatches an async fetch — disk first, then network — and returns nil; + /// completion arrives via `.imageDidBecomeAvailable`. + /// + /// Pass `activityOwner` / `activityKind` (and optionally `activityDetail`) + /// to have an entry written to the activity log if and only if the fetch + /// goes to network. Disk-only fetches stay silent. @discardableResult - func image(for url: String) -> Data? { + func image(for url: String, activityOwner: ActivityOwner? = nil, activityKind: ActivityKind? = nil, activityDetail: String? = nil) -> Data? { assert(Thread.isMainThread) if let data = imageCache[url] { return data } Task { @MainActor in - await findImage(url) + await findImage(url, activityOwner: activityOwner, activityKind: activityKind, activityDetail: activityDetail) } return nil @@ -66,7 +74,7 @@ private extension ImageDownloader { postImageDidBecomeAvailableNotification(url) } - func findImage(_ url: String) async { + func findImage(_ url: String, activityOwner: ActivityOwner?, activityKind: ActivityKind?, activityDetail: String?) async { guard !urlsInProgress.contains(url) && !badURLs.contains(url) else { return } @@ -78,10 +86,27 @@ private extension ImageDownloader { return } - if let image = await downloadImage(url) { - cacheImage(url, image) - urlsInProgress.remove(url) + // Disk missed — going to network. Produce activity if the caller asked. + let activityLog = ActivityLog.shared + if let activityOwner, let activityKind { + activityLog.createActivity(owner: activityOwner, kind: activityKind, detail: activityDetail) + activityLog.didStart(activityOwner, kind: activityKind) } + + let image = await downloadImage(url) + if let activityOwner, let activityKind { + if image != nil { + activityLog.didComplete(activityOwner, kind: activityKind) + } else { + let error = NSError(domain: "ImageDownloader", code: 0, userInfo: [NSLocalizedDescriptionKey: "Download failed"]) + activityLog.didFail(activityOwner, kind: activityKind, error: error) + } + } + + if let image { + cacheImage(url, image) + } + urlsInProgress.remove(url) } func readFromDisk(url: String) async -> Data? {