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.
|
|
|
|
//
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
import Foundation
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-07-28 21:16:14 +02:00
|
|
|
import Account
|
2019-04-12 00:53:03 +02:00
|
|
|
import RSCore
|
2017-11-26 22:48:40 +01:00
|
|
|
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>()
|
2019-04-12 00:53:03 +02:00
|
|
|
private var cache = [Feed: RSImage]()
|
2017-11-26 22:48:40 +01:00
|
|
|
|
|
|
|
init(imageDownloader: ImageDownloader) {
|
|
|
|
|
|
|
|
self.imageDownloader = imageDownloader
|
|
|
|
}
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
func icon(for feed: Feed) -> RSImage? {
|
2017-11-26 22:48:40 +01:00
|
|
|
|
2017-11-29 06:39:09 +01:00
|
|
|
if let cachedImage = cache[feed] {
|
|
|
|
return cachedImage
|
|
|
|
}
|
|
|
|
|
2019-05-18 08:02:18 +02:00
|
|
|
func checkHomePageURL() {
|
|
|
|
guard let homePageURL = feed.homePageURL else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
icon(forHomePageURL: homePageURL) { (image) in
|
2019-05-15 07:44:06 +02:00
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
|
|
|
self.cache[feed] = image
|
|
|
|
}
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-18 08:02:18 +02:00
|
|
|
if let iconURL = feed.iconURL {
|
|
|
|
icon(forURL: iconURL) { (image) in
|
2019-05-15 07:44:06 +02:00
|
|
|
if let image = image {
|
|
|
|
self.postFeedIconDidBecomeAvailableNotification(feed)
|
|
|
|
self.cache[feed] = image
|
|
|
|
}
|
2019-05-18 08:02:18 +02:00
|
|
|
else {
|
|
|
|
checkHomePageURL()
|
|
|
|
}
|
2017-11-29 06:39:09 +01:00
|
|
|
}
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
2019-05-18 08:02:18 +02:00
|
|
|
else {
|
|
|
|
checkHomePageURL()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-05-15 07:44:06 +02: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) {
|
2019-05-15 07:44:06 +02:00
|
|
|
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) {
|
2019-05-15 07:44:06 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:44:06 +02:00
|
|
|
func icon(forURL url: String, _ imageResultBlock: @escaping (RSImage?) -> Void) {
|
|
|
|
guard let imageData = imageDownloader.image(for: url) else {
|
|
|
|
imageResultBlock(nil)
|
|
|
|
return
|
2019-04-12 00:53:03 +02:00
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
RSImage.scaledForAvatar(imageData, imageResultBlock: imageResultBlock)
|
2017-11-26 22:48:40 +01:00
|
|
|
}
|
|
|
|
|
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)
|
2019-05-15 07:44:06 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|