damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 16e3c4e1cf526f8c256705fe6009085409490e88
parent b3b8a708f3a9c1a05b6088e9c5053465371c2295
Author: William Casarin <jb55@jb55.com>
Date:   Tue,  3 Jan 2023 11:42:23 -0800

Fix crash with link previews

Changelog-Fixed: Fixed crash with link previews

Diffstat:
Mdamus/Util/LinkView.swift | 24+++++++++++-------------
Mdamus/Views/NoteContentView.swift | 8+++++---
2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/damus/Util/LinkView.swift b/damus/Util/LinkView.swift @@ -12,26 +12,24 @@ class CustomLinkView: LPLinkView { override var intrinsicContentSize: CGSize { CGSize(width: 0, height: super.intrinsicContentSize.height) } } +enum Metadata { + case linkmeta(LPLinkMetadata) + case url(URL) +} + struct LinkViewRepresentable: UIViewRepresentable { typealias UIViewType = CustomLinkView - var metadata: LPLinkMetadata? - var url: URL? + let meta: Metadata func makeUIView(context: Context) -> CustomLinkView { - - if let metadata { - let linkView = CustomLinkView(metadata: metadata) - return linkView - } - - if let url { - let linkView = CustomLinkView(url: url) - return linkView + switch meta { + case .linkmeta(let linkmeta): + return CustomLinkView(metadata: linkmeta) + case .url(let url): + return CustomLinkView(url: url) } - - return CustomLinkView() } func updateUIView(_ uiView: CustomLinkView, context: Context) { diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift @@ -94,8 +94,10 @@ struct NoteContentView: View { self.preview } else { ForEach(artifacts.links, id:\.self) { link in - LinkViewRepresentable(url: link) - .frame(height: 50) + if let url = link { + LinkViewRepresentable(meta: .url(url)) + .frame(height: 50) + } } } } @@ -136,7 +138,7 @@ struct NoteContentView: View { if show_images, artifacts.links.count == 1 { let meta = await getMetaData(for: artifacts.links.first!) - let view = LinkViewRepresentable(metadata: meta) + let view = meta.map { LinkViewRepresentable(meta: .linkmeta($0)) } previews.store(evid: self.event.id, preview: view) self.preview = view }