// // FeedImageLoader.swift // NetNewsWire // // Created by Maurice Parker on 6/29/20. // Copyright © 2020 Ranchero Software. All rights reserved. // import SwiftUI import Account final class FeedImageLoader: ObservableObject { private var feed: Feed? @Published var image: IconImage? init() { NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil) } func loadImage(for feed: Feed) { self.feed = feed if let webFeed = feed as? WebFeed { if let feedIconImage = appDelegate.webFeedIconDownloader.icon(for: webFeed) { image = feedIconImage return } if let faviconImage = appDelegate.faviconDownloader.faviconAsIcon(for: webFeed) { image = faviconImage return } } if let smallIconProvider = feed as? SmallIconProvider { image = smallIconProvider.smallIcon } } } private extension FeedImageLoader { @objc func faviconDidBecomeAvailable(_ note: Notification) { guard let feed = feed else { return } loadImage(for: feed) } @objc func webFeedIconDidBecomeAvailable(_ note: Notification) { guard let feed = feed as? WebFeed, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, feed == noteFeed else { return } loadImage(for: feed) } }