2017-11-26 22:16:32 +01:00
|
|
|
//
|
|
|
|
// AuthorAvatarDownloader.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2017-11-26 22:16:32 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/26/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
import Foundation
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2024-03-21 04:49:15 +01:00
|
|
|
import Core
|
2017-11-26 22:16:32 +01:00
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
extension Notification.Name {
|
|
|
|
|
2018-01-05 22:22:16 +01:00
|
|
|
static let AvatarDidBecomeAvailable = Notification.Name("AvatarDidBecomeAvailableNotification") // UserInfoKey.imageURL (which is an avatarURL)
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 22:16:32 +01:00
|
|
|
final class AuthorAvatarDownloader {
|
|
|
|
|
|
|
|
private let imageDownloader: ImageDownloader
|
2019-11-06 01:05:57 +01:00
|
|
|
private var cache = [String: IconImage]() // avatarURL: RSImage
|
2018-01-05 22:22:16 +01:00
|
|
|
private var waitingForAvatarURLs = Set<String>()
|
2017-11-26 22:16:32 +01:00
|
|
|
|
|
|
|
init(imageDownloader: ImageDownloader) {
|
|
|
|
|
|
|
|
self.imageDownloader = imageDownloader
|
2018-01-05 22:22:16 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
2017-11-26 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 22:33:13 +02:00
|
|
|
func resetCache() {
|
2019-11-06 01:05:57 +01:00
|
|
|
cache = [String: IconImage]()
|
2019-06-14 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
func image(for author: Author) -> IconImage? {
|
2017-11-26 22:16:32 +01:00
|
|
|
|
|
|
|
guard let avatarURL = author.avatarURL else {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-12 00:53:03 +02:00
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
if let cachedImage = cache[avatarURL] {
|
|
|
|
return cachedImage
|
|
|
|
}
|
2019-04-12 00:53:03 +02:00
|
|
|
|
|
|
|
if let imageData = imageDownloader.image(for: avatarURL) {
|
2019-05-15 07:44:06 +02:00
|
|
|
scaleAndCacheImageData(imageData, avatarURL)
|
|
|
|
}
|
|
|
|
else {
|
2018-01-05 22:22:16 +01:00
|
|
|
waitingForAvatarURLs.insert(avatarURL)
|
|
|
|
}
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-05 22:22:16 +01:00
|
|
|
|
|
|
|
@objc func imageDidBecomeAvailable(_ note: Notification) {
|
|
|
|
guard let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard waitingForAvatarURLs.contains(avatarURL) else {
|
|
|
|
return
|
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
guard let imageData = imageDownloader.image(for: avatarURL) else {
|
2018-01-05 22:22:16 +01:00
|
|
|
return
|
|
|
|
}
|
2019-05-15 07:44:06 +02:00
|
|
|
scaleAndCacheImageData(imageData, avatarURL)
|
2018-01-05 22:22:16 +01:00
|
|
|
}
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension AuthorAvatarDownloader {
|
|
|
|
|
2019-05-15 07:44:06 +02:00
|
|
|
func scaleAndCacheImageData(_ imageData: Data, _ avatarURL: String) {
|
2019-11-06 01:05:57 +01:00
|
|
|
RSImage.scaledForIcon(imageData) { (image) in
|
2019-05-15 07:44:06 +02:00
|
|
|
if let image = image {
|
|
|
|
self.handleImageDidBecomeAvailable(avatarURL, image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 22:22:16 +01:00
|
|
|
|
2019-05-15 07:44:06 +02:00
|
|
|
func handleImageDidBecomeAvailable(_ avatarURL: String, _ image: RSImage) {
|
2018-01-05 22:22:16 +01:00
|
|
|
if cache[avatarURL] == nil {
|
2019-11-06 01:05:57 +01:00
|
|
|
cache[avatarURL] = IconImage(image)
|
2018-01-05 22:22:16 +01:00
|
|
|
}
|
|
|
|
if waitingForAvatarURLs.contains(avatarURL) {
|
|
|
|
waitingForAvatarURLs.remove(avatarURL)
|
|
|
|
postAvatarDidBecomeAvailableNotification(avatarURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func postAvatarDidBecomeAvailableNotification(_ avatarURL: String) {
|
2018-01-05 06:20:09 +01:00
|
|
|
DispatchQueue.main.async {
|
2018-01-05 22:22:16 +01:00
|
|
|
NotificationCenter.default.post(name: .AvatarDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: avatarURL])
|
2018-01-05 06:20:09 +01:00
|
|
|
}
|
2017-11-26 22:16:32 +01:00
|
|
|
}
|
|
|
|
}
|