-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainCheckViewController.swift
More file actions
219 lines (171 loc) · 9.56 KB
/
MainCheckViewController.swift
File metadata and controls
219 lines (171 loc) · 9.56 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// MainCheckViewController.swift
// SplitCheck
//
// Created by Luis Puentes on 6/20/17.
// Copyright © 2017 LuisPuentes. All rights reserved.
//
import UIKit
class MainCheckViewController: UIViewController {
var mainCheckController = MainCheckController()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(red: 96.0/255.0, green: 120.0/255.0, blue: 208.0/255.0, alpha: 1.0)
setupLabels()
setupTextFields()
setupButtons()
setupLabelConstraints()
setupTextFieldConstraints()
setupButtonConstraints()
tapGesture(self)
}
// MARK: - Labels
let mealTitleLabel = UILabel()
let partySizeLabel = UILabel()
let totalAmountLabel = UILabel()
let splitAmountLabel = UILabel()
let grandTotalAmountLabel = UILabel()
// MARK: - Text Fields
let partySizeTextField = UITextField()
let totalAmountTextField = UITextField()
// MARK: - Buttons
let splitDatCheckButton = UIButton()
let clearButton = UIButton()
func setupLabels() {
// Set Text
mealTitleLabel.text = "Total Check"
mealTitleLabel.textColor = .black
mealTitleLabel.font = UIFont(name: "Palatino-BoldItalic", size: 50)
mealTitleLabel.textAlignment = .center
mealTitleLabel.adjustsFontSizeToFitWidth = true
partySizeLabel.text = "Party Size:"
partySizeLabel.textColor = .black
partySizeLabel.adjustsFontSizeToFitWidth = true
totalAmountLabel.text = "Total Amount:"
totalAmountLabel.textColor = .black
totalAmountLabel.adjustsFontSizeToFitWidth = true
splitAmountLabel.text = "Split Amount:"
splitAmountLabel.textColor = .black
splitAmountLabel.adjustsFontSizeToFitWidth = true
grandTotalAmountLabel.textColor = .white
grandTotalAmountLabel.backgroundColor = .darkGray
grandTotalAmountLabel.adjustsFontSizeToFitWidth = true
grandTotalAmountLabel.layer.masksToBounds = true
grandTotalAmountLabel.layer.cornerRadius = 5
// Add buttons to the subview
view.addSubview(mealTitleLabel)
view.addSubview(partySizeLabel)
view.addSubview(totalAmountLabel)
view.addSubview(splitAmountLabel)
view.addSubview(grandTotalAmountLabel)
}
func setupLabelConstraints() {
// Translates AutoResizingMaskIntoConstraints
partySizeLabel.translatesAutoresizingMaskIntoConstraints = false
totalAmountLabel.translatesAutoresizingMaskIntoConstraints = false
splitAmountLabel.translatesAutoresizingMaskIntoConstraints = false
grandTotalAmountLabel.translatesAutoresizingMaskIntoConstraints = false
mealTitleLabel.translatesAutoresizingMaskIntoConstraints = false
// Height and Width
mealTitleLabel.widthAnchor.constraint(equalToConstant: 180).isActive = true
mealTitleLabel.heightAnchor.constraint(equalToConstant: 40).isActive = true
partySizeLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
partySizeLabel.heightAnchor.constraint(equalToConstant: 25).isActive = true
totalAmountLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
totalAmountLabel.heightAnchor.constraint(equalToConstant: 25).isActive = true
splitAmountLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
splitAmountLabel.heightAnchor.constraint(equalToConstant: 25).isActive = true
grandTotalAmountLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
grandTotalAmountLabel.heightAnchor.constraint(equalToConstant: 25).isActive = true
// Horizontal and Vertical
mealTitleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
mealTitleLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 75).isActive = true
partySizeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -75).isActive = true
partySizeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -175).isActive = true
totalAmountLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -75).isActive = true
totalAmountLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
splitAmountLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -75).isActive = true
splitAmountLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 115).isActive = true
grandTotalAmountLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 75).isActive = true
grandTotalAmountLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 115).isActive = true
}
func setupTextFields() {
// Set borders
partySizeTextField.borderStyle = .roundedRect
totalAmountTextField.borderStyle = .roundedRect
// Add buttons to the subview
view.addSubview(partySizeTextField)
view.addSubview(totalAmountTextField)
}
func setupTextFieldConstraints() {
// Translates AutoResizingMaskIntoConstraints
partySizeTextField.translatesAutoresizingMaskIntoConstraints = false
totalAmountTextField.translatesAutoresizingMaskIntoConstraints = false
// Height and Width
partySizeTextField.widthAnchor.constraint(equalToConstant: 100).isActive = true
partySizeTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
totalAmountTextField.widthAnchor.constraint(equalToConstant: 100).isActive = true
totalAmountTextField.heightAnchor.constraint(equalToConstant: 30).isActive = true
// Horizontal and Vertical
partySizeTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 75).isActive = true
partySizeTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -175).isActive = true
totalAmountTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 75).isActive = true
totalAmountTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -100).isActive = true
}
func setupButtons() {
// Set Color & Text
splitDatCheckButton.setTitle("Split Check!", for: .normal)
splitDatCheckButton.setTitleColor(.white, for: .normal)
splitDatCheckButton.backgroundColor = .darkGray
splitDatCheckButton.titleLabel?.textAlignment = .center
splitDatCheckButton.titleLabel?.adjustsFontSizeToFitWidth = true
splitDatCheckButton.layer.masksToBounds = true
splitDatCheckButton.layer.cornerRadius = 5
clearButton.setTitle("Clear", for: .normal)
clearButton.setTitleColor(.white, for: .normal)
clearButton.layer.borderColor = UIColor.white.cgColor
clearButton.layer.borderWidth = 1
clearButton.layer.masksToBounds = true
clearButton.layer.cornerRadius = 5
clearButton.titleLabel?.textAlignment = .center
clearButton.titleLabel?.adjustsFontSizeToFitWidth = true
// Add Targets - for splitting the check and clearing the text
splitDatCheckButton.addTarget(self, action: #selector(splitDatCheckButtonTapped), for: .touchUpInside)
clearButton.addTarget(self, action: #selector(clearButtonTapped), for: .touchUpInside)
// Add buttons to the subview
view.addSubview(splitDatCheckButton)
view.addSubview(clearButton)
}
func setupButtonConstraints() {
// Translates AutoResizingMaskIntoConstraints
splitDatCheckButton.translatesAutoresizingMaskIntoConstraints = false
clearButton.translatesAutoresizingMaskIntoConstraints = false
// Height and Width
splitDatCheckButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
splitDatCheckButton.heightAnchor.constraint(equalToConstant: 35).isActive = true
clearButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
clearButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
// Horizontal and Vertical
splitDatCheckButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
splitDatCheckButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
clearButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
clearButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -75).isActive = true
}
func splitDatCheckButtonTapped() {
guard let partySize = partySizeTextField.text, !partySize.isEmpty else { return }
guard let partySizeDouble = Double(partySize) else { return }
guard let totalAmount = totalAmountTextField.text, !totalAmount.isEmpty else { return }
guard let totalAmountDouble = Double(totalAmount) else { return }
let mealCost = mainCheckController.totalCost(partySize: partySizeDouble, totalAmount: totalAmountDouble)
grandTotalAmountLabel.text = "$\(mealCost)0"
}
func clearButtonTapped() {
partySizeTextField.text = ""
totalAmountTextField.text = ""
grandTotalAmountLabel.text = ""
}
@IBAction func tapGesture(_ sender: Any) {
partySizeTextField.resignFirstResponder()
totalAmountTextField.resignFirstResponder()
}
}