diff --git a/Modules/Images/Sources/Images/FaviconDownloader.swift b/Modules/Images/Sources/Images/FaviconDownloader.swift index ddda46e2a..2236300d1 100644 --- a/Modules/Images/Sources/Images/FaviconDownloader.swift +++ b/Modules/Images/Sources/Images/FaviconDownloader.swift @@ -34,7 +34,9 @@ extension Notification.Name { private var homePagesAwaitingMetadataForSearch = Set() private var homePagesCheckedForNewFavicon = Set() private var homePagesWithTransientSearchFailure = Set() - private var feeds = Set() // Every feed that looked up its favicon, so its favicon’s arrival is posted + // Feeds that looked up their favicon, so its arrival is posted to them + private var feedsByDeclaredFaviconURL = [String: Set]() // favicon URL the feed specifies: feeds + private var feedsByHomePageURL = [String: Set]() // home page whose favicon they use: feeds private var faviconURLs = Set() // Every favicon URL looked up private var faviconURLsThatAreNotImages = Set() @@ -53,13 +55,14 @@ extension Notification.Name { if shouldSkipDownloadingFavicon(feed: feed) { return .unavailable } - feeds.insert(feed) if let faviconURL = usableDeclaredFaviconURL(for: feed) { + feedsByDeclaredFaviconURL[faviconURL, default: Set()].insert(feed) return iconImage(for: faviconURL, homePageURL: feed.homePageURL) } guard let homePageURL = faviconHomePageURL(for: feed) else { return .unavailable } + feedsByHomePageURL[homePageURL, default: Set()].insert(feed) return favicon(forHomePageURL: homePageURL) } @@ -123,7 +126,7 @@ extension Notification.Name { guard let feed = notification.object as? Feed, let key = notification.userInfo?[Feed.SettingUserInfoKey] as? Feed.SettingKey else { return } - guard key == .faviconURL || key == .homePageURL, feeds.contains(feed) else { + guard key == .faviconURL || key == .homePageURL else { return } postFaviconDidChange(Set([feed])) @@ -325,21 +328,11 @@ private extension FaviconDownloader { ImageDatabase.shared.saveHomePageFavicon(homePageURL: homePageURL, faviconURL: faviconURL) } - /// The known favicon URL for `feed` (from feed settings or the home-page→favicon map), without - /// triggering any download. Both lookups are in-memory, so this is cheap to call per feed. - /// The home page is the same one `favicon(for:)` saves under. - func cachedFaviconURL(for feed: Feed) -> String? { - if let faviconURL = usableDeclaredFaviconURL(for: feed) { - return faviconURL - } - if let homePageURL = faviconHomePageURL(for: feed), let faviconURL = ImageDatabase.shared.faviconURL(forHomePageURL: homePageURL) { - return faviconURL - } - return nil - } - func postFaviconDidChange(_ faviconURL: String) { - let feedsUsingFavicon = feeds.filter { cachedFaviconURL(for: $0) == faviconURL } + var feedsUsingFavicon = feedsByDeclaredFaviconURL[faviconURL] ?? Set() + for (homePageURL, feeds) in feedsByHomePageURL where ImageDatabase.shared.faviconURL(forHomePageURL: homePageURL) == faviconURL { + feedsUsingFavicon.formUnion(feeds) + } guard !feedsUsingFavicon.isEmpty else { return } diff --git a/Modules/Images/Sources/Images/HomePageIconDownloader.swift b/Modules/Images/Sources/Images/HomePageIconDownloader.swift index ed8946ff0..2430677be 100644 --- a/Modules/Images/Sources/Images/HomePageIconDownloader.swift +++ b/Modules/Images/Sources/Images/HomePageIconDownloader.swift @@ -31,6 +31,7 @@ extension Notification.Name { private var feedsByHomePageURL = [String: Set]() // home page URL: feeds that looked it up private var homePagesCheckedForNewIconURL = Set() private var newIconURLs = [String: String]() // home page URL: icon URL that replaces the saved one once its image loads + private var iconURLs = Set() // Every icon URL looked up, so other kinds of icons are ignored when they arrive init() { NotificationCenter.default.addObserver(self, selector: #selector(handleIconImageDidBecomeAvailable(_:)), name: .iconImageDidBecomeAvailable, object: IconImageDownloader.shared) @@ -65,7 +66,7 @@ extension Notification.Name { // MARK: - Notifications @objc func handleIconImageDidBecomeAvailable(_ notification: Notification) { - guard let iconURL = notification.userInfo?[IconImageDownloader.urlKey] as? String else { + guard let iconURL = notification.userInfo?[IconImageDownloader.urlKey] as? String, iconURLs.contains(iconURL) else { return } for (homePageURL, newIconURL) in newIconURLs where newIconURL == iconURL { @@ -77,7 +78,7 @@ extension Notification.Name { /// Feeds showing this icon look again, which falls back to the next source. @objc func handleIconImageDownloadDidFail(_ notification: Notification) { - guard let iconURL = notification.userInfo?[IconImageDownloader.urlKey] as? String else { + guard let iconURL = notification.userInfo?[IconImageDownloader.urlKey] as? String, iconURLs.contains(iconURL) else { return } newIconURLs = newIconURLs.filter { $0.value != iconURL } @@ -148,7 +149,8 @@ private extension HomePageIconDownloader { } func iconImage(for iconURL: String, homePageURL: String) -> IconLookup { - IconImageDownloader.shared.iconImage(for: iconURL, activityOwner: .feedImageDownloader, activityKind: .downloadFeedImage(feedURL: iconURL), activityDetail: homePageURL) + iconURLs.insert(iconURL) + return IconImageDownloader.shared.iconImage(for: iconURL, activityOwner: .feedImageDownloader, activityKind: .downloadFeedImage(feedURL: iconURL), activityDetail: homePageURL) } func postHomePageIconDidChange(iconURL: String) {