Retry favicons when the network comes back.

This commit is contained in:
Brent Simmons committed 2026-11-03 10:21:42 -08:00
1 parent e8c713308f
commit 1972d25f20
3 files changed
+28 -9

No files matched your search

@@ -30,6 +30,7 @@ extension Notification.Name {
private var searches = [String: FaviconSearch]() // homePageURL: search through its favicon URLs
private var homePagesAwaitingMetadataForSearch = Set<String>()
private var homePagesCheckedForNewFavicon = Set<String>()
private var homePagesWithTransientSearchFailure = Set<String>()
private var cache = [Feed: IconImage]()
@@ -43,6 +44,7 @@ extension Notification.Name {
NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleAppDidGoToBackground(_:)), name: .appDidGoToBackground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleDownloadedImageDidChange(_:)), name: .downloadedImageDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleNetworkStatusDidChange(_:)), name: .networkStatusDidChange, object: nil)
}
// MARK: - API
@@ -190,6 +192,24 @@ extension Notification.Name {
singleFaviconDownloaderCache[faviconURL] = SingleFaviconDownloader(faviconURL: faviconURL, homePageURL: downloader.homePageURL)
}
/// Waits for transient failures may have just ended, so those favicons and searches are tried again.
/// Runs after `DownloadedImageCache` ends those waits, which it does on this notification too.
@objc func handleNetworkStatusDidChange(_ notification: Notification) {
guard NetworkMonitor.shared.isConnected else {
return
}
Task { @MainActor in
for downloader in singleFaviconDownloaderCache.values where downloader.lastFailureWasTransient {
downloader.downloadFaviconIfNeeded()
}
let homePageURLs = homePagesWithTransientSearchFailure
homePagesWithTransientSearchFailure.removeAll()
for homePageURL in homePageURLs {
searchForFavicon(homePageURL: homePageURL)
}
}
}
@objc func htmlMetadataIsAvailable(_ note: Notification) {
guard let url = note.userInfo?[HTMLMetadataUserInfoKey.url] as? String else {
@@ -348,7 +368,9 @@ private extension FaviconDownloader {
guard let search = searches.removeValue(forKey: homePageURL) else {
return
}
if !search.hadTransientFailure && !search.keepsSavedFavicon {
if search.hadTransientFailure {
homePagesWithTransientSearchFailure.insert(homePageURL)
} else if !search.keepsSavedFavicon {
ImageDatabase.shared.saveHomePageFavicon(homePageURL: homePageURL, faviconURL: nil)
}
}
@@ -137,6 +137,9 @@ extension Notification.Name {
/// Waits for transient failures may have just ended, so waiting icons are requested again.
/// Runs after `DownloadedImageCache` ends those waits, which it does on this notification too.
@objc func handleNetworkStatusDidChange(_ notification: Notification) {
guard NetworkMonitor.shared.isConnected else {
return
}
Task { @MainActor in
for (url, feeds) in waitingForFeedURLs where !DownloadedImageCache.shared.isWaitingToRetry(url) {
_ = imageData(for: url, feed: feeds.first)
@@ -27,29 +27,23 @@ extension Notification.Name {
/// True until `.DidLoadFavicon` is posted for the current attempt.
private(set) var isFinding = true
private static let retryInterval: TimeInterval = 30 * 60 // 30 minutes
private var lastDownloadAttemptDate: Date
public init(faviconURL: String, homePageURL: String?) {
self.faviconURL = faviconURL
self.homePageURL = homePageURL
self.lastDownloadAttemptDate = Date()
Task { @MainActor in
await findFavicon()
}
}
/// Tries again when there’s no image and the last attempt was a while ago.
/// Tries again when there’s no image, once `DownloadedImageCache`’s wait after a failure has ended.
public func downloadFaviconIfNeeded() {
guard iconImage == nil, !isFinding else {
return
}
if Date().timeIntervalSince(lastDownloadAttemptDate) < Self.retryInterval {
if DownloadedImageCache.shared.isWaitingToRetry(faviconURL) || ImageDatabase.shared.recentlyFailed(url: faviconURL) {
return
}
lastDownloadAttemptDate = Date()
isFinding = true
Task { @MainActor in