/
ContentView.swift
94 lines (78 loc) · 2.77 KB
/
ContentView.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import SwiftUI
struct ContentView: View {
@State private var bill: String = ""
@State private var tip: Double = 15.0
@State private var persons: Double = 2.0
@State private var amtDue: Double? = 0
@State private var isEditing = false
@State private var tipAmtSelected = false
@State private var numPersonsSelected = false
let tipAmounts = [5.0, 10.0, 15.0, 20.0, 25.0]
let noAmtWarning = "You haven't entered your bill amount"
func calculateTip() {
let billAmt = Double(bill) ?? 0
amtDue = ((billAmt) + ((billAmt) * (tip / 100))) / persons
}
var body: some View {
VStack {
Text(String(format: "%.2f per person", amtDue ?? 0))
.font(.largeTitle)
.padding()
if bill.isEmpty && (tipAmtSelected || numPersonsSelected) {
Text(noAmtWarning)
.foregroundColor(.red)
.font(.headline)
.padding()
} else {
Text("")
.font(.headline)
.padding()
}
Spacer()
TextField("Enter Bill Amount", text: $bill)
.textFieldStyle(.roundedBorder)
.padding()
.font(.largeTitle)
.multilineTextAlignment(.center)
.onChange(of: bill) { _ in
calculateTip()
}
Spacer()
Text("Choose tip percentage")
.font(.title2)
Picker("Tip Amount", selection: $tip) {
ForEach(tipAmounts, id: \.self) { value in
Text("\(Int(value))")
.fontWeight(value == tip ? .bold : .regular)
}
}
.pickerStyle(.segmented)
.onChange(of: tip) { _ in
calculateTip()
tipAmtSelected = true
}
Spacer()
Text("How many persons are splitting?")
.font(.title2)
Slider(
value: $persons,
in: 1...20,
step: 1
) {
} minimumValueLabel: {
Text("1")
} maximumValueLabel: {
Text("20")
} onEditingChanged: { editing in
isEditing = editing
numPersonsSelected = true
calculateTip()
}
Text("\(Int(persons)) \(persons == 1 ? "person" : "persons")")
.font(.title3)
.padding()
Spacer()
}
.padding()
}
}