-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditCurrencyViewController.swift
157 lines (126 loc) · 4.68 KB
/
EditCurrencyViewController.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
//
// EditCurrencyViewController.swift
// CoinPush
//
// Created by Bijan Massoumi on 7/20/17.
// Copyright © 2017 Goods and Services. All rights reserved.
//
import UIKit
class EditCurrencyViewController: UIViewController, UITextFieldDelegate {
//MARK: attributes
@IBOutlet weak var identiferLabel: UILabel!
@IBOutlet weak var conversionLabel: UILabel!
@IBOutlet weak var pushSwitch: UISwitch!
@IBOutlet weak var increaseLabel: UILabel!
@IBOutlet weak var increaseValue: UITextField!
@IBOutlet weak var decreaseLabel: UILabel!
@IBOutlet weak var decreaseValue: UITextField!
var pair: EditInfo?
override func viewDidLoad() {
super.viewDidLoad()
if let pair = pair {
//initialize top labels
identiferLabel?.text = "\(pair.Pair.fromTag) to \(pair.Pair.toTag)"
conversionLabel?.text = "\(helper.symbolDict[(pair.Pair.fromTag)]!)1 = \(pair.currentPrice)"
let currency = pair.Pair.fromTag
//deal with advanced push setup
increaseLabel.text! = "When \(currency) increases by "
decreaseLabel.text! = "When \(currency) decreases by "
initializeTextBoxs(pair: pair)
increaseValue.delegate = self
decreaseValue.delegate = self
increaseLabel.tag = 1
increaseValue.tag = 1
decreaseLabel.tag = 2
decreaseValue.tag = 2
//pushswitch setup
pushSwitch.isOn = (pair.Pair.pushEnabled)
if (pushSwitch.isOn){
revealAdvancedPush()
} else {
hideAdvancedPush()
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
//MARK: Actions
@IBAction func isSwitched(_ sender: UISwitch) {
if sender.isOn {
revealAdvancedPush()
} else {
hideAdvancedPush()
}
}
//MARK: Private Functions
private func hideAdvancedPush(){
increaseLabel.isHidden = true
decreaseLabel.isHidden = true
increaseValue.isHidden = true
decreaseValue.isHidden = true
}
private func revealAdvancedPush(){
increaseLabel.isHidden = false
decreaseLabel.isHidden = false
increaseValue.isHidden = false
decreaseValue.isHidden = false
}
private func initializeTextBoxs(pair: EditInfo) {
if let increase = pair.Pair.increaseValue {
increaseValue.text? = "\(increase)%"
increaseLabel.isEnabled = true
} else {
increaseValue.placeholder = "0.00%"
increaseLabel.isEnabled = false
}
if let decrease = pair.Pair.decreaseValue {
decreaseValue.text? = "\(decrease)%"
decreaseLabel.isEnabled = true
} else {
decreaseValue.placeholder = "0.00%"
decreaseLabel.isEnabled = false
}
}
//MARK: UITextField Delegate
func textFieldDidEndEditing(_ textField: UITextField) {
let value = textField.text!.components(separatedBy: "%")
let numVal = Float(value[0])
if (numVal == 0 || numVal == nil) {
if (textField.tag == 1) {
increaseLabel.isEnabled = false
} else {
decreaseLabel.isEnabled = false
}
textField.placeholder? = "0.00%"
} else {
if (textField.tag == 1) {
increaseLabel.isEnabled = true
} else {
decreaseLabel.isEnabled = true
}
textField.text? += "%"
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if let data = textField.text{
var value = data.components(separatedBy: "%")
textField.text? = value[0]
}
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
pair?.Pair.pushEnabled = pushSwitch.isOn
if let val = Float((increaseValue?.text?.components(separatedBy: "%")[0])!) {
pair?.Pair.increaseValue = val
}
if let val = Float((decreaseValue?.text?.components(separatedBy: "%")[0])!) {
pair?.Pair.decreaseValue = val
}
}
}