2021-05-08 12:42:44 -07:00
|
|
|
|
//
|
|
|
|
|
|
// IconImageCache.swift
|
|
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Brent Simmons on 5/2/21.
|
|
|
|
|
|
// Copyright © 2021 Ranchero Software. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
import Account
|
|
|
|
|
|
import Articles
|
2026-03-07 17:13:37 -08:00
|
|
|
|
import RSCore
|
2026-05-29 10:46:00 -07:00
|
|
|
|
import Images
|
2021-05-08 12:42:44 -07:00
|
|
|
|
|
2026-11-03 11:30:26 -08:00
|
|
|
|
extension Notification.Name {
|
2026-11-03 11:53:31 -08:00
|
|
|
|
/// Posted when the icon `iconImage(for:)` returns for a feed may have changed. userInfo key: `UserInfoKey.feed`.
|
|
|
|
|
|
static let feedIconImageDidChange = Notification.Name("FeedIconImageDidChange")
|
|
|
|
|
|
/// Posted when an author’s avatar loads or changes. userInfo key: `UserInfoKey.url` (the avatar URL).
|
|
|
|
|
|
static let authorAvatarImageDidChange = Notification.Name("AuthorAvatarImageDidChange")
|
2026-11-03 11:30:26 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-03 11:53:31 -08:00
|
|
|
|
/// The icons the UI shows for feeds, articles, and sidebar items. The UI watches its two notifications, not the downloaders’.
|
2025-11-13 17:11:34 -08:00
|
|
|
|
@MainActor final class IconImageCache {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
|
|
|
|
|
|
static var shared = IconImageCache()
|
|
|
|
|
|
|
2025-11-02 11:48:14 -08:00
|
|
|
|
private var smartFeedIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2026-11-03 11:42:18 -08:00
|
|
|
|
private var lastFoundFeedIconImages = [SidebarItemIdentifier: IconImage]()
|
2025-11-02 11:48:14 -08:00
|
|
|
|
private var smallIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2021-05-08 12:42:44 -07:00
|
|
|
|
private var authorIconImageCache = [Author: IconImage]()
|
|
|
|
|
|
|
2026-03-07 17:13:37 -08:00
|
|
|
|
init() {
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil)
|
2026-11-03 11:30:26 -08:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleFeedIconDidChange(_:)), name: .feedIconDidChange, object: nil)
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleHomePageIconDidChange(_:)), name: .homePageIconDidChange, object: nil)
|
2026-11-03 11:53:31 -08:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleFaviconDidChange(_:)), name: .faviconDidChange, object: nil)
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(handleAvatarDidChange(_:)), name: .avatarDidChange, object: nil)
|
2026-03-07 17:13:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@objc func handleLowMemory(_ notification: Notification) {
|
|
|
|
|
|
emptyCache()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-03 11:30:26 -08:00
|
|
|
|
@objc func handleFeedIconDidChange(_ notification: Notification) {
|
2026-11-03 11:53:31 -08:00
|
|
|
|
postFeedIconImageDidChange(notification.userInfo?[FeedIconDownloader.feedsKey] as? Set<Feed>)
|
2026-11-03 11:30:26 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@objc func handleHomePageIconDidChange(_ notification: Notification) {
|
2026-11-03 11:53:31 -08:00
|
|
|
|
postFeedIconImageDidChange(notification.userInfo?[HomePageIconDownloader.feedsKey] as? Set<Feed>)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@objc func handleFaviconDidChange(_ notification: Notification) {
|
|
|
|
|
|
postFeedIconImageDidChange(notification.userInfo?[FaviconDownloader.feedsKey] as? Set<Feed>)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@objc func handleAvatarDidChange(_ notification: Notification) {
|
|
|
|
|
|
guard let avatarURL = notification.userInfo?[AuthorAvatarDownloader.avatarURLKey] as? String else {
|
2026-11-03 11:30:26 -08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-11-03 11:53:31 -08:00
|
|
|
|
NotificationCenter.default.post(name: .authorAvatarImageDidChange, object: self, userInfo: [UserInfoKey.url: avatarURL])
|
2026-11-03 11:30:26 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 11:48:14 -08:00
|
|
|
|
func imageFor(_ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
if let smartFeed = SmartFeedsController.shared.find(by: feedID) {
|
|
|
|
|
|
return imageForFeed(smartFeed)
|
|
|
|
|
|
}
|
|
|
|
|
|
if let feed = AccountManager.shared.existingFeed(with: feedID) {
|
|
|
|
|
|
return imageForFeed(feed)
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 12:17:44 -08:00
|
|
|
|
func imageForFeed(_ sidebarItem: SidebarItem) -> IconImage? {
|
2026-11-03 11:42:18 -08:00
|
|
|
|
if let feed = sidebarItem as? Feed {
|
|
|
|
|
|
return iconImage(for: feed)
|
|
|
|
|
|
}
|
2025-11-02 12:17:44 -08:00
|
|
|
|
guard let sidebarItemID = sidebarItem.sidebarItemID else {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-11-02 12:17:44 -08:00
|
|
|
|
if let smartFeed = sidebarItem as? PseudoFeed {
|
2025-11-02 11:48:14 -08:00
|
|
|
|
return imageForSmartFeed(smartFeed, sidebarItemID)
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
2025-11-02 12:17:44 -08:00
|
|
|
|
if let smallIconProvider = sidebarItem as? SmallIconProvider {
|
2025-11-02 11:48:14 -08:00
|
|
|
|
return imageForSmallIconProvider(smallIconProvider, sidebarItemID)
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-28 09:28:06 -07:00
|
|
|
|
func prefetchImagesForArticles(_ articles: ArticleArray) {
|
|
|
|
|
|
|
|
|
|
|
|
var feedsSeen = Set<SidebarItemIdentifier>()
|
|
|
|
|
|
var authorsSeen = Set<String>()
|
|
|
|
|
|
|
|
|
|
|
|
for article in articles {
|
|
|
|
|
|
if let authors = article.authors {
|
|
|
|
|
|
for author in authors {
|
|
|
|
|
|
if let avatarURL = author.avatarURL, !authorsSeen.contains(avatarURL) {
|
|
|
|
|
|
authorsSeen.insert(avatarURL)
|
2026-11-03 11:10:10 -08:00
|
|
|
|
_ = AuthorAvatarDownloader.shared.avatar(for: author)
|
2026-05-28 09:28:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let feed = article.feed, let feedID = feed.sidebarItemID, !feedsSeen.contains(feedID) {
|
|
|
|
|
|
feedsSeen.insert(feedID)
|
2026-11-03 11:42:18 -08:00
|
|
|
|
_ = feedIcon(for: feed)
|
2026-05-28 09:28:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-09 10:19:33 -07:00
|
|
|
|
func prefetchImagesForFeeds(_ feeds: [Feed]) {
|
|
|
|
|
|
for feed in feeds {
|
2026-11-03 11:42:18 -08:00
|
|
|
|
_ = feedIcon(for: feed)
|
2026-06-09 10:19:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-03 11:42:18 -08:00
|
|
|
|
/// Precedence: author avatar, then the feed’s icon.
|
2021-05-08 12:42:44 -07:00
|
|
|
|
func imageForArticle(_ article: Article) -> IconImage? {
|
|
|
|
|
|
if let iconImage = imageForAuthors(article.authors) {
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
2025-11-02 15:12:00 -08:00
|
|
|
|
guard let feed = article.feed else {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2026-11-03 11:42:18 -08:00
|
|
|
|
return iconImage(for: feed)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Precedence: feed icon, home page icon, favicon, generated favicon.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// While a source is loading, or after the downloaders empty their caches, the icon found last is shown,
|
|
|
|
|
|
/// so an icon doesn’t flash. Without one, the generated favicon is shown.
|
|
|
|
|
|
/// <https://github.com/Ranchero-Software/NetNewsWire/issues/5376>
|
|
|
|
|
|
func iconImage(for feed: Feed) -> IconImage {
|
|
|
|
|
|
if Self.isNetNewsWireBrandedFeed(feed) {
|
|
|
|
|
|
return IconImage.nnwFeedIcon
|
|
|
|
|
|
}
|
|
|
|
|
|
let feedID = feed.sidebarItemID
|
|
|
|
|
|
if let iconImage = feedIcon(for: feed).iconImage {
|
|
|
|
|
|
if let feedID {
|
|
|
|
|
|
lastFoundFeedIconImages[feedID] = iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
if let feedID, let iconImage = lastFoundFeedIconImages[feedID] {
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
return FaviconGenerator.shared.favicon(feed)
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func emptyCache() {
|
2026-03-07 17:13:37 -08:00
|
|
|
|
smartFeedIconImageCache.removeAll()
|
2026-11-03 11:42:18 -08:00
|
|
|
|
lastFoundFeedIconImages.removeAll()
|
2026-03-07 17:13:37 -08:00
|
|
|
|
smallIconImageCache.removeAll()
|
|
|
|
|
|
authorIconImageCache.removeAll()
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private extension IconImageCache {
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-05-29 10:46:00 -07:00
|
|
|
|
static func isNetNewsWireBrandedFeed(_ feed: Feed) -> Bool {
|
|
|
|
|
|
if let homePageURLString = feed.homePageURL, let homePageURL = URL(string: homePageURLString), let host = homePageURL.host {
|
|
|
|
|
|
if host == "nnw.ranchero.com" || host == "netnewswire.blog" || host.hasSuffix("netnewswire.com") {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return feed.url.hasPrefix("https://ranchero.com/downloads/netnewswire")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-03 11:42:18 -08:00
|
|
|
|
/// Precedence: feed icon, home page icon, favicon.
|
|
|
|
|
|
/// Stops at the first source that has an icon or is still loading, so later sources aren’t looked up.
|
|
|
|
|
|
func feedIcon(for feed: Feed) -> IconLookup {
|
2026-11-03 11:30:26 -08:00
|
|
|
|
let feedIcon = FeedIconDownloader.shared.feedIcon(for: feed)
|
|
|
|
|
|
guard case .unavailable = feedIcon else {
|
|
|
|
|
|
return feedIcon
|
|
|
|
|
|
}
|
2026-11-03 11:42:18 -08:00
|
|
|
|
let homePageIcon = HomePageIconDownloader.shared.homePageIcon(for: feed)
|
|
|
|
|
|
guard case .unavailable = homePageIcon else {
|
|
|
|
|
|
return homePageIcon
|
|
|
|
|
|
}
|
|
|
|
|
|
return FaviconDownloader.shared.favicon(for: feed)
|
2026-11-03 11:30:26 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-03 11:53:31 -08:00
|
|
|
|
func postFeedIconImageDidChange(_ feeds: Set<Feed>?) {
|
|
|
|
|
|
guard let feeds else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
for feed in feeds {
|
|
|
|
|
|
NotificationCenter.default.post(name: .feedIconImageDidChange, object: self, userInfo: [UserInfoKey.feed: feed])
|
|
|
|
|
|
}
|
2026-11-03 11:30:26 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 11:48:14 -08:00
|
|
|
|
func imageForSmartFeed(_ smartFeed: PseudoFeed, _ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
if let iconImage = smartFeedIconImageCache[feedID] {
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
if let iconImage = smartFeed.smallIcon {
|
|
|
|
|
|
smartFeedIconImageCache[feedID] = iconImage
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-02 11:48:14 -08:00
|
|
|
|
func imageForSmallIconProvider(_ provider: SmallIconProvider, _ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
if let iconImage = smallIconImageCache[feedID] {
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
if let iconImage = provider.smallIcon {
|
|
|
|
|
|
smallIconImageCache[feedID] = iconImage
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func imageForAuthors(_ authors: Set<Author>?) -> IconImage? {
|
|
|
|
|
|
guard let authors = authors, authors.count == 1, let author = authors.first else {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return imageForAuthor(author)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func imageForAuthor(_ author: Author) -> IconImage? {
|
2026-11-03 11:10:10 -08:00
|
|
|
|
if let iconImage = AuthorAvatarDownloader.shared.avatar(for: author).iconImage {
|
2021-05-08 12:42:44 -07:00
|
|
|
|
authorIconImageCache[author] = iconImage
|
|
|
|
|
|
return iconImage
|
|
|
|
|
|
}
|
2026-10-29 09:12:16 -07:00
|
|
|
|
return authorIconImageCache[author]
|
2021-05-08 12:42:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|