1
1
//
2
2
// OpenAIAsyncImage.swift
3
- //
3
+ //
4
4
//
5
5
// Created by Igor on 18.02.2023.
6
6
//
@@ -13,44 +13,44 @@ fileprivate typealias ImageSize = OpenAIImageSize
13
13
@available ( iOS 15 . 0 , macOS 12 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
14
14
public struct OpenAIAsyncImage < Content: View , T: IOpenAILoader > : View {
15
15
16
- /// Custom view builder tpl
16
+ /// Custom view builder template type alias
17
17
public typealias ImageProcess = ( ImageState ) -> Content
18
18
19
- /// Default loader
19
+ /// Default loader, injected from environment
20
20
@Environment ( \. openAIDefaultLoader) var defaultLoader : OpenAIDefaultLoader
21
21
22
22
// MARK: - Private properties
23
23
24
- /// OpenAI image
24
+ /// State variable to hold the OpenAI image
25
25
@State private var image : Image ?
26
26
27
- /// Error
27
+ /// State variable to hold any errors encountered during loading
28
28
@State private var error : Error ?
29
29
30
- /// Current task
30
+ /// State variable to hold the current task responsible for loading the image
31
31
@State private var task : Task < Void , Never > ?
32
32
33
33
// MARK: - Config
34
34
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
36
36
@Binding var prompt : String
37
37
38
- /// Custom loader
38
+ /// Optional custom loader conforming to `IOpenAILoader` protocol
39
39
let loader : T ?
40
40
41
- /// Image size
41
+ /// The size of the image to be generated
42
42
let size : OpenAIImageSize
43
43
44
- /// Custom view builder tpl
44
+ /// Optional custom view builder template
45
45
let tpl : ImageProcess ?
46
46
47
- // MARK: - Life circle
47
+ // MARK: - Life cycle
48
48
49
49
/// - Parameters:
50
50
/// - prompt: A text description of the desired image(s). The maximum length is 1000 characters
51
51
/// - 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`
54
54
public init (
55
55
prompt : Binding < String > ,
56
56
size : OpenAIImageSize = . dpi256,
@@ -86,9 +86,9 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
86
86
}
87
87
}
88
88
89
- // MARK: - Private
89
+ // MARK: - Private methods
90
90
91
- /// - Returns: Current image state status
91
+ /// - Returns: The current image state status
92
92
private func getState ( ) -> ImageState {
93
93
94
94
if let image { return . loaded( image) }
@@ -97,17 +97,20 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
97
97
return . loading
98
98
}
99
99
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
101
104
/// - Returns: OpenAI image
102
105
private func loadImageDefault( _ prompt : String , with size : ImageSize ) async throws -> Image {
103
106
try await defaultLoader. load ( prompt, with: size)
104
107
}
105
108
106
- /// Load image by text
109
+ /// Load image using the provided or default loader
107
110
/// - 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
111
114
private func loadImage( _ prompt : String , with size : ImageSize ) async -> Image ? {
112
115
do {
113
116
if let loader = loader{
@@ -124,25 +127,28 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
124
127
}
125
128
}
126
129
127
- /// - Parameter value: OpenAI image
130
+ /// Sets the image on the main thread
131
+ /// - Parameter value: The image to be set
128
132
@MainActor
129
133
private func setImage( _ value : Image ) {
130
134
image = value
131
135
}
132
136
133
- /// Clear properties
137
+ /// Clears the image and error state properties
134
138
@MainActor
135
139
private func clear( ) {
136
140
image = nil
137
141
error = nil
138
142
}
139
143
144
+ /// Cancels the current loading task if any
140
145
private func cancelTask( ) {
141
146
task? . cancel ( )
142
147
task = nil
143
148
}
144
149
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
146
152
private func getTask( ) -> Task < Void , Never > {
147
153
Task {
148
154
if let image = await loadImage ( prompt, with: size) {
@@ -152,13 +158,14 @@ public struct OpenAIAsyncImage<Content: View, T: IOpenAILoader>: View {
152
158
}
153
159
}
154
160
155
- // MARK: - Extension public -
161
+ // MARK: - Public extensions -
156
162
157
163
public extension OpenAIAsyncImage where Content == EmptyView , T == OpenAIDefaultLoader {
158
164
165
+ /// Convenience initializer for default loader without custom view template
159
166
/// - 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
162
169
init (
163
170
prompt : Binding < String > ,
164
171
size : OpenAIImageSize = . dpi256
@@ -172,10 +179,11 @@ public extension OpenAIAsyncImage where Content == EmptyView, T == OpenAIDefault
172
179
173
180
public extension OpenAIAsyncImage where T == OpenAIDefaultLoader {
174
181
182
+ /// Convenience initializer for default loader with custom view template
175
183
/// - 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
179
187
init (
180
188
prompt : Binding < String > ,
181
189
size : OpenAIImageSize = . dpi256,
@@ -188,7 +196,7 @@ public extension OpenAIAsyncImage where T == OpenAIDefaultLoader{
188
196
}
189
197
}
190
198
191
- // MARK: - File private -
199
+ // MARK: - File private functions -
192
200
193
201
@ViewBuilder
194
202
fileprivate func imageTpl( _ state : ImageState ) -> some View {
0 commit comments