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
|
2020-07-18 22:20:15 +02:00
|
|
|
import Combine
|
2020-07-02 00:21:58 +02:00
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
final class ArticleIconImageLoader: ObservableObject {
|
|
|
|
|
|
|
|
@Published var image: IconImage?
|
2020-07-18 22:20:15 +02:00
|
|
|
private var article: Article?
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2020-07-02 00:21:58 +02:00
|
|
|
init() {
|
2020-07-18 22:20:15 +02:00
|
|
|
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) {
|
2020-07-02 02:50:40 +02:00
|
|
|
guard image == nil else { return }
|
2020-07-02 00:21:58 +02:00
|
|
|
self.article = article
|
|
|
|
image = article.iconImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|