Skip to content

Commit 39b34a1

Browse files
authored
Modernize app example (#231)
Since we already required Swift 5.5 for Concurrency (Xcode 13), it's natural to update the demo app to reflect latest SwiftUI changes, which makes the code neater and more expressive.
1 parent 011e8dd commit 39b34a1

File tree

12 files changed

+153
-389
lines changed

12 files changed

+153
-389
lines changed

Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj

+86-87
Large diffs are not rendered by default.

Examples/LocalDebugging/MyApp/MyApp.xcodeproj/xcshareddata/xcschemes/MyApp.xcscheme

-78
This file was deleted.

Examples/LocalDebugging/MyApp/MyApp/AppDelegate.swift

-37
This file was deleted.

Examples/LocalDebugging/MyApp/MyApp/Base.lproj/LaunchScreen.storyboard

-25
This file was deleted.

Examples/LocalDebugging/MyApp/MyApp/ContentView.swift

+39-33
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
}

Examples/LocalDebugging/MyApp/MyApp/Info.plist

-60
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftUI
16+
17+
@main
18+
struct MyApp: App {
19+
var body: some Scene {
20+
WindowGroup {
21+
ContentView()
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)