2017-11-26 22:48:40 +01:00
|
|
|
//
|
|
|
|
// FeedIconDownloader.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/26/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import Data
|
|
|
|
import RSWeb
|
|
|
|
import RSParser
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
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>()
|
2017-11-29 06:39:09 +01:00
|
|
|
private var urlsInProgress = Set<String>()
|
|
|
|
private var cache = [Feed: NSImage]()
|
2017-11-26 22:48:40 +01:00
|
|
|
|
|
|
|
init(imageDownloader: ImageDownloader) {
|
|
|
|
|
|
|
|
self.imageDownloader = imageDownloader
|
|
|
|
}
|
|
|
|
|
|
|
|
func icon(for feed: Feed) -> NSImage? {
|
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
if let cachedImage = cache[feed] {
|
|
|
|
return cachedImage
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
if let iconURL = feed.iconURL {
|
2017-11-29 06:39:09 +01:00
|
|
|
if let image = icon(forURL: iconURL) {
|
2018-01-05 06:20:09 +01:00
|
|
|
postFeedIconDidBecomeAvailableNotification(feed)
|
2017-11-29 06:39:09 +01:00
|
|
|
cache[feed] = image
|
|
|
|
return image
|
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let homePageURL = feed.homePageURL {
|
2017-11-29 06:39:09 +01:00
|
|
|
if let image = icon(forHomePageURL: homePageURL) {
|
2018-01-05 06:20:09 +01:00
|
|
|
postFeedIconDidBecomeAvailableNotification(feed)
|
2017-11-29 06:39:09 +01:00
|
|
|
cache[feed] = image
|
|
|
|
return image
|
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension FeedIconDownloader {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
|
|
|
func icon(forHomePageURL homePageURL: String) -> NSImage? {
|
|
|
|
|
2017-11-27 04:57:45 +01:00
|
|
|
if homePagesWithNoIconURL.contains(homePageURL) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
if let iconURL = cachedIconURL(for: homePageURL) {
|
|
|
|
return icon(forURL: iconURL)
|
|
|
|
}
|
2017-11-26 23:03:08 +01:00
|
|
|
|
2017-11-26 22:48:40 +01:00
|
|
|
findIconURLForHomePageURL(homePageURL)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func icon(forURL url: String) -> NSImage? {
|
|
|
|
|
|
|
|
return imageDownloader.image(for: url)
|
|
|
|
}
|
|
|
|
|
2018-01-05 06:20:09 +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) {
|
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
guard !urlsInProgress.contains(homePageURL) else {
|
2017-11-27 04:57:45 +01:00
|
|
|
return
|
|
|
|
}
|
2017-11-29 06:39:09 +01:00
|
|
|
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
|
|
|
|
2017-11-29 06:39:09 +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)
|
|
|
|
let _ = icon(forURL: url)
|
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
|
|
|
}
|
|
|
|
}
|