2021-05-08 21:42:44 +02:00
|
|
|
//
|
|
|
|
// IconImageCache.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 5/2/21.
|
|
|
|
// Copyright © 2021 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Account
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
class IconImageCache {
|
|
|
|
|
|
|
|
static var shared = IconImageCache()
|
|
|
|
|
2024-02-26 06:17:00 +01:00
|
|
|
private var smartFeedIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2024-02-26 08:12:21 +01:00
|
|
|
private var feedIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2024-02-26 06:17:00 +01:00
|
|
|
private var faviconImageCache = [SidebarItemIdentifier: IconImage]()
|
|
|
|
private var smallIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2021-05-08 21:42:44 +02:00
|
|
|
private var authorIconImageCache = [Author: IconImage]()
|
|
|
|
|
2024-02-26 06:17:00 +01:00
|
|
|
func imageFor(_ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 21:42:44 +02:00
|
|
|
if let smartFeed = SmartFeedsController.shared.find(by: feedID) {
|
|
|
|
return imageForFeed(smartFeed)
|
|
|
|
}
|
2024-02-26 17:12:34 +01:00
|
|
|
if let feed = AccountManager.shared.existingSidebarItem(with: feedID) {
|
2021-05-08 21:42:44 +02:00
|
|
|
return imageForFeed(feed)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 06:34:22 +01:00
|
|
|
func imageForFeed(_ sidebarItem: SidebarItem) -> IconImage? {
|
|
|
|
guard let sidebarItemID = sidebarItem.sidebarItemID else {
|
2021-05-08 21:42:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 06:34:22 +01:00
|
|
|
if let smartFeed = sidebarItem as? PseudoFeed {
|
|
|
|
return imageForSmartFeed(smartFeed, sidebarItemID)
|
2021-05-08 21:42:44 +02:00
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
if let feed = sidebarItem as? Feed, let iconImage = imageForFeed(feed, sidebarItemID) {
|
2021-05-08 21:42:44 +02:00
|
|
|
return iconImage
|
|
|
|
}
|
2024-02-26 06:34:22 +01:00
|
|
|
if let smallIconProvider = sidebarItem as? SmallIconProvider {
|
|
|
|
return imageForSmallIconProvider(smallIconProvider, sidebarItemID)
|
2021-05-08 21:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageForArticle(_ article: Article) -> IconImage? {
|
|
|
|
if let iconImage = imageForAuthors(article.authors) {
|
|
|
|
return iconImage
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
guard let feed = article.feed else {
|
2021-05-08 21:42:44 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return imageForFeed(feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emptyCache() {
|
2024-02-26 06:17:00 +01:00
|
|
|
smartFeedIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2024-02-26 08:12:21 +01:00
|
|
|
feedIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2024-02-26 06:17:00 +01:00
|
|
|
faviconImageCache = [SidebarItemIdentifier: IconImage]()
|
|
|
|
smallIconImageCache = [SidebarItemIdentifier: IconImage]()
|
2021-05-08 21:42:44 +02:00
|
|
|
authorIconImageCache = [Author: IconImage]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension IconImageCache {
|
|
|
|
|
2024-02-26 06:17:00 +01:00
|
|
|
func imageForSmartFeed(_ smartFeed: PseudoFeed, _ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 21:42:44 +02:00
|
|
|
if let iconImage = smartFeedIconImageCache[feedID] {
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
if let iconImage = smartFeed.smallIcon {
|
|
|
|
smartFeedIconImageCache[feedID] = iconImage
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
func imageForFeed(_ feed: Feed, _ feedID: SidebarItemIdentifier) -> IconImage? {
|
|
|
|
if let iconImage = feedIconImageCache[feedID] {
|
2021-05-08 21:42:44 +02:00
|
|
|
return iconImage
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
if let iconImage = appDelegate.feedIconDownloader.icon(for: feed) {
|
|
|
|
feedIconImageCache[feedID] = iconImage
|
2021-05-08 21:42:44 +02:00
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
if let faviconImage = faviconImageCache[feedID] {
|
|
|
|
return faviconImage
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
if let faviconImage = appDelegate.faviconDownloader.faviconAsIcon(for: feed) {
|
2021-05-08 21:42:44 +02:00
|
|
|
faviconImageCache[feedID] = faviconImage
|
|
|
|
return faviconImage
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 06:17:00 +01:00
|
|
|
func imageForSmallIconProvider(_ provider: SmallIconProvider, _ feedID: SidebarItemIdentifier) -> IconImage? {
|
2021-05-08 21:42:44 +02:00
|
|
|
if let iconImage = smallIconImageCache[feedID] {
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
if let iconImage = provider.smallIcon {
|
|
|
|
smallIconImageCache[feedID] = iconImage
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageForAuthors(_ authors: Set<Author>?) -> IconImage? {
|
|
|
|
guard let authors = authors, authors.count == 1, let author = authors.first else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return imageForAuthor(author)
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageForAuthor(_ author: Author) -> IconImage? {
|
|
|
|
if let iconImage = authorIconImageCache[author] {
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
if let iconImage = appDelegate.authorAvatarDownloader.image(for: author) {
|
|
|
|
authorIconImageCache[author] = iconImage
|
|
|
|
return iconImage
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|