From bf4dd59d9048de405001e8b26986892e4925e64a Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Sat, 7 Dec 2019 15:43:11 -0600 Subject: [PATCH 1/2] Use the app icon for the NNW feed --- NetNewsWire.xcodeproj/project.pbxproj | 8 +++++ Shared/Extensions/RSImage-AppIcons.swift | 38 +++++++++++++++++++++++ Shared/Favicons/FaviconDownloader.swift | 5 +++ Shared/Images/WebFeedIconDownloader.swift | 5 +++ 4 files changed, 56 insertions(+) create mode 100644 Shared/Extensions/RSImage-AppIcons.swift diff --git a/NetNewsWire.xcodeproj/project.pbxproj b/NetNewsWire.xcodeproj/project.pbxproj index 7123eab29..52fd04c74 100644 --- a/NetNewsWire.xcodeproj/project.pbxproj +++ b/NetNewsWire.xcodeproj/project.pbxproj @@ -631,6 +631,9 @@ 84F9EAF4213660A100CF2DE4 /* testGenericScript.applescript in Sources */ = {isa = PBXBuildFile; fileRef = 84F9EAE1213660A100CF2DE4 /* testGenericScript.applescript */; }; 84F9EAF5213660A100CF2DE4 /* establishMainWindowStartingState.applescript in Sources */ = {isa = PBXBuildFile; fileRef = 84F9EAE2213660A100CF2DE4 /* establishMainWindowStartingState.applescript */; }; 84FF69B11FC3793300DC198E /* FaviconURLFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FF69B01FC3793300DC198E /* FaviconURLFinder.swift */; }; + B2B8075E239C49D300F191E0 /* RSImage-AppIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8075D239C49D300F191E0 /* RSImage-AppIcons.swift */; }; + B2B80778239C4C7000F191E0 /* RSImage-AppIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8075D239C49D300F191E0 /* RSImage-AppIcons.swift */; }; + B2B80779239C4C7300F191E0 /* RSImage-AppIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2B8075D239C49D300F191E0 /* RSImage-AppIcons.swift */; }; B528F81E23333C7E00E735DD /* page.html in Resources */ = {isa = PBXBuildFile; fileRef = B528F81D23333C7E00E735DD /* page.html */; }; D553738B20186C20006D8857 /* Article+Scriptability.swift in Sources */ = {isa = PBXBuildFile; fileRef = D553737C20186C1F006D8857 /* Article+Scriptability.swift */; }; D57BE6E0204CD35F00D11AAC /* NSScriptCommand+NetNewsWire.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57BE6DF204CD35F00D11AAC /* NSScriptCommand+NetNewsWire.swift */; }; @@ -1563,6 +1566,7 @@ 84FF69B01FC3793300DC198E /* FaviconURLFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FaviconURLFinder.swift; sourceTree = ""; }; B24EFD482330FF99006C6242 /* NetNewsWire-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NetNewsWire-Bridging-Header.h"; sourceTree = ""; }; B24EFD5923310109006C6242 /* WKPreferencesPrivate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WKPreferencesPrivate.h; sourceTree = ""; }; + B2B8075D239C49D300F191E0 /* RSImage-AppIcons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RSImage-AppIcons.swift"; sourceTree = ""; }; B528F81D23333C7E00E735DD /* page.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = page.html; sourceTree = ""; }; D519E74722EE553300923F27 /* NetNewsWire_safariextension_target.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = NetNewsWire_safariextension_target.xcconfig; sourceTree = ""; }; D553737C20186C1F006D8857 /* Article+Scriptability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Article+Scriptability.swift"; sourceTree = ""; }; @@ -2324,6 +2328,7 @@ 8405DD9B22153BD7008CE1BF /* NSView-Extensions.swift */, 51126DA3225FDE2F00722696 /* RSImage-Extensions.swift */, 516AE9DE2372269A007DEEAA /* IconImage.swift */, + B2B8075D239C49D300F191E0 /* RSImage-AppIcons.swift */, ); path = Extensions; sourceTree = ""; @@ -3764,6 +3769,7 @@ 65ED4023235DEF6C0081F399 /* Folder+Scriptability.swift in Sources */, 65ED4024235DEF6C0081F399 /* TimelineCellLayout.swift in Sources */, 65ED4025235DEF6C0081F399 /* DetailWebView.swift in Sources */, + B2B80779239C4C7300F191E0 /* RSImage-AppIcons.swift in Sources */, 65ED4026235DEF6C0081F399 /* TimelineTableRowView.swift in Sources */, 65ED4027235DEF6C0081F399 /* UnreadIndicatorView.swift in Sources */, 51A9A5F22380DE520033AADF /* AddWebFeedDefaultContainer.swift in Sources */, @@ -3814,6 +3820,7 @@ 51C452A422650A2D00C03939 /* ArticleUtilities.swift in Sources */, 51EF0F79227716380050506E /* ColorHash.swift in Sources */, 5183CCDA226E31A50010922C /* NonIntrinsicImageView.swift in Sources */, + B2B80778239C4C7000F191E0 /* RSImage-AppIcons.swift in Sources */, 51A9A5F52380F6A60033AADF /* ModalNavigationController.swift in Sources */, 51EAED96231363EF00A9EEE3 /* NonIntrinsicButton.swift in Sources */, 51C4527B2265091600C03939 /* MasterUnreadIndicatorView.swift in Sources */, @@ -3998,6 +4005,7 @@ 849A975C1ED9EB0D007D329B /* DefaultFeedsImporter.swift in Sources */, 84A37CB5201ECD610087C5AF /* RenameWindowController.swift in Sources */, 84A14FF320048CA70046AD9A /* SendToMicroBlogCommand.swift in Sources */, + B2B8075E239C49D300F191E0 /* RSImage-AppIcons.swift in Sources */, 849A97891ED9ECEF007D329B /* ArticleStyle.swift in Sources */, 84FF69B11FC3793300DC198E /* FaviconURLFinder.swift in Sources */, 84B7178C201E66580091657D /* SidebarViewController+ContextualMenus.swift in Sources */, diff --git a/Shared/Extensions/RSImage-AppIcons.swift b/Shared/Extensions/RSImage-AppIcons.swift new file mode 100644 index 000000000..8af09f486 --- /dev/null +++ b/Shared/Extensions/RSImage-AppIcons.swift @@ -0,0 +1,38 @@ +// +// RSImage-AppIcons.swift +// NetNewsWire +// +// Created by Nate Weaver on 2019-12-07. +// Copyright © 2019 Ranchero Software. All rights reserved. +// + +import Foundation +import RSCore + +extension RSImage { + static var appIconImage: RSImage? { + #if os(macOS) + return RSImage(named: NSImage.applicationIconName) + #elseif os(iOS) + // https://stackoverflow.com/a/51241158/14256 + if let icons = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String: Any], + let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any], + let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String], + let lastIcon = iconFiles.last { + return RSImage(named: lastIcon) + } + #endif + + return nil + } +} + +extension IconImage { + static var appIcon: IconImage? { + if let image = RSImage.appIconImage { + return IconImage(image) + } + + return nil + } +} diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index 068ac580b..b493e168e 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -108,6 +108,11 @@ final class FaviconDownloader { } func favicon(with faviconURL: String, homePageURL: String?) -> IconImage? { + if let homePageURL = homePageURL, let url = URL(string: homePageURL) { + if url.host == "nnw.ranchero.com" { + return IconImage.appIcon + } + } let downloader = faviconDownloader(withURL: faviconURL, homePageURL: homePageURL) return downloader.iconImage } diff --git a/Shared/Images/WebFeedIconDownloader.swift b/Shared/Images/WebFeedIconDownloader.swift index f102ef212..d62f5201c 100644 --- a/Shared/Images/WebFeedIconDownloader.swift +++ b/Shared/Images/WebFeedIconDownloader.swift @@ -124,6 +124,11 @@ private extension WebFeedIconDownloader { func icon(forHomePageURL homePageURL: String, feed: WebFeed, _ imageResultBlock: @escaping (RSImage?) -> Void) { + if let url = URL(string: homePageURL), url.host == "nnw.ranchero.com" { + imageResultBlock(RSImage.appIconImage) + return + } + if homePagesWithNoIconURLCache.contains(homePageURL) || homePagesWithUglyIcons.contains(homePageURL) { imageResultBlock(nil) return From 71bc1e5d8afa567c89483cbf76c0e0d785e327dd Mon Sep 17 00:00:00 2001 From: Nate Weaver Date: Sat, 7 Dec 2019 16:08:57 -0600 Subject: [PATCH 2/2] Return the app icon earlier so we never hit the favicon cache --- Shared/Favicons/FaviconDownloader.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index b493e168e..a49ad6dfd 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -108,11 +108,6 @@ final class FaviconDownloader { } func favicon(with faviconURL: String, homePageURL: String?) -> IconImage? { - if let homePageURL = homePageURL, let url = URL(string: homePageURL) { - if url.host == "nnw.ranchero.com" { - return IconImage.appIcon - } - } let downloader = faviconDownloader(withURL: faviconURL, homePageURL: homePageURL) return downloader.iconImage } @@ -120,6 +115,13 @@ final class FaviconDownloader { func favicon(withHomePageURL homePageURL: String) -> IconImage? { let url = homePageURL.rs_normalizedURL() + + if let url = URL(string: homePageURL) { + if url.host == "nnw.ranchero.com" { + return IconImage.appIcon + } + } + if homePageURLsWithNoFaviconURLCache.contains(url) { return nil }