Skip to content

Modernize app example #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 86 additions & 87 deletions Examples/LocalDebugging/MyApp/MyApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

This file was deleted.

37 changes: 0 additions & 37 deletions Examples/LocalDebugging/MyApp/MyApp/AppDelegate.swift

This file was deleted.

This file was deleted.

72 changes: 39 additions & 33 deletions Examples/LocalDebugging/MyApp/MyApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Copyright (c) 2020-2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -19,30 +19,43 @@ struct ContentView: View {
@State var name: String = ""
@State var password: String = ""
@State var response: String = ""
@State private var isLoading: Bool = false

var body: some View {
VStack(alignment: .leading, spacing: 20) {
TextField("Username", text: $name)
SecureField("Password", text: $password)
Button(
action: {
Task {
await self.register()
let inputIncomplete = name.isEmpty || password.isEmpty
Button {
Task {
isLoading = true
do {
response = try await self.register()
} catch {
response = error.localizedDescription
}
},
label: {
Text("Register")
.padding()
.foregroundColor(.white)
.background(Color.black)
.border(Color.black, width: 2)
isLoading = false
}
)
} label: {
Text("Register")
.padding()
.foregroundColor(.white)
.background(.black)
.border(.black, width: 2)
.opacity(isLoading ? 0 : 1)
.overlay {
if isLoading {
ProgressView()
}
}
}
.disabled(inputIncomplete || isLoading)
.opacity(inputIncomplete ? 0.5 : 1)
Text(response)
}.padding(100)
}

func register() async {
func register() async throws -> String {
guard let url = URL(string: "http://127.0.0.1:7000/invoke") else {
fatalError("invalid url")
}
Expand All @@ -54,27 +67,17 @@ struct ContentView: View {
}
request.httpBody = jsonRequest

do {
let (data, response) = try await URLSession.shared.data(for: request)

guard let httpResponse = response as? HTTPURLResponse else {
throw CommunicationError(reason: "invalid response, expected HTTPURLResponse")
}
guard httpResponse.statusCode == 200 else {
throw CommunicationError(reason: "invalid response code: \(httpResponse.statusCode)")
}
let jsonResponse = try JSONDecoder().decode(Response.self, from: data)
let (data, response) = try await URLSession.shared.data(for: request)

self.response = jsonResponse.message
} catch {
self.response = error.localizedDescription
guard let httpResponse = response as? HTTPURLResponse else {
throw CommunicationError(reason: "Invalid response, expected HTTPURLResponse.")
}
}

func setResponse(_ text: String) {
DispatchQueue.main.async {
self.response = text
guard httpResponse.statusCode == 200 else {
throw CommunicationError(reason: "Invalid response code: \(httpResponse.statusCode)")
}

let jsonResponse = try JSONDecoder().decode(Response.self, from: data)
return jsonResponse.message
}
}

Expand All @@ -84,6 +87,9 @@ struct ContentView_Previews: PreviewProvider {
}
}

struct CommunicationError: Error {
struct CommunicationError: LocalizedError {
let reason: String
var errorDescription: String? {
self.reason
}
}
60 changes: 0 additions & 60 deletions Examples/LocalDebugging/MyApp/MyApp/Info.plist

This file was deleted.

24 changes: 24 additions & 0 deletions Examples/LocalDebugging/MyApp/MyApp/MyApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import SwiftUI

@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Loading