mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-29 00:31:14 +00:00
Rename ImageDidBecomeAvailable to imageDidBecomeAvailable. Do a few small code cleanups.
This commit is contained in:
1 parent
b1d4a8ac6f
commit
a4249fc554
4 files changed
+10
-23
No files matched your search
@@ -49,7 +49,7 @@ final class FeedInspectorViewController: NSViewController, Inspector {
|
||||
|
||||
override func viewDidLoad() {
|
||||
updateUI()
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .imageDidBecomeAvailable, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: .DidUpdateFeedPreferencesFromContextMenu, object: nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ final class AuthorAvatarDownloader {
|
||||
|
||||
init() {
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .imageDidBecomeAvailable, object: imageDownloader)
|
||||
}
|
||||
|
||||
func resetCache() {
|
||||
|
||||
@@ -41,7 +41,7 @@ public final class FeedIconDownloader {
|
||||
self.feedURLToIconURLCachePath = folder.appendingPathComponent("FeedURLToIconURLCache.plist")
|
||||
loadFeedURLToIconURLCache()
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: imageDownloader)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .imageDidBecomeAvailable, object: imageDownloader)
|
||||
}
|
||||
|
||||
func icon(for feed: Feed) -> IconImage? {
|
||||
|
||||
@@ -13,12 +13,10 @@ import RSWeb
|
||||
import RSCore
|
||||
|
||||
extension Notification.Name {
|
||||
|
||||
static let ImageDidBecomeAvailable = Notification.Name("ImageDidBecomeAvailableNotification") // UserInfoKey.url
|
||||
static let imageDidBecomeAvailable = Notification.Name("ImageDidBecomeAvailableNotification") // UserInfoKey.url
|
||||
}
|
||||
|
||||
final class ImageDownloader {
|
||||
|
||||
public static let shared = ImageDownloader()
|
||||
|
||||
static private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "ImageDownloader")
|
||||
@@ -30,7 +28,6 @@ final class ImageDownloader {
|
||||
private var badURLs = Set<String>() // That return a 404 or whatever. Just skip them in the future.
|
||||
|
||||
init() {
|
||||
|
||||
let folder = AppConfig.cacheSubfolder(named: "Images")
|
||||
self.diskCache = BinaryDiskCache(folder: folder.path)
|
||||
self.queue = DispatchQueue(label: "ImageDownloader serial queue - \(folder.path)")
|
||||
@@ -38,7 +35,7 @@ final class ImageDownloader {
|
||||
|
||||
@discardableResult
|
||||
func image(for url: String) -> Data? {
|
||||
|
||||
assert(Thread.isMainThread)
|
||||
if let data = imageCache[url] {
|
||||
return data
|
||||
}
|
||||
@@ -51,21 +48,19 @@ final class ImageDownloader {
|
||||
private extension ImageDownloader {
|
||||
|
||||
func cacheImage(_ url: String, _ image: Data) {
|
||||
|
||||
assert(Thread.isMainThread)
|
||||
imageCache[url] = image
|
||||
postImageDidBecomeAvailableNotification(url)
|
||||
}
|
||||
|
||||
func findImage(_ url: String) {
|
||||
|
||||
guard !urlsInProgress.contains(url) && !badURLs.contains(url) else {
|
||||
return
|
||||
}
|
||||
urlsInProgress.insert(url)
|
||||
|
||||
readFromDisk(url) { (image) in
|
||||
|
||||
if let image = image {
|
||||
if let image {
|
||||
self.cacheImage(url, image)
|
||||
self.urlsInProgress.remove(url)
|
||||
return
|
||||
@@ -74,7 +69,7 @@ private extension ImageDownloader {
|
||||
Task { @MainActor in
|
||||
let image = await self.downloadImage(url)
|
||||
|
||||
if let image = image {
|
||||
if let image {
|
||||
self.cacheImage(url, image)
|
||||
}
|
||||
self.urlsInProgress.remove(url)
|
||||
@@ -83,9 +78,7 @@ private extension ImageDownloader {
|
||||
}
|
||||
|
||||
func readFromDisk(_ url: String, _ completion: @escaping (Data?) -> Void) {
|
||||
|
||||
queue.async {
|
||||
|
||||
if let data = self.diskCache[self.diskKey(url)], !data.isEmpty {
|
||||
DispatchQueue.main.async {
|
||||
completion(data)
|
||||
@@ -100,7 +93,6 @@ private extension ImageDownloader {
|
||||
}
|
||||
|
||||
func downloadImage(_ url: String) async -> Data? {
|
||||
|
||||
guard let imageURL = URL(string: url) else {
|
||||
return nil
|
||||
}
|
||||
@@ -125,21 +117,16 @@ private extension ImageDownloader {
|
||||
}
|
||||
|
||||
func saveToDisk(_ url: String, _ data: Data) {
|
||||
|
||||
queue.async {
|
||||
self.diskCache[self.diskKey(url)] = data
|
||||
}
|
||||
}
|
||||
|
||||
func diskKey(_ url: String) -> String {
|
||||
|
||||
return url.md5String
|
||||
url.md5String
|
||||
}
|
||||
|
||||
func postImageDidBecomeAvailableNotification(_ url: String) {
|
||||
|
||||
DispatchQueue.main.async {
|
||||
NotificationCenter.default.post(name: .ImageDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: url])
|
||||
}
|
||||
NotificationCenter.default.postOnMainThread(name: .imageDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: url])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user