diff --git a/Modules/Images/Sources/Images/FaviconDownloader.swift b/Modules/Images/Sources/Images/FaviconDownloader.swift index c9d0b2c59..86404c7c9 100644 --- a/Modules/Images/Sources/Images/FaviconDownloader.swift +++ b/Modules/Images/Sources/Images/FaviconDownloader.swift @@ -30,6 +30,7 @@ extension Notification.Name { private var searches = [String: FaviconSearch]() // homePageURL: search through its favicon URLs private var homePagesAwaitingMetadataForSearch = Set() private var homePagesCheckedForNewFavicon = Set() + private var homePagesWithTransientSearchFailure = Set() 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) } } diff --git a/Modules/Images/Sources/Images/FeedIconDownloader.swift b/Modules/Images/Sources/Images/FeedIconDownloader.swift index 7cca318c9..d02bdc961 100644 --- a/Modules/Images/Sources/Images/FeedIconDownloader.swift +++ b/Modules/Images/Sources/Images/FeedIconDownloader.swift @@ -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) diff --git a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift index 5242fd306..261792941 100644 --- a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift +++ b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift @@ -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