From da9ca78a8be69fb341e3ca1cbdd4558cc149671d Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 21 Jul 2024 18:40:58 -0700 Subject: [PATCH] Update card view less often Speculative fix for #314 --- .../Appearance/MockStatusView.swift | 7 ++-- Tusker/Views/Status/StatusCardView.swift | 39 ++++++++++++++++--- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Tusker/Screens/Preferences/Appearance/MockStatusView.swift b/Tusker/Screens/Preferences/Appearance/MockStatusView.swift index 5c313ae1..5cbd46e2 100644 --- a/Tusker/Screens/Preferences/Appearance/MockStatusView.swift +++ b/Tusker/Screens/Preferences/Appearance/MockStatusView.swift @@ -135,12 +135,11 @@ private struct MockStatusCardView: UIViewRepresentable { func makeUIView(context: Context) -> StatusCardView { let view = StatusCardView() view.isUserInteractionEnabled = false - let card = Card( + let card = StatusCardView.CardData( url: WebURL("https://vaccor.space/tusker")!, - title: "Tusker", - description: "Tusker is an iOS app for Mastodon", image: WebURL("https://vaccor.space/tusker/img/icon.png")!, - kind: .link + title: "Tusker", + description: "Tusker is an iOS app for Mastodon" ) view.updateUI(card: card, sensitive: false) return view diff --git a/Tusker/Views/Status/StatusCardView.swift b/Tusker/Views/Status/StatusCardView.swift index b8f7d6bb..a8feb6bd 100644 --- a/Tusker/Views/Status/StatusCardView.swift +++ b/Tusker/Views/Status/StatusCardView.swift @@ -9,6 +9,7 @@ import UIKit import Pachyderm import SafariServices +import WebURL import WebURLFoundationExtras import HTMLStreamer @@ -18,7 +19,7 @@ class StatusCardView: UIView { weak var actionProvider: MenuActionProvider? private var statusID: String? - private(set) var card: Card? + private(set) var card: CardData? private static let activeBackgroundColor = UIColor.secondarySystemFill private static let inactiveBackgroundColor = UIColor.secondarySystemBackground @@ -163,20 +164,22 @@ class StatusCardView: UIView { } func updateUI(status: StatusMO) { - guard status.id != statusID else { + let newData = status.card.map { CardData(card: $0) } + guard self.card != newData else { return } - self.card = status.card + self.card = newData self.statusID = status.id - guard let card = status.card else { + guard let newData else { return } - updateUI(card: card, sensitive: status.sensitive) + updateUI(card: newData, sensitive: status.sensitive) } - func updateUI(card: Card, sensitive: Bool) { + // This method is internal for use by MockStatusView + func updateUI(card: CardData, sensitive: Bool) { if let image = card.image { if sensitive { if let blurhash = card.blurhash { @@ -243,6 +246,30 @@ class StatusCardView: UIView { hStack.backgroundColor = StatusCardView.inactiveBackgroundColor setNeedsDisplay() } + + struct CardData: Equatable { + let url: WebURL + let image: WebURL? + let title: String + let description: String + let blurhash: String? + + init(card: Card) { + self.url = card.url + self.image = card.image + self.title = card.title + self.description = card.description + self.blurhash = card.blurhash + } + + init(url: WebURL, image: WebURL? = nil, title: String, description: String, blurhash: String? = nil) { + self.url = url + self.image = image + self.title = title + self.description = description + self.blurhash = blurhash + } + } }