From 9f90c7b4cb273f71e28396243e6901806210415c Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Wed, 10 Jan 2024 08:46:16 -0800 Subject: [PATCH] Add a method to refresh following planet icon --- Planet/Entities/FollowingPlanetModel.swift | 100 ++++++++++++++++++ .../Sidebar/FollowingPlanetSidebarItem.swift | 18 ++++ Planet/versioning.xcconfig | 2 +- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/Planet/Entities/FollowingPlanetModel.swift b/Planet/Entities/FollowingPlanetModel.swift index 57fb3e8b..8b54b36c 100644 --- a/Planet/Entities/FollowingPlanetModel.swift +++ b/Planet/Entities/FollowingPlanetModel.swift @@ -1657,6 +1657,106 @@ class FollowingPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, } } + func removeIcon() async { + try? FileManager.default.removeItem(at: avatarPath) + DispatchQueue.main.async { + self.avatar = nil + } + } + + func refreshIcon() async { + switch planetType { + case .planet: + if let planetAvatarURL = URL( + string: "\(IPFSDaemon.shared.gateway)/ipfs/\(cid)/avatar.png" + ), + let (data, response) = try? await URLSession.shared.data(from: planetAvatarURL), + let httpResponse = response as? HTTPURLResponse, + httpResponse.ok, + let image = NSImage(data: data), + let _ = try? data.write(to: self.avatarPath) + { + DispatchQueue.main.async { + self.avatar = image + } + } + case .dns: + guard let feedURL = URL(string: link) else { + return + } + do { + let (feedData, htmlSoup) = try await FeedUtils.findFeed(url: feedURL) + + guard let feedData = feedData else { + return + } + + var feedAvatar: Data? = nil + var urlForFindingAvatar: URL? = nil + let homepageDocument: Document? + + if htmlSoup == nil { + if let domain = feedURL.host { + urlForFindingAvatar = URL(string: "https://\(domain)") + } + if let avatarPageURL = urlForFindingAvatar { + homepageDocument = try await FeedUtils.getHTMLDocument(url: avatarPageURL) + } + else { + homepageDocument = nil + } + } + else { + homepageDocument = htmlSoup + urlForFindingAvatar = feedURL + } + if let soup = homepageDocument, let url = urlForFindingAvatar { + debugPrint("refreshIcon: Trying to fetch og:image as feed avatar from \(url)") + feedAvatar = try await FeedUtils.findAvatarFromHTMLOGImage( + htmlDocument: soup, + htmlURL: url + ) + var avatarIsSquare = true + if let imageData = feedAvatar, let feedAvatarImage = NSImage(data: imageData) { + avatarIsSquare = feedAvatarImage.size.width == feedAvatarImage.size.height + } + if feedAvatar == nil || !avatarIsSquare { + debugPrint( + "refreshIcon: Trying to fetch icons from links as feed avatar from \(url)" + ) + feedAvatar = try await FeedUtils.findAvatarFromHTMLIcons( + htmlDocument: soup, + htmlURL: feedURL + ) + } + if feedAvatar == nil { + debugPrint("refreshIcon: avatar not found for \(feedURL)") + } + else { + if let data = feedAvatar, let image = NSImage(data: data), + let _ = try? data.write(to: self.avatarPath) + { + DispatchQueue.main.async { + self.avatar = image + } + debugPrint( + "refreshIcon: written avatar for \(self.name) to \(self.avatarPath)" + ) + } + } + } + else { + debugPrint("refreshIcon: no soup") + } + } + catch { + debugPrint("refreshIcon error: \(error)") + } + default: + break + } + } + func save() throws { try JSONEncoder.shared.encode(self).write(to: infoPath) } diff --git a/Planet/Views/Sidebar/FollowingPlanetSidebarItem.swift b/Planet/Views/Sidebar/FollowingPlanetSidebarItem.swift index 17d98c35..0cd48280 100644 --- a/Planet/Views/Sidebar/FollowingPlanetSidebarItem.swift +++ b/Planet/Views/Sidebar/FollowingPlanetSidebarItem.swift @@ -57,6 +57,24 @@ struct FollowingPlanetSidebarItem: View { Divider() + #if DEBUG + Button { + Task { + try await planet.refreshIcon() + } + } label: { + Text("Refresh Icon") + } + + Button { + Task { + try await planet.removeIcon() + } + } label: { + Text("Remove Icon") + } + #endif + Button { isShowingArchiveConfirmation = true } label: { diff --git a/Planet/versioning.xcconfig b/Planet/versioning.xcconfig index c087a997..4e56c524 100644 --- a/Planet/versioning.xcconfig +++ b/Planet/versioning.xcconfig @@ -1 +1 @@ -CURRENT_PROJECT_VERSION = 1833 +CURRENT_PROJECT_VERSION = 1834