Skip to content

Commit 328d126

Browse files
committed
Update OpenAIAsyncImage.swift
1 parent eb1a284 commit 328d126

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

Sources/openai-async-image-swiftui/OpenAIAsyncImage.swift

+38-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// OpenAIAsyncImage.swift
3-
//
3+
//
44
//
55
// Created by Igor on 18.02.2023.
66
//
@@ -13,44 +13,44 @@ fileprivate typealias ImageSize = OpenAIImageSize
1313
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
1414
public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
1515

16-
/// Custom view builder tpl
16+
/// Custom view builder template type alias
1717
public typealias ImageProcess = (ImageState) -> Content
1818

19-
/// Default loader
19+
/// Default loader, injected from environment
2020
@Environment(\.openAIDefaultLoader) var defaultLoader : OpenAIDefaultLoader
2121

2222
// MARK: - Private properties
2323

24-
/// OpenAI image
24+
/// State variable to hold the OpenAI image
2525
@State private var image: Image?
2626

27-
/// Error
27+
/// State variable to hold any errors encountered during loading
2828
@State private var error: Error?
2929

30-
/// Current task
30+
/// State variable to hold the current task responsible for loading the image
3131
@State private var task : Task<Void, Never>?
3232

3333
// MARK: - Config
3434

35-
/// A text description of the desired image(s). The maximum length is 1000 characters
35+
/// A binding to the text prompt describing the desired image. The maximum length is 1000 characters
3636
@Binding var prompt : String
3737

38-
/// Custom loader
38+
/// Optional custom loader conforming to `IOpenAILoader` protocol
3939
let loader : T?
4040

41-
/// Image size
41+
/// The size of the image to be generated
4242
let size : OpenAIImageSize
4343

44-
/// Custom view builder tpl
44+
/// Optional custom view builder template
4545
let tpl : ImageProcess?
4646

47-
// MARK: - Life circle
47+
// MARK: - Life cycle
4848

4949
/// - Parameters:
5050
/// - prompt: A text description of the desired image(s). The maximum length is 1000 characters
5151
/// - size: The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024
52-
/// - tpl: Custom view builder tpl
53-
/// - loader: Custom loader
52+
/// - tpl: Custom view builder template
53+
/// - loader: Custom loader conforming to `IOpenAILoader`
5454
public init(
5555
prompt : Binding<String>,
5656
size : OpenAIImageSize = .dpi256,
@@ -86,9 +86,9 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
8686
}
8787
}
8888

89-
// MARK: - Private
89+
// MARK: - Private methods
9090

91-
/// - Returns: Current image state status
91+
/// - Returns: The current image state status
9292
private func getState () -> ImageState{
9393

9494
if let image { return .loaded(image) }
@@ -97,17 +97,20 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
9797
return .loading
9898
}
9999

100-
/// Load using default loader
100+
/// Load using the default loader
101+
/// - Parameters:
102+
/// - prompt: The text prompt for generating the image
103+
/// - size: The desired size of the image
101104
/// - Returns: OpenAI image
102105
private func loadImageDefault(_ prompt : String, with size : ImageSize) async throws -> Image{
103106
try await defaultLoader.load(prompt, with: size)
104107
}
105108

106-
/// Load image by text
109+
/// Load image using the provided or default loader
107110
/// - Parameters:
108-
/// - prompt: Text
109-
/// - size: Image size
110-
/// - Returns: Open AI Image
111+
/// - prompt: The text prompt for generating the image
112+
/// - size: The desired size of the image
113+
/// - Returns: OpenAI image if successful, otherwise nil
111114
private func loadImage(_ prompt : String, with size : ImageSize) async -> Image?{
112115
do{
113116
if let loader = loader{
@@ -124,25 +127,28 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
124127
}
125128
}
126129

127-
/// - Parameter value: OpenAI image
130+
/// Sets the image on the main thread
131+
/// - Parameter value: The image to be set
128132
@MainActor
129133
private func setImage(_ value : Image){
130134
image = value
131135
}
132136

133-
/// Clear properties
137+
/// Clears the image and error state properties
134138
@MainActor
135139
private func clear(){
136140
image = nil
137141
error = nil
138142
}
139143

144+
/// Cancels the current loading task if any
140145
private func cancelTask(){
141146
task?.cancel()
142147
task = nil
143148
}
144149

145-
/// - Returns: Task to fetch OpenAI image
150+
/// Creates and returns a task to fetch the OpenAI image
151+
/// - Returns: A task that fetches the OpenAI image
146152
private func getTask() -> Task<Void, Never>{
147153
Task{
148154
if let image = await loadImage(prompt, with: size){
@@ -152,13 +158,14 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
152158
}
153159
}
154160

155-
// MARK: - Extension public -
161+
// MARK: - Public extensions -
156162

157163
public extension OpenAIAsyncImage where Content == EmptyView, T == OpenAIDefaultLoader{
158164

165+
/// Convenience initializer for default loader without custom view template
159166
/// - Parameters:
160-
/// - prompt: Text
161-
/// - size: Image size
167+
/// - prompt: The text prompt for generating the image
168+
/// - size: The desired size of the image
162169
init(
163170
prompt : Binding<String>,
164171
size : OpenAIImageSize = .dpi256
@@ -172,10 +179,11 @@ public extension OpenAIAsyncImage where Content == EmptyView, T == OpenAIDefault
172179

173180
public extension OpenAIAsyncImage where T == OpenAIDefaultLoader{
174181

182+
/// Convenience initializer for default loader with custom view template
175183
/// - Parameters:
176-
/// - prompt: Text
177-
/// - size: Image size
178-
/// - tpl: View tpl
184+
/// - prompt: The text prompt for generating the image
185+
/// - size: The desired size of the image
186+
/// - tpl: Custom view template
179187
init(
180188
prompt : Binding<String>,
181189
size : OpenAIImageSize = .dpi256,
@@ -188,7 +196,7 @@ public extension OpenAIAsyncImage where T == OpenAIDefaultLoader{
188196
}
189197
}
190198

191-
// MARK: - File private -
199+
// MARK: - File private functions -
192200

193201
@ViewBuilder
194202
fileprivate func imageTpl(_ state : ImageState) -> some View{

0 commit comments

Comments
 (0)