commit cabe3cf791bd99e5ddb56b7ee9698d3cf3a2f1f7
parent c1c832fab717245073c7ae136dd27f72fb8d9d62
Author: Sam DuBois <sdubois@umass.edu>
Date: Tue, 27 Dec 2022 18:04:12 -0700
Show Link Previews
Closes: #170
Changelog-Added: Link Previews!
Diffstat:
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift
@@ -66,7 +66,7 @@ struct NoteContentView: View {
@State var artifacts: NoteArtifacts
- @State var metaData: [LPLinkMetadata] = []
+ @State var metaData: LPLinkMetadata? = nil
let size: EventViewKind
func MainContent() -> some View {
@@ -96,20 +96,12 @@ struct NoteContentView: View {
InvoicesView(invoices: artifacts.invoices)
}
- ForEach(artifacts.links, id:\.self) { link in
- LinkViewRepresentable(url: link)
- .frame(height: 50)
- }
- }
- }
-
- func getMetaData() async {
- let provider = LPMetadataProvider()
-
- if artifacts.links.count > 0 {
- if let metaData = try? await provider.startFetchingMetadata(for: artifacts.links.first!) {
- DispatchQueue.main.async {
- self.metaData.append(metaData)
+ if show_images, self.metaData != nil {
+ LinkViewRepresentable(metadata: self.metaData)
+ } else {
+ ForEach(artifacts.links, id:\.self) { link in
+ LinkViewRepresentable(url: link)
+ .frame(height: 50)
}
}
}
@@ -117,6 +109,7 @@ struct NoteContentView: View {
var body: some View {
MainContent()
+ .animation(.easeInOut, value: metaData)
.onAppear() {
self.artifacts = render_note_content(ev: event, profiles: profiles, privkey: privkey)
}
@@ -136,6 +129,21 @@ struct NoteContentView: View {
}
}
}
+ .task {
+ if show_images, artifacts.links.count == 1 {
+ self.metaData = await getMetaData(for: artifacts.links.first!)
+ }
+ }
+ }
+
+ func getMetaData(for url: URL) async -> LPLinkMetadata? {
+ let provider = LPMetadataProvider()
+
+ do {
+ return try await provider.startFetchingMetadata(for: url)
+ } catch {
+ return nil
+ }
}
}