Skip to content

Commit 97c6101

Browse files
committed
ImageCreationView now uses LinkPresentation
1 parent 1133061 commit 97c6101

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
import SwiftUI
99
import OpenAI
10-
import SafariServices
1110

1211
public struct ImageCreationView: View {
1312
@ObservedObject var store: ImageStore
1413

1514
@State private var prompt: String = ""
1615
@State private var n: Int = 1
1716
@State private var size: String
18-
@State private var showSafari = false
1917

2018
private var sizes = ["256x256", "512x512", "1024x1024"]
2119

@@ -61,14 +59,8 @@ public struct ImageCreationView: View {
6159
ForEach($store.images, id: \.self) { image in
6260
let urlString = image.wrappedValue.url
6361
if let imageURL = URL(string: urlString), UIApplication.shared.canOpenURL(imageURL) {
64-
Button {
65-
showSafari.toggle()
66-
} label: {
67-
Text(urlString)
68-
.foregroundStyle(.foreground)
69-
}.fullScreenCover(isPresented: $showSafari, content: {
70-
SafariView(url: imageURL)
71-
})
62+
LinkPreview(previewURL: imageURL)
63+
.aspectRatio(contentMode: .fit)
7264
} else {
7365
Text(urlString)
7466
.foregroundStyle(.secondary)
@@ -81,13 +73,3 @@ public struct ImageCreationView: View {
8173
.navigationTitle("Create Image")
8274
}
8375
}
84-
85-
private struct SafariView: UIViewControllerRepresentable {
86-
var url: URL
87-
88-
func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
89-
SFSafariViewController(url: url)
90-
}
91-
92-
func updateUIViewController(_ safariViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) { }
93-
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// LinkPreview.swift
3+
// DemoChat
4+
//
5+
// Created by Aled Samuel on 25/04/2023.
6+
//
7+
8+
import SwiftUI
9+
import LinkPresentation
10+
11+
struct LinkPreview: UIViewRepresentable {
12+
typealias UIViewType = LPLinkView
13+
14+
var previewURL: URL
15+
16+
func makeUIView(context: Context) -> LPLinkView {
17+
LPLinkView(url: previewURL)
18+
}
19+
20+
func updateUIView(_ uiView: UIViewType, context: Context) {
21+
LPMetadataProvider().startFetchingMetadata(for: previewURL) { metadata, error in
22+
if let error = error {
23+
print(error.localizedDescription)
24+
return
25+
}
26+
guard let metadata = metadata else {
27+
print("Metadata missing for \(previewURL.absoluteString)")
28+
return
29+
}
30+
uiView.metadata = metadata
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)