Skip to content

Instantly share code, notes, and snippets.

@ShawonAshraf
Created October 6, 2019 21:12
Show Gist options
  • Select an option

  • Save ShawonAshraf/d46c5be0034e2987634a0c5f1b24ea00 to your computer and use it in GitHub Desktop.

Select an option

Save ShawonAshraf/d46c5be0034e2987634a0c5f1b24ea00 to your computer and use it in GitHub Desktop.
import SwiftUI
struct CheckoutView: View {
@EnvironmentObject var order: Order
@State private var paymentType = 0
@State private var addLoyaltyDetails = false
@State private var loyaltyNumber = ""
@State private var tipAmount = 0
@State private var showingPaymentAlert = false
@State private var pickupTime = 0
static let paymentTypes = ["Cash", "Credit Card", "iDine Points"]
static let tipAmounts = [10, 15, 20, 25, 0]
static let pickupTimes = ["Now", "Tonight", "Tomorrow Morning"]
var totalPrice: Double {
let total = Double(order.total)
let tipValue = total / 100 * Double(Self.tipAmounts[tipAmount])
return total + tipValue
}
var body: some View {
Form {
Section {
Picker("How do you want to pay?", selection: $paymentType) {
ForEach(0 ..< Self.paymentTypes.count) {
Text(Self.paymentTypes[$0])
}
}
Toggle(isOn: $addLoyaltyDetails.animation()) {
Text("Add iDine loyalty card")
}
if addLoyaltyDetails {
TextField("Enter your iDine ID", text: $loyaltyNumber)
}
}
Section(header: Text("Add a Tip")) {
Picker("Percentage", selection: $tipAmount) {
ForEach(0 ..< Self.tipAmounts.count) {
Text("\(Self.tipAmounts[$0])%")
}
}.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Select pickup time")) {
Picker("Pickup", selection: $pickupTime) {
ForEach(0 ..< Self.pickupTimes.count) {
Text(Self.pickupTimes[$0])
}
}.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Total $\(totalPrice, specifier: "%.2f")").font(.largeTitle)) {
Button("Confirm order") {
// place the order
self.showingPaymentAlert.toggle()
}
}
}
.navigationBarTitle(Text("Payment"), displayMode: .inline)
.alert(isPresented: $showingPaymentAlert) {
Alert(title: Text("Order confirmed"), message: Text("Your total was $\(totalPrice, specifier: "%.2f") – thank you!"), dismissButton: .default(Text("OK")))
}
}
}
struct CheckoutView_Previews: PreviewProvider {
static let order = Order()
static var previews: some View {
CheckoutView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment