Skip to content

Commit

Permalink
projeto 10 completo, das 52 finalizado .
Browse files Browse the repository at this point in the history
  • Loading branch information
joaocamargo committed Jun 4, 2021
1 parent c22a9ea commit b2713e5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 51 additions & 1 deletion CupcakeCorner/CupcakeCorner/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,59 @@ import SwiftUI
struct CheckoutView: View {

@ObservedObject var order: Order
@State private var confirmationMessage = ""
@State private var showingConfirmation = false


var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
GeometryReader { geo in
ScrollView{
VStack {
Image("cupcakes").resizable().scaledToFit()
.frame(width: geo.size.width)

Text("Your Total is: $\(self.order.cost, specifier: "%.2f")").font(.title)


Button("Place Order"){
self.placeOrder()
}.padding()
}
}
}
.navigationBarTitle("Checkout",displayMode: .inline)
.alert(isPresented: $showingConfirmation) {
Alert(title: Text("Thank You!"), message: Text(confirmationMessage), dismissButton: .default(Text("OK")))
}
}

func placeOrder() {
guard let encoded = try? JSONEncoder().encode(order) else {
print("Failed to encode order")
return
}

let url = URL(string: "https://reqres.in/api/cupcakes")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = encoded

URLSession.shared.dataTask(with: request) { data,response,error in
guard let data = data else {
print("no data in response \(error?.localizedDescription ?? "Unknow" )")
return
}

if let decodedOrder = try? JSONDecoder().decode(Order.self, from: data) {
self.confirmationMessage = "Your order for \(decodedOrder.quantity)x\(Order.types[decodedOrder.type].lowercased()) cupcakes is on its way!"
self.showingConfirmation = true
} else {
print("Invalid")
}

}.resume()

}
}

Expand Down
58 changes: 57 additions & 1 deletion CupcakeCorner/CupcakeCorner/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

import Foundation

class Order: ObservableObject {
class Order: ObservableObject, Codable {


enum CodingKeys: CodingKey {
case type, quantity, extraFrosting, addSprinkles, name, streetAddress, city, zip
}


static let types = ["Vanilla", "Strawberry","Chocolate","Rainbow"]


Expand Down Expand Up @@ -37,4 +44,53 @@ class Order: ObservableObject {
return true
}

var cost: Double {
var cost = Double(quantity) * 2
cost += Double(type) / 2

if extraFrosting {
cost += Double(quantity)
}

if addSprinkles{
cost += Double(quantity) / 2
}

return cost
}

init() {}

required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

type = try container.decode(Int.self, forKey: .type)
quantity = try container.decode(Int.self, forKey: .quantity)
extraFrosting = try container.decode(Bool.self, forKey: .extraFrosting)
addSprinkles = try container.decode(Bool.self, forKey: .addSprinkles)

name = try container.decode(String.self, forKey: .name)
streetAddress = try container.decode(String.self, forKey: .streetAddress)
city = try container.decode(String.self, forKey: .city)
zip = try container.decode(String.self, forKey: .zip)


}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(type, forKey: .type)
try container.encode(quantity, forKey: .quantity)
try container.encode(extraFrosting, forKey: .extraFrosting)
try container.encode(addSprinkles, forKey: .addSprinkles)
try container.encode(name, forKey: .name)
try container.encode(streetAddress, forKey: .streetAddress)
try container.encode(city, forKey: .city)
try container.encode(zip, forKey: .zip)



}

}

0 comments on commit b2713e5

Please sign in to comment.