From 03af68924ce36ed4e198025602f54f627ca1d375 Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 18 Apr 2022 17:14:49 +0800 Subject: [PATCH] feat: add favicon for NewsView --- .../xcshareddata/swiftpm/Package.resolved | 18 ++++++++++++ MastodonSDK/Package.swift | 4 ++- .../View/Content/NewsView+Configuration.swift | 19 ++++++++++++ .../MastodonUI/View/Content/NewsView.swift | 29 +++++++++++++++++-- 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved index 8dd69150..53f6a3a4 100644 --- a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -55,6 +55,15 @@ "version": "1.2.0" } }, + { + "package": "FaviconFinder", + "repositoryURL": "https://github.com/will-lumley/FaviconFinder.git", + "state": { + "branch": null, + "revision": "1f74844f77f79b95c0bb0130b3a87d4f340e6d3a", + "version": "3.3.0" + } + }, { "package": "FLAnimatedImage", "repositoryURL": "https://github.com/Flipboard/FLAnimatedImage.git", @@ -172,6 +181,15 @@ "version": "1.0.0" } }, + { + "package": "SwiftSoup", + "repositoryURL": "https://github.com/scinfu/SwiftSoup.git", + "state": { + "branch": null, + "revision": "41e7c263fb8c277e980ebcb9b0b5f6031d3d4886", + "version": "2.4.2" + } + }, { "package": "Introspect", "repositoryURL": "https://github.com/siteline/SwiftUI-Introspect.git", diff --git a/MastodonSDK/Package.swift b/MastodonSDK/Package.swift index ed4a13d5..8b007c2a 100644 --- a/MastodonSDK/Package.swift +++ b/MastodonSDK/Package.swift @@ -37,7 +37,8 @@ let package = Package( .package(url: "https://github.com/Alamofire/AlamofireImage.git", from: "4.1.0"), .package(name: "NukeFLAnimatedImagePlugin", url: "https://github.com/kean/Nuke-FLAnimatedImage-Plugin.git", from: "8.0.0"), .package(name: "UITextView+Placeholder", url: "https://github.com/MainasuK/UITextView-Placeholder.git", from: "1.4.1"), - .package(name: "Introspect", url: "https://github.com/siteline/SwiftUI-Introspect.git", from: "0.1.3") + .package(name: "Introspect", url: "https://github.com/siteline/SwiftUI-Introspect.git", from: "0.1.3"), + .package(name: "FaviconFinder", url: "https://github.com/will-lumley/FaviconFinder.git", from: "3.2.2"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. @@ -95,6 +96,7 @@ let package = Package( .product(name: "AlamofireImage", package: "AlamofireImage"), .product(name: "MetaTextKit", package: "MetaTextKit"), .product(name: "FLAnimatedImage", package: "FLAnimatedImage"), + .product(name: "FaviconFinder", package: "FaviconFinder"), ] ), .testTarget( diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift index e045eafe..397982aa 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView+Configuration.swift @@ -9,9 +9,28 @@ import UIKit import MastodonSDK import MastodonLocalization import AlamofireImage +import FaviconFinder extension NewsView { public func configure(link: Mastodon.Entity.Link) { + let faviconPlaceholder = UIImage(systemName: "network") + providerFaviconImageView.image = faviconPlaceholder + if let url = URL(string: link.url) { + let token = providerFaviconImageView.tag + FaviconFinder(url: url).downloadFavicon { [weak self] result in + guard let self = self else { return } + switch result { + case .success(let favicon): + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + guard self.providerFaviconImageView.tag == token else { return } + self.providerFaviconImageView.image = favicon.image + } + case .failure: + break + } + } + } providerNameLabel.text = link.providerName headlineLabel.text = link.title footnoteLabel.text = L10n.Plural.peopleTalking(link.talkingPeopleCount ?? 0) diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift index ee9506a9..6d4cf3fd 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NewsView.swift @@ -12,6 +12,15 @@ public final class NewsView: UIView { let container = UIStackView() + let providerFaviconImageView: UIImageView = { + let imageView = UIImageView() + imageView.contentMode = .scaleAspectFit + imageView.layer.masksToBounds = true + imageView.layer.cornerRadius = 2 + imageView.layer.cornerCurve = .continuous + return imageView + }() + let providerNameLabel: UILabel = { let label = UILabel() label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: .systemFont(ofSize: 13, weight: .semibold)) @@ -37,6 +46,7 @@ public final class NewsView: UIView { let imageView = MediaView() public func prepareForReuse() { + providerFaviconImageView.tag = (0..