Standardize notification handling on Combine instead of selectors

This commit is contained in:
Maurice Parker
2020-07-18 15:20:15 -05:00
parent 5845925b3a
commit 876f978347
5 changed files with 116 additions and 154 deletions

View File

@@ -7,19 +7,40 @@
//
import Foundation
import Combine
import Account
import Articles
final class ArticleIconImageLoader: ObservableObject {
private var article: Article?
@Published var image: IconImage?
private var article: Article?
private var cancellables = Set<AnyCancellable>()
init() {
NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(webFeedIconDidBecomeAvailable(_:)), name: .WebFeedIconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil)
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)
}
func loadImage(for article: Article) {
@@ -29,31 +50,3 @@ final class ArticleIconImageLoader: ObservableObject {
}
}
private extension ArticleIconImageLoader {
@objc func faviconDidBecomeAvailable(_ note: Notification) {
guard let article = article else { return }
image = article.iconImage()
}
@objc func webFeedIconDidBecomeAvailable(_ note: Notification) {
guard let article = article, let noteFeed = note.userInfo?[UserInfoKey.webFeed] as? WebFeed, noteFeed == article.webFeed else {
return
}
image = article.iconImage()
}
@objc func avatarDidBecomeAvailable(_ note: Notification) {
guard let article = article, let authors = article.authors, let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
return
}
for author in authors {
if author.avatarURL == avatarURL {
image = article.iconImage()
return
}
}
}
}