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
|
2020-07-18 22:20:15 +02:00
|
|
|
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?
|
2020-07-18 22:20:15 +02:00
|
|
|
private var feed: Feed?
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2020-06-30 03:09:11 +02:00
|
|
|
|
|
|
|
init() {
|
2020-07-18 22:20:15 +02:00
|
|
|
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) {
|
2020-07-02 02:50:40 +02:00
|
|
|
guard image == nil else { return }
|
2020-06-30 03:09:11 +02:00
|
|
|
self.feed = feed
|
2020-07-02 02:50:40 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|