diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 50361a2ae..6b65a8b21 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -29,7 +29,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat } var userNotificationManager: UserNotificationManager! - var faviconDownloader: FaviconDownloader! var extensionContainersFile: ExtensionContainersFile! var extensionFeedAddRequestFile: ExtensionFeedAddRequestFile! @@ -143,11 +142,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat cacheFolder = (NSTemporaryDirectory() as NSString).appendingPathComponent(bundleIdentifier) } - let faviconsFolder = (cacheFolder as NSString).appendingPathComponent("Favicons") - let faviconsFolderURL = URL(fileURLWithPath: faviconsFolder) - try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil) - faviconDownloader = FaviconDownloader(folder: faviconsFolder) - let imagesFolder = (cacheFolder as NSString).appendingPathComponent("Images") let imagesFolderURL = URL(fileURLWithPath: imagesFolder) try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) @@ -315,7 +309,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat return } if key == Feed.FeedSettingKey.homePageURL || key == Feed.FeedSettingKey.faviconURL { - _ = faviconDownloader.favicon(for: feed) + _ = FaviconDownloader.shared.favicon(for: feed) } } diff --git a/Shared/Extensions/SmallIconProvider.swift b/Shared/Extensions/SmallIconProvider.swift index 7dd606bdd..e1143b5a1 100644 --- a/Shared/Extensions/SmallIconProvider.swift +++ b/Shared/Extensions/SmallIconProvider.swift @@ -28,7 +28,7 @@ extension Account: SmallIconProvider { extension Feed: SmallIconProvider { var smallIcon: IconImage? { - if let iconImage = appDelegate.faviconDownloader.favicon(for: self) { + if let iconImage = FaviconDownloader.shared.favicon(for: self) { return iconImage } return FaviconGenerator.favicon(self) diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index 1182cb20b..b874a9ad5 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -23,6 +23,8 @@ extension Notification.Name { final class FaviconDownloader { + static let shared = FaviconDownloader() + private static let saveQueue = CoalescingQueue(name: "Cache Save Queue", interval: 1.0) private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FaviconDownloader") @@ -57,8 +59,9 @@ final class FaviconDownloader { static let faviconURL = "faviconURL" } - init(folder: String) { + init() { + let folder = AppConfig.cacheSubfolder(named: "Favicons") self.folder = folder self.diskCache = BinaryDiskCache(folder: folder) self.queue = DispatchQueue(label: "FaviconDownloader serial queue - \(folder)") diff --git a/Shared/IconImageCache.swift b/Shared/IconImageCache.swift index 69623e8d3..1884d784b 100644 --- a/Shared/IconImageCache.swift +++ b/Shared/IconImageCache.swift @@ -91,7 +91,7 @@ private extension IconImageCache { if let faviconImage = faviconImageCache[feedID] { return faviconImage } - if let faviconImage = appDelegate.faviconDownloader.faviconAsIcon(for: feed) { + if let faviconImage = FaviconDownloader.shared.faviconAsIcon(for: feed) { faviconImageCache[feedID] = faviconImage return faviconImage } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index af1edafea..e47ab9110 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -39,7 +39,6 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "Application") var userNotificationManager: UserNotificationManager! - var faviconDownloader: FaviconDownloader! var extensionContainersFile: ExtensionContainersFile! var extensionFeedAddRequestFile: ExtensionFeedAddRequestFile! var widgetDataEncoder: WidgetDataEncoder! @@ -224,14 +223,8 @@ private extension AppDelegate { private func initializeDownloaders() { let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! - let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") let imagesFolderURL = tempDir.appendingPathComponent("Images") - try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil) - let faviconsFolder = faviconsFolderURL.absoluteString - let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7)) - faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath)) - try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) }