Browse Source

Fix crash with link previews

Changelog-Fixed: Fixed crash with link previews
post-button-style
William Casarin 2 years ago
parent
commit
16e3c4e1cf
  1. 24
      damus/Util/LinkView.swift
  2. 6
      damus/Views/NoteContentView.swift

24
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
switch meta {
case .linkmeta(let linkmeta):
return CustomLinkView(metadata: linkmeta)
case .url(let url):
return CustomLinkView(url: url)
}
if let url {
let linkView = CustomLinkView(url: url)
return linkView
}
return CustomLinkView()
}
func updateUIView(_ uiView: CustomLinkView, context: Context) {

6
damus/Views/NoteContentView.swift

@ -94,12 +94,14 @@ struct NoteContentView: View {
self.preview
} else {
ForEach(artifacts.links, id:\.self) { link in
LinkViewRepresentable(url: link)
if let url = link {
LinkViewRepresentable(meta: .url(url))
.frame(height: 50)
}
}
}
}
}
var body: some View {
MainContent()
@ -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
}

Loading…
Cancel
Save