NetNewsWire/Multiplatform/Shared/Images/ArticleIconImageLoader.swift

53 lines
1.5 KiB
Swift
Raw Normal View History

2020-07-02 00:21:58 +02:00
//
// ArticleIconImageLoader.swift
// NetNewsWire
//
// Created by Maurice Parker on 7/1/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import Foundation
import Combine
2020-07-02 00:21:58 +02:00
import Account
import Articles
final class ArticleIconImageLoader: ObservableObject {
@Published var image: IconImage?
private var article: Article?
private var cancellables = Set<AnyCancellable>()
2020-07-02 00:21:58 +02:00
init() {
NotificationCenter.default.publisher(for: .FaviconDidBecomeAvailable).sink { [weak self] _ in
guard let self = self, let article = self.article else { return }
self.image = article.iconImage()
}.store(in: &cancellables)
NotificationCenter.default.publisher(for: .WebFeedIconDidBecomeAvailable).sink { [weak self] note in
guard let self = self, let article = self.article, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, noteFeed == article.webFeed else {
return
}
self.image = article.iconImage()
}.store(in: &cancellables)
NotificationCenter.default.publisher(for: .AvatarDidBecomeAvailable).sink { [weak self] note in
guard let self = self, let article = self.article, let authors = article.authors, let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
return
}
for author in authors {
if author.avatarURL == avatarURL {
self.image = article.iconImage()
return
}
}
}.store(in: &cancellables)
2020-07-02 00:21:58 +02:00
}
func loadImage(for article: Article) {
guard image == nil else { return }
2020-07-02 00:21:58 +02:00
self.article = article
image = article.iconImage()
}
}