2
2
//
3
3
// This source file is part of the SwiftAWSLambdaRuntime open source project
4
4
//
5
- // Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5
+ // Copyright (c) 2020-2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6
6
// Licensed under Apache License v2.0
7
7
//
8
8
// See LICENSE.txt for license information
@@ -19,30 +19,43 @@ struct ContentView: View {
19
19
@State var name : String = " "
20
20
@State var password : String = " "
21
21
@State var response : String = " "
22
+ @State private var isLoading : Bool = false
22
23
23
24
var body : some View {
24
25
VStack ( alignment: . leading, spacing: 20 ) {
25
26
TextField ( " Username " , text: $name)
26
27
SecureField ( " Password " , text: $password)
27
- Button (
28
- action: {
29
- Task {
30
- await self . register ( )
28
+ let inputIncomplete = name. isEmpty || password. isEmpty
29
+ Button {
30
+ Task {
31
+ isLoading = true
32
+ do {
33
+ response = try await self . register ( )
34
+ } catch {
35
+ response = error. localizedDescription
31
36
}
32
- } ,
33
- label: {
34
- Text ( " Register " )
35
- . padding ( )
36
- . foregroundColor ( . white)
37
- . background ( Color . black)
38
- . border ( Color . black, width: 2 )
37
+ isLoading = false
39
38
}
40
- )
39
+ } label: {
40
+ Text ( " Register " )
41
+ . padding ( )
42
+ . foregroundColor ( . white)
43
+ . background ( . black)
44
+ . border ( . black, width: 2 )
45
+ . opacity ( isLoading ? 0 : 1 )
46
+ . overlay {
47
+ if isLoading {
48
+ ProgressView ( )
49
+ }
50
+ }
51
+ }
52
+ . disabled ( inputIncomplete || isLoading)
53
+ . opacity ( inputIncomplete ? 0.5 : 1 )
41
54
Text ( response)
42
55
} . padding ( 100 )
43
56
}
44
57
45
- func register( ) async {
58
+ func register( ) async throws -> String {
46
59
guard let url = URL ( string: " http://127.0.0.1:7000/invoke " ) else {
47
60
fatalError ( " invalid url " )
48
61
}
@@ -54,27 +67,17 @@ struct ContentView: View {
54
67
}
55
68
request. httpBody = jsonRequest
56
69
57
- do {
58
- let ( data, response) = try await URLSession . shared. data ( for: request)
59
-
60
- guard let httpResponse = response as? HTTPURLResponse else {
61
- throw CommunicationError ( reason: " invalid response, expected HTTPURLResponse " )
62
- }
63
- guard httpResponse. statusCode == 200 else {
64
- throw CommunicationError ( reason: " invalid response code: \( httpResponse. statusCode) " )
65
- }
66
- let jsonResponse = try JSONDecoder ( ) . decode ( Response . self, from: data)
70
+ let ( data, response) = try await URLSession . shared. data ( for: request)
67
71
68
- self . response = jsonResponse. message
69
- } catch {
70
- self . response = error. localizedDescription
72
+ guard let httpResponse = response as? HTTPURLResponse else {
73
+ throw CommunicationError ( reason: " Invalid response, expected HTTPURLResponse. " )
71
74
}
72
- }
73
-
74
- func setResponse( _ text: String ) {
75
- DispatchQueue . main. async {
76
- self . response = text
75
+ guard httpResponse. statusCode == 200 else {
76
+ throw CommunicationError ( reason: " Invalid response code: \( httpResponse. statusCode) " )
77
77
}
78
+
79
+ let jsonResponse = try JSONDecoder ( ) . decode ( Response . self, from: data)
80
+ return jsonResponse. message
78
81
}
79
82
}
80
83
@@ -84,6 +87,9 @@ struct ContentView_Previews: PreviewProvider {
84
87
}
85
88
}
86
89
87
- struct CommunicationError : Error {
90
+ struct CommunicationError : LocalizedError {
88
91
let reason : String
92
+ var errorDescription : String ? {
93
+ self . reason
94
+ }
89
95
}
0 commit comments