NetNewsWire/Shared/Images/FeedIconDownloader.swift

255 lines
6.9 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 static let saveQueue = CoalescingQueue(name: "Cache Save Queue", interval: 1.0)
2017-11-26 22:48:40 +01:00
private let imageDownloader: ImageDownloader
2017-11-26 22:48:40 +01:00
private var homePageToIconURLCache = [String: String]()
private var homePageToIconURLCachePath: String
private var homePageToIconURLCacheDirty = false {
didSet {
queueSaveHomePageToIconURLCacheIfNeeded()
}
}
private var homePagesWithNoIconURLCache = Set<String>()
private var homePagesWithNoIconURLCachePath: String
private var homePagesWithNoIconURLCacheDirty = false {
didSet {
queueHomePagesWithNoIconURLCacheIfNeeded()
}
}
private var homePagesWithUglyIcons: Set<String> = {
return Set(["https://www.macsparky.com/"])
}()
private var urlsInProgress = Set<String>()
private var cache = [Feed: IconImage]()
private var waitingForFeedURLs = [String: Feed]()
2017-11-26 22:48:40 +01:00
init(imageDownloader: ImageDownloader, folder: String) {
2017-11-26 22:48:40 +01:00
self.imageDownloader = imageDownloader
self.homePageToIconURLCachePath = (folder as NSString).appendingPathComponent("HomePageToIconURLCache.plist")
self.homePagesWithNoIconURLCachePath = (folder as NSString).appendingPathComponent("HomePagesWithNoIconURLCache.plist")
loadHomePageToIconURLCache()
loadHomePagesWithNoIconURLCache()
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
2017-11-26 22:48:40 +01:00
}
func resetCache() {
cache = [Feed: IconImage]()
}
func icon(for feed: Feed) -> IconImage? {
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, feed: feed) { (image) in
if let image = image {
self.postFeedIconDidBecomeAvailableNotification(feed)
self.cache[feed] = IconImage(image)
}
}
2017-11-26 22:48:40 +01:00
}
if let iconURL = feed.iconURL {
icon(forURL: iconURL, feed: feed) { (image) in
if let image = image {
self.postFeedIconDidBecomeAvailableNotification(feed)
self.cache[feed] = IconImage(image)
}
else {
checkHomePageURL()
}
}
2017-11-26 22:48:40 +01:00
}
else {
checkHomePageURL()
}
2017-11-26 22:48:40 +01:00
return nil
}
@objc func imageDidBecomeAvailable(_ note: Notification) {
guard let url = note.userInfo?[UserInfoKey.url] as? String, let feed = waitingForFeedURLs[url] else {
return
}
waitingForFeedURLs[url] = nil
_ = icon(for: feed)
}
@objc func saveHomePageToIconURLCacheIfNeeded() {
if homePageToIconURLCacheDirty {
saveHomePageToIconURLCache()
}
}
@objc func saveHomePagesWithNoIconURLCacheIfNeeded() {
if homePagesWithNoIconURLCacheDirty {
saveHomePagesWithNoIconURLCache()
}
}
}
private extension FeedIconDownloader {
2017-11-26 22:48:40 +01:00
func icon(forHomePageURL homePageURL: String, feed: Feed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
2017-11-26 22:48:40 +01:00
if homePagesWithNoIconURLCache.contains(homePageURL) || homePagesWithUglyIcons.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, feed: feed, imageResultBlock)
return
2017-11-26 22:48:40 +01:00
}
2017-11-26 23:03:08 +01:00
findIconURLForHomePageURL(homePageURL, feed: feed)
2017-11-26 22:48:40 +01:00
}
func icon(forURL url: String, feed: Feed, _ imageResultBlock: @escaping (RSImage?) -> Void) {
waitingForFeedURLs[url] = feed
guard let imageData = imageDownloader.image(for: url) else {
imageResultBlock(nil)
return
}
RSImage.scaledForIcon(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) {
homePagesWithNoIconURLCache.remove(homePageURL)
homePagesWithNoIconURLCacheDirty = true
2017-11-26 22:48:40 +01:00
homePageToIconURLCache[homePageURL] = iconURL
homePageToIconURLCacheDirty = true
2017-11-26 22:48:40 +01:00
}
func findIconURLForHomePageURL(_ homePageURL: String, feed: Feed) {
2017-11-26 22:48:40 +01:00
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
}
self.pullIconURL(from: metadata, homePageURL: homePageURL, feed: feed)
2017-11-26 22:48:40 +01:00
}
}
func pullIconURL(from metadata: RSHTMLMetadata, homePageURL: String, feed: Feed) {
2017-11-26 22:48:40 +01:00
2017-11-27 04:57:45 +01:00
if let url = metadata.bestWebsiteIconURL() {
cacheIconURL(for: homePageURL, url)
icon(forURL: url, feed: feed) { (image) in
}
2017-11-26 22:48:40 +01:00
return
}
homePagesWithNoIconURLCache.insert(homePageURL)
homePagesWithNoIconURLCacheDirty = true
2017-11-26 22:48:40 +01:00
}
func loadHomePageToIconURLCache() {
let url = URL(fileURLWithPath: homePageToIconURLCachePath)
guard let data = try? Data(contentsOf: url) else {
return
}
let decoder = PropertyListDecoder()
homePageToIconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
}
func loadHomePagesWithNoIconURLCache() {
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
guard let data = try? Data(contentsOf: url) else {
return
}
let decoder = PropertyListDecoder()
let decoded = (try? decoder.decode([String].self, from: data)) ?? [String]()
homePagesWithNoIconURLCache = Set(decoded)
}
func queueSaveHomePageToIconURLCacheIfNeeded() {
FeedIconDownloader.saveQueue.add(self, #selector(saveHomePageToIconURLCacheIfNeeded))
}
func queueHomePagesWithNoIconURLCacheIfNeeded() {
FeedIconDownloader.saveQueue.add(self, #selector(saveHomePagesWithNoIconURLCacheIfNeeded))
}
func saveHomePageToIconURLCache() {
homePageToIconURLCacheDirty = false
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
let url = URL(fileURLWithPath: homePageToIconURLCachePath)
do {
let data = try encoder.encode(homePageToIconURLCache)
try data.write(to: url)
} catch {
assertionFailure(error.localizedDescription)
}
}
func saveHomePagesWithNoIconURLCache() {
homePagesWithNoIconURLCacheDirty = false
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
let url = URL(fileURLWithPath: homePagesWithNoIconURLCachePath)
do {
let data = try encoder.encode(Array(homePagesWithNoIconURLCache))
try data.write(to: url)
} catch {
assertionFailure(error.localizedDescription)
}
}
2017-11-26 22:48:40 +01:00
}