1
1
import SwiftUI
2
+ import PhotosUI
2
3
3
4
struct VisionView : View {
4
- var viewModel : VisionViewModel
5
+ @State private var visionStrategy = 0
6
+ @State var viewModel : VisionViewModel
5
7
6
8
var body : some View {
7
9
VStack {
8
- AsyncImage ( url: URL ( string: viewModel. imageVisionURL) ) { image in
9
- image
10
- . resizable ( )
11
- . scaledToFit ( )
12
- . frame ( width: 300 , height: 300 )
13
- } placeholder: {
14
- ProgressView ( )
10
+ Picker ( " What is your favorite color? " , selection: $visionStrategy) {
11
+ Text ( " URL " ) . tag ( 0 )
12
+ Text ( " Gallery " ) . tag ( 1 )
13
+ }
14
+ . pickerStyle ( . segmented)
15
+
16
+ if visionStrategy == 0 {
17
+ AsyncImage ( url: URL ( string: viewModel. imageVisionURL) ) { image in
18
+ image
19
+ . resizable ( )
20
+ . scaledToFit ( )
21
+ . frame ( width: 300 , height: 300 )
22
+ } placeholder: {
23
+ ProgressView ( )
24
+ . padding ( . bottom, 20 )
25
+ }
26
+ } else {
27
+ PhotosPicker ( selection: $viewModel. photoSelection,
28
+ matching: . images,
29
+ photoLibrary: . shared( ) ) {
30
+ Label ( " Add video or audio " ,
31
+ systemImage: " video.fill " )
32
+ }
33
+ . frame ( height: 300 )
34
+ . photosPickerStyle ( . inline)
35
+ . onChange ( of: viewModel. photoSelection!) { oldValue, newValue in
36
+ newValue. loadTransferable ( type: Data . self) { [ self ] result in
37
+ switch result {
38
+ case . success( let data) :
39
+ if let data {
40
+ viewModel. currentData = data
41
+ } else {
42
+ print ( " No supported content type found. " )
43
+ }
44
+ case . failure( let error) :
45
+ fatalError ( error. localizedDescription)
46
+ }
47
+ }
48
+ }
15
49
}
16
50
17
51
if !viewModel. isLoading {
@@ -20,18 +54,22 @@ struct VisionView: View {
20
54
await viewModel. send ( message: " Please analyze the image and describe its contents, providing any relevant details or information " )
21
55
}
22
56
} , label: {
23
- Text ( " Describe Image " )
57
+ Text ( " Describe Image from URL " )
24
58
} )
25
59
. buttonStyle ( . borderedProminent)
26
60
} else {
27
61
ProgressView ( )
28
62
}
29
63
30
- TextEditor ( text: . constant( viewModel. message) )
64
+ Divider ( )
65
+ . padding ( . top, 20 )
66
+
67
+ TextEditor ( text: . constant( viewModel. message) )
31
68
. font ( . body)
32
69
. padding ( . top, 12 )
33
70
. padding ( . horizontal)
34
71
}
72
+ . padding ( . horizontal, 32 )
35
73
}
36
74
}
37
75
0 commit comments