-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckoutView.swift
74 lines (66 loc) · 2.29 KB
/
CheckoutView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// CheckoutView.swift
// iDine
//
// Created by WHGNR-1671 on 25/05/22.
//
import SwiftUI
struct CheckoutView: View {
@EnvironmentObject var order: Order
@State private var paymentType = "Cash"
@State private var addLoyaltyDetails = false
@State private var loyaltyNumber = ""
@State private var tipAmount = 15
@State private var showingPaymentAlert = false
let paymentTypes = ["Cash", "Credit Card", "iDine Points"]
let tipAmounts = [10, 15, 20 ,25 , 0]
var totalPrice: String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
let total = Double(order.total)
let tipValue = total / 100 * Double(tipAmount)
return formatter.string(from: NSNumber(value: total + tipValue)) ?? "$0"
}
var body: some View {
Form {
Section {
Picker("How do you want to pay?", selection: $paymentType) {
ForEach(paymentTypes, id: \.self) {
Text($0)
}
}
Toggle("Add iDine Loyalty Card", isOn: $addLoyaltyDetails.animation())
if addLoyaltyDetails {
TextField("Enter your iDine ID", text: $loyaltyNumber)
}
}
Section(header:Text("Add a tip?")) {
Picker("Percentage:", selection: $tipAmount) {
ForEach(tipAmounts, id: \.self) {
Text("\($0)%")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header:
Text("TOTAL: \(totalPrice)")
.font(.largeTitle)
) {
Button("Confirm order") {
showingPaymentAlert.toggle()
}
}
}
.navigationTitle("Payment")
.navigationBarTitleDisplayMode(.inline)
.alert(isPresented: $showingPaymentAlert) {
Alert(title: Text("Order confirmed"), message: Text("Your total was \(totalPrice) – thank you!"), dismissButton: .default(Text("OK")))
}
}
}
struct CheckoutView_Previews: PreviewProvider {
static var previews: some View {
CheckoutView()
.environmentObject(Order())
}
}