NetNewsWire/Multiplatform/Shared/Images/FeedIconImageLoader.swift

61 lines
1.4 KiB
Swift
Raw Normal View History

2020-06-30 03:09:11 +02:00
//
2020-07-01 23:37:20 +02:00
// FeedIconImageLoader.swift
2020-06-30 03:09:11 +02:00
// NetNewsWire
//
// Created by Maurice Parker on 6/29/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Combine
2020-06-30 03:09:11 +02:00
import Account
2020-07-01 23:37:20 +02:00
final class FeedIconImageLoader: ObservableObject {
2020-06-30 03:09:11 +02:00
@Published var image: IconImage?
private var feed: Feed?
private var cancellables = Set<AnyCancellable>()
2020-06-30 03:09:11 +02:00
init() {
NotificationCenter.default.publisher(for: .FaviconDidBecomeAvailable).sink { [weak self] _ in
self?.fetchImage()
}.store(in: &cancellables)
NotificationCenter.default.publisher(for: .WebFeedIconDidBecomeAvailable).sink { [weak self] note in
guard let feed = self?.feed as? WebFeed, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, feed == noteFeed else {
return
}
self?.fetchImage()
}.store(in: &cancellables)
2020-06-30 03:09:11 +02:00
}
func loadImage(for feed: Feed) {
guard image == nil else { return }
2020-06-30 03:09:11 +02:00
self.feed = feed
fetchImage()
}
}
private extension FeedIconImageLoader {
func fetchImage() {
2020-06-30 03:09:11 +02:00
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
}
}
}