Skip to content

Commit

Permalink
ImageCreationView now uses LinkPresentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SunburstEnzo committed Apr 25, 2023
1 parent fbc9129 commit 9898058
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
22 changes: 2 additions & 20 deletions Demo/DemoChat/Sources/UI/Images/ImageCreationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

import SwiftUI
import OpenAI
import SafariServices

public struct ImageCreationView: View {
@ObservedObject var store: ImageStore

@State private var prompt: String = ""
@State private var n: Int = 1
@State private var size: String
@State private var showSafari = false

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

Expand Down Expand Up @@ -61,14 +59,8 @@ public struct ImageCreationView: View {
ForEach($store.images, id: \.self) { image in
let urlString = image.wrappedValue.url
if let imageURL = URL(string: urlString), UIApplication.shared.canOpenURL(imageURL) {
Button {
showSafari.toggle()
} label: {
Text(urlString)
.foregroundStyle(.foreground)
}.fullScreenCover(isPresented: $showSafari, content: {
SafariView(url: imageURL)
})
LinkPreview(previewURL: imageURL)
.aspectRatio(contentMode: .fit)
} else {
Text(urlString)
.foregroundStyle(.secondary)
Expand All @@ -81,13 +73,3 @@ public struct ImageCreationView: View {
.navigationTitle("Create Image")
}
}

private struct SafariView: UIViewControllerRepresentable {
var url: URL

func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
SFSafariViewController(url: url)
}

func updateUIViewController(_ safariViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) { }
}
33 changes: 33 additions & 0 deletions Demo/DemoChat/Sources/UI/Images/LinkPreview.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// LinkPreview.swift
// DemoChat
//
// Created by Aled Samuel on 25/04/2023.
//

import SwiftUI
import LinkPresentation

struct LinkPreview: UIViewRepresentable {
typealias UIViewType = LPLinkView

var previewURL: URL

func makeUIView(context: Context) -> LPLinkView {
LPLinkView(url: previewURL)
}

func updateUIView(_ uiView: UIViewType, context: Context) {
LPMetadataProvider().startFetchingMetadata(for: previewURL) { metadata, error in
if let error = error {
print(error.localizedDescription)
return
}
guard let metadata = metadata else {
print("Metadata missing for \(previewURL.absoluteString)")
return
}
uiView.metadata = metadata
}
}
}

0 comments on commit 9898058

Please sign in to comment.