22//
33// This source file is part of the SwiftAWSLambdaRuntime open source project
44//
5- // Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5+ // Copyright (c) 2020-2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
66// Licensed under Apache License v2.0
77//
88// See LICENSE.txt for license information
@@ -19,30 +19,43 @@ struct ContentView: View {
1919 @State var name : String = " "
2020 @State var password : String = " "
2121 @State var response : String = " "
22+ @State private var isLoading : Bool = false
2223
2324 var body : some View {
2425 VStack ( alignment: . leading, spacing: 20 ) {
2526 TextField ( " Username " , text: $name)
2627 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
3136 }
32- } ,
33- label: {
34- Text ( " Register " )
35- . padding ( )
36- . foregroundColor ( . white)
37- . background ( Color . black)
38- . border ( Color . black, width: 2 )
37+ isLoading = false
3938 }
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 )
4154 Text ( response)
4255 } . padding ( 100 )
4356 }
4457
45- func register( ) async {
58+ func register( ) async throws -> String {
4659 guard let url = URL ( string: " http://127.0.0.1:7000/invoke " ) else {
4760 fatalError ( " invalid url " )
4861 }
@@ -54,27 +67,17 @@ struct ContentView: View {
5467 }
5568 request. httpBody = jsonRequest
5669
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)
6771
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. " )
7174 }
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) " )
7777 }
78+
79+ let jsonResponse = try JSONDecoder ( ) . decode ( Response . self, from: data)
80+ return jsonResponse. message
7881 }
7982}
8083
@@ -84,6 +87,9 @@ struct ContentView_Previews: PreviewProvider {
8487 }
8588}
8689
87- struct CommunicationError : Error {
90+ struct CommunicationError : LocalizedError {
8891 let reason : String
92+ var errorDescription : String ? {
93+ self . reason
94+ }
8995}
0 commit comments