From fe194ec2e5bd7a6ab8c62bfc6f37a680271ac9c6 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Thu, 24 Dec 2020 07:31:44 +0800 Subject: [PATCH] adds iconImageUrl to article utils --- Shared/Extensions/ArticleUtilities.swift | 16 ++++++++++++++++ .../UserNotificationManager.swift | 13 ++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Shared/Extensions/ArticleUtilities.swift b/Shared/Extensions/ArticleUtilities.swift index aeee931ae..6f6cd5126 100644 --- a/Shared/Extensions/ArticleUtilities.swift +++ b/Shared/Extensions/ArticleUtilities.swift @@ -103,6 +103,22 @@ extension Article { return FaviconGenerator.favicon(webFeed) } + func iconImageUrl(webFeed: WebFeed) -> URL? { + if let image = iconImage() { + let fm = FileManager.default + var path = fm.urls(for: .cachesDirectory, in: .userDomainMask)[0] + #if os(macOS) + path.appendPathComponent(webFeed.webFeedID + "_smallIcon.tiff") + #else + path.appendPathComponent(webFeed.webFeedID + "_smallIcon.png") + #endif + fm.createFile(atPath: path.path, contents: image.image.dataRepresentation()!, attributes: nil) + return path + } else { + return nil + } + } + func byline() -> String { guard let authors = authors ?? webFeed?.authors, !authors.isEmpty else { return "" diff --git a/Shared/UserNotifications/UserNotificationManager.swift b/Shared/UserNotifications/UserNotificationManager.swift index 0e040fa3a..50fe22227 100644 --- a/Shared/UserNotifications/UserNotificationManager.swift +++ b/Shared/UserNotifications/UserNotificationManager.swift @@ -73,17 +73,8 @@ private extension UserNotificationManager { /// - Returns: A `UNNotifcationAttachment` if an icon is available. Otherwise nil. /// - Warning: In certain scenarios, this will return the `faviconTemplateImage`. func thumbnailAttachment(for article: Article, webFeed: WebFeed) -> UNNotificationAttachment? { - if let image = article.iconImage() { - let fm = FileManager.default - var path = fm.urls(for: .cachesDirectory, in: .userDomainMask)[0] - #if os(macOS) - path.appendPathComponent(webFeed.webFeedID + "_smallIcon.tiff") - #else - path.appendPathComponent(webFeed.webFeedID + "_smallIcon.png") - #endif - fm.createFile(atPath: path.path, contents: image.image.dataRepresentation()!, attributes: nil) - - let thumbnail = try? UNNotificationAttachment(identifier: webFeed.webFeedID, url: path, options: nil) + if let imageURL = article.iconImageUrl(webFeed: webFeed) { + let thumbnail = try? UNNotificationAttachment(identifier: webFeed.webFeedID, url: imageURL, options: nil) return thumbnail } return nil