NetNewsWire/Shared/Images/FeedIconDownloader.swift

149 lines
3.3 KiB
Swift
Raw Normal View History

2017-11-26 22:48:40 +01:00
//
// FeedIconDownloader.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-11-26 22:48:40 +01:00
//
// Created by Brent Simmons on 11/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import Articles
import Account
import RSCore
2017-11-26 22:48:40 +01:00
import RSWeb
import RSParser
extension Notification.Name {
static let FeedIconDidBecomeAvailable = Notification.Name("FeedIconDidBecomeAvailableNotification") // UserInfoKey.feed
}
2017-11-26 22:48:40 +01:00
public final class FeedIconDownloader {
private let imageDownloader: ImageDownloader
private var homePageToIconURLCache = [String: String]()
2017-11-27 04:57:45 +01:00
private var homePagesWithNoIconURL = Set<String>()
private var urlsInProgress = Set<String>()
private var cache = [Feed: RSImage]()
2017-11-26 22:48:40 +01:00
init(imageDownloader: ImageDownloader) {
self.imageDownloader = imageDownloader
}
func resetCache() {
cache = [Feed: RSImage]()
}
func icon(for feed: Feed) -> RSImage? {
2017-11-26 22:48:40 +01:00
if let cachedImage = cache[feed] {
return cachedImage
}
func checkHomePageURL() {
guard let homePageURL = feed.homePageURL else {
return
}
icon(forHomePageURL: homePageURL) { (image) in
if let image = image {
self.postFeedIconDidBecomeAvailableNotification(feed)
self.cache[feed] = image
}
}
2017-11-26 22:48:40 +01:00
}
if let iconURL = feed.iconURL {
icon(forURL: iconURL) { (image) in
if let image = image {
self.postFeedIconDidBecomeAvailableNotification(feed)
self.cache[feed] = image
}
else {
checkHomePageURL()
}
}
2017-11-26 22:48:40 +01:00
}
else {
checkHomePageURL()
}
2017-11-26 22:48:40 +01:00
return nil
}
}
private extension FeedIconDownloader {
2017-11-26 22:48:40 +01:00
func icon(forHomePageURL homePageURL: String, _ imageResultBlock: @escaping (RSImage?) -> Void) {
2017-11-26 22:48:40 +01:00
2017-11-27 04:57:45 +01:00
if homePagesWithNoIconURL.contains(homePageURL) {
imageResultBlock(nil)
return
2017-11-27 04:57:45 +01:00
}
2017-11-26 22:48:40 +01:00
if let iconURL = cachedIconURL(for: homePageURL) {
icon(forURL: iconURL, imageResultBlock)
return
2017-11-26 22:48:40 +01:00
}
2017-11-26 23:03:08 +01:00
2017-11-26 22:48:40 +01:00
findIconURLForHomePageURL(homePageURL)
}
func icon(forURL url: String, _ imageResultBlock: @escaping (RSImage?) -> Void) {
guard let imageData = imageDownloader.image(for: url) else {
imageResultBlock(nil)
return
}
RSImage.scaledForAvatar(imageData, imageResultBlock: imageResultBlock)
2017-11-26 22:48:40 +01:00
}
func postFeedIconDidBecomeAvailableNotification(_ feed: Feed) {
DispatchQueue.main.async {
let userInfo: [AnyHashable: Any] = [UserInfoKey.feed: feed]
NotificationCenter.default.post(name: .FeedIconDidBecomeAvailable, object: self, userInfo: userInfo)
}
}
2017-11-26 22:48:40 +01:00
func cachedIconURL(for homePageURL: String) -> String? {
return homePageToIconURLCache[homePageURL]
}
func cacheIconURL(for homePageURL: String, _ iconURL: String) {
2017-11-27 04:57:45 +01:00
homePagesWithNoIconURL.remove(homePageURL)
2017-11-26 22:48:40 +01:00
homePageToIconURLCache[homePageURL] = iconURL
}
func findIconURLForHomePageURL(_ homePageURL: String) {
guard !urlsInProgress.contains(homePageURL) else {
2017-11-27 04:57:45 +01:00
return
}
urlsInProgress.insert(homePageURL)
2017-11-27 04:57:45 +01:00
2017-11-26 23:03:08 +01:00
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (metadata) in
2017-11-26 22:48:40 +01:00
self.urlsInProgress.remove(homePageURL)
2017-11-26 23:03:08 +01:00
guard let metadata = metadata else {
2017-11-26 22:48:40 +01:00
return
}
2017-11-26 23:03:08 +01:00
self.pullIconURL(from: metadata, homePageURL: homePageURL)
2017-11-26 22:48:40 +01:00
}
}
func pullIconURL(from metadata: RSHTMLMetadata, homePageURL: String) {
2017-11-27 04:57:45 +01:00
if let url = metadata.bestWebsiteIconURL() {
cacheIconURL(for: homePageURL, url)
icon(forURL: url) { (image) in
}
2017-11-26 22:48:40 +01:00
return
}
2017-11-27 04:57:45 +01:00
homePagesWithNoIconURL.insert(homePageURL)
2017-11-26 22:48:40 +01:00
}
}