-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeViewController.swift
194 lines (163 loc) · 6.19 KB
/
HomeViewController.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
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
//
// ViewController.swift
// SimpleCalculator
//
// Created by framgia on 10/24/16.
// Copyright © 2016 framgia. All rights reserved.
//
import UIKit
class HomeViewController: UIViewController, ResultViewControllerDelegate {
// MARK: - Properties
@IBOutlet weak var intButton: UIButton!
@IBOutlet weak var floatButton: UIButton!
@IBOutlet weak var stringButton: UIButton!
var topButtons = [UIButton]()
@IBOutlet weak var valueOfX: UITextField!
@IBOutlet weak var valueOfY: UITextField!
@IBOutlet weak var sumButton: UIButton!
@IBOutlet weak var mulButton: UIButton!
@IBOutlet weak var concatButton: UIButton!
@IBOutlet weak var sumLabel: UILabel!
@IBOutlet weak var mulLabel: UILabel!
@IBOutlet weak var conLabel: UILabel!
var operation = ""
var argument1 = ""
var argument2 = ""
var dataType = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
topButtons = [intButton, floatButton, stringButton]
sumButton.enabled = false
mulButton.enabled = false
concatButton.enabled = false
NSNotificationCenter.defaultCenter()
.addObserver(self,
selector: #selector(self.catchNotification(_:)),
name: NotificationKeys.notificationKey,
object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Actions
@IBAction func selectTypeInt(sender: UIButton) {
dataType = DataTypes.int
updateButtons(topButtons, selectedButton: sender)
}
@IBAction func selectTypeFloat(sender: UIButton) {
dataType = DataTypes.float
updateButtons(topButtons, selectedButton: sender)
}
@IBAction func selectTypeString(sender: UIButton) {
dataType = DataTypes.string
updateButtons(topButtons, selectedButton: sender)
}
@IBAction func selectOperationSum(sender: UIButton) {
calculate(Operations.summation)
}
@IBAction func selectOperationMultiplication(sender: UIButton) {
calculate(Operations.multiplication)
}
@IBAction func selectOperationConcatanation(sender: UIButton) {
calculate(Operations.concatanation)
}
// MARK: - Utils
func updateButtons(buttons: [UIButton], selectedButton: UIButton) {
for button: UIButton in buttons {
if button == selectedButton {
button.backgroundColor = UIColor.grayColor()
} else {
button.backgroundColor = UIColor.clearColor()
}
}
switch dataType {
case DataTypes.int:
sumButton.enabled = true
mulButton.enabled = true
concatButton.enabled = false
break
case DataTypes.float:
sumButton.enabled = true
mulButton.enabled = true
concatButton.enabled = false
break
case DataTypes.string:
sumButton.enabled = false
mulButton.enabled = false
concatButton.enabled = true
default:
break
}
}
func calculate(operation: String) {
var result = ""
switch operation {
case Operations.summation:
if(dataType == DataTypes.int){
if let x = valueOfX.text?.componentsSeparatedByString("."), y = valueOfY.text?.componentsSeparatedByString(".") {
result = "\(Int(x.first!)! + Int(y.first!)!)"
} else {
result = "\(Int(valueOfX.text!)! + Int(valueOfY.text!)!)"
}
} else {
result = "\(Float(valueOfX.text!)! + Float(valueOfY.text!)!)"
}
break
case Operations.multiplication:
if(dataType == DataTypes.int){
if let x = valueOfX.text?.componentsSeparatedByString("."), y = valueOfY.text?.componentsSeparatedByString(".") {
result = "\(Int(x.first!)! * Int(y.first!)!)"
} else {
result = "\(Int(valueOfX.text!)! * Int(valueOfY.text!)!)"
}
} else {
result = "\(Float(valueOfX.text!)! * Float(valueOfY.text!)!)"
}
break
case Operations.concatanation:
result = valueOfX.text! + valueOfY.text!
break
default:
break
}
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ResultViewController") as? ResultViewController
secondViewController?.result = Result(argument1: valueOfX.text!, argument2: valueOfY.text!, operation: operation, result: result, dataType: dataType)
secondViewController?.delegate = self
secondViewController?.onClosureButtonTapped = {(data: [Result]) -> Void in
self.updateLabels(data)
}
self.presentViewController(secondViewController!, animated:true, completion:nil)
}
func updateLabels(data: [Result]) {
var sum = 0
var mul = 0
var con = 0
for r: Result in data {
if (r.operation == Operations.summation) {
sum += 1
} else if (r.operation == Operations.multiplication) {
mul += 1
} else {
con += 1
}
}
sumLabel.text = "SUM: #" + "\(sum)"
mulLabel.text = "MUL: #" + "\(mul)"
conLabel.text = "CON: #" + "\(con)"
}
func catchNotification(notification: NSNotification) -> Void {
guard let userInfo = notification.userInfo,
let message = userInfo["message"] as? [Result],
let date = userInfo["date"] as? NSDate else {
print("No userInfo found in notification")
return
}
updateLabels(message)
}
//MARK: - ResultViewControllerDelegate
func onDataSaved(data: [Result]) {
updateLabels(data)
}
}