-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Create Image example to Demo app
- Loading branch information
1 parent
f0e1709
commit fbc9129
Showing
6 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ImageStore.swift | ||
// DemoChat | ||
// | ||
// Created by Aled Samuel on 24/04/2023. | ||
// | ||
|
||
import Foundation | ||
import OpenAI | ||
|
||
public final class ImageStore: ObservableObject { | ||
public var openAIClient: OpenAIProtocol | ||
|
||
@Published var images: [ImagesResult.URLResult] = [] | ||
|
||
public init( | ||
openAIClient: OpenAIProtocol | ||
) { | ||
self.openAIClient = openAIClient | ||
} | ||
|
||
@MainActor | ||
func images(query: ImagesQuery) async { | ||
images.removeAll() | ||
do { | ||
let response = try await openAIClient.images(query: query) | ||
images = response.data | ||
} catch { | ||
// TODO: Better error handling | ||
print(error.localizedDescription) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// ImageCreationView.swift | ||
// DemoChat | ||
// | ||
// Created by Aled Samuel on 24/04/2023. | ||
// | ||
|
||
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"] | ||
|
||
public init(store: ImageStore) { | ||
self.store = store | ||
size = sizes[0] | ||
} | ||
|
||
public var body: some View { | ||
List { | ||
Section { | ||
HStack { | ||
Text("Prompt") | ||
Spacer() | ||
TextEditor(text: $prompt) | ||
.multilineTextAlignment(.trailing) | ||
} | ||
HStack { | ||
Stepper("Amount: \(n)", value: $n, in: 1...10) | ||
} | ||
HStack { | ||
Picker("Size", selection: $size) { | ||
ForEach(sizes, id: \.self) { | ||
Text($0) | ||
} | ||
} | ||
} | ||
} | ||
Section { | ||
HStack { | ||
Button("Create Image" + (n == 1 ? "" : "s")) { | ||
Task { | ||
let query = ImagesQuery(prompt: prompt, n: n, size: size) | ||
await store.images(query: query) | ||
} | ||
} | ||
.foregroundColor(.accentColor) | ||
Spacer() | ||
} | ||
} | ||
if !$store.images.isEmpty { | ||
Section("Images") { | ||
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) | ||
}) | ||
} else { | ||
Text(urlString) | ||
.foregroundStyle(.secondary) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.listStyle(.insetGrouped) | ||
.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>) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// ImageView.swift | ||
// DemoChat | ||
// | ||
// Created by Aled Samuel on 24/04/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
public struct ImageView: View { | ||
@ObservedObject var store: ImageStore | ||
|
||
public init(store: ImageStore) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
NavigationStack { | ||
List { | ||
NavigationLink("Create Image", destination: ImageCreationView(store: store)) | ||
NavigationLink("Create Image Edit", destination: ImageEditView(store: store)) | ||
.disabled(true) | ||
NavigationLink("Create Image Variation", destination: ImageVariationView(store: store)) | ||
.disabled(true) | ||
|
||
} | ||
.listStyle(.insetGrouped) | ||
.navigationTitle("Image") | ||
} | ||
} | ||
} | ||
|
||
public struct ImageEditView: View { | ||
@ObservedObject var store: ImageStore | ||
|
||
public init(store: ImageStore) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
List { | ||
|
||
} | ||
.listStyle(.insetGrouped) | ||
.navigationTitle("Create Image Edit") | ||
} | ||
} | ||
|
||
public struct ImageVariationView: View { | ||
@ObservedObject var store: ImageStore | ||
|
||
public init(store: ImageStore) { | ||
self.store = store | ||
} | ||
|
||
public var body: some View { | ||
List { | ||
|
||
} | ||
.listStyle(.insetGrouped) | ||
.navigationTitle("Create Image Variation") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters