Note when image was used so icons in use aren’t the first removed.

This commit is contained in:
Brent Simmons committed 2026-10-29 10:58:43 -07:00
1 parent 736ba28c8e
commit af2d55a194
3 files changed
+21 -18

No files matched your search

@@ -95,6 +95,7 @@ extension Notification.Name {
/// `.articleThumbnailDidBecomeAvailable` once the image is ready.
public func thumbnail(url: String, mode: Mode, articleTitle: String?) -> RSImage? {
if let image = imageCache.object(forKey: Self.cacheKey(url, mode)) {
DownloadedImageCache.shared.imageWasUsed(url, checksForUpdates: false)
return image
}
findImage(url, mode: mode, downloadPriority: .visible, activityDetail: articleTitle)
@@ -126,6 +126,24 @@ extension Notification.Name {
downloadLimiter.prioritize(key: url)
}
/// At most once a day per image: saves the access date, which orders images for removal,
/// and, when `checksForUpdates`, checks with the server if the copy is due.
/// Callers that keep images in memory call this on memory hits too, so images in use aren’t removed first.
func imageWasUsed(_ url: String, checksForUpdates: Bool) {
let now = Date()
if let readDate = readDatesNoted[url], now.timeIntervalSince(readDate) < Self.readNoteInterval {
return
}
readDatesNoted[url] = now
ImageDatabase.shared.saveDownloadedImageAccessDate(url: url)
guard checksForUpdates else {
return
}
Task {
await revalidateIfDue(url)
}
}
/// Failures while offline say nothing about the servers, so retries can start right away.
@objc func handleNetworkStatusDidChange(_ notification: Notification) {
retryDelays.removeAll()
@@ -137,7 +155,7 @@ private extension DownloadedImageCache {
func findImage(_ url: String, downloadPriority: DownloadPriority?, checksForUpdates: Bool, activityOwner: ActivityOwner?, activityKind: ActivityKind?, activityDetail: String?) async -> Result<Data, ImageDownloadError> {
if let data = await Self.readFromDisk(diskCache, key: Self.diskKey(url)) {
imageWasRead(url, checksForUpdates: checksForUpdates)
imageWasUsed(url, checksForUpdates: checksForUpdates)
return .success(data)
}
@@ -292,23 +310,6 @@ private extension DownloadedImageCache {
return true
}
/// At most once a day per image: saves the access date, which orders images for removal,
/// and, when `checksForUpdates`, checks with the server if the copy is due.
func imageWasRead(_ url: String, checksForUpdates: Bool) {
let now = Date()
if let readDate = readDatesNoted[url], now.timeIntervalSince(readDate) < Self.readNoteInterval {
return
}
readDatesNoted[url] = now
ImageDatabase.shared.saveDownloadedImageAccessDate(url: url)
guard checksForUpdates else {
return
}
Task {
await revalidateIfDue(url)
}
}
// MARK: - Revalidating Images
/// Doesn’t use the download limiter, since icons aren’t limited either.
@@ -102,6 +102,7 @@ extension ImageDownloadError: LocalizedError {
return nil
}
if let data = imageCache.object(forKey: url as NSString) {
DownloadedImageCache.shared.imageWasUsed(url, checksForUpdates: true)
return data as Data
}
if ImageDatabase.shared.recentlyFailed(url: url) {