This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyBoard.swift
executable file
·147 lines (114 loc) · 4.35 KB
/
KeyBoard.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
//
// KeyBoard.swift
// Utility Converter
//
// Created by Emanual Mariampillai on 05/03/2019.
// Copyright © 2019 Arnold Anthonypillai. All rights reserved.
//
import UIKit
protocol KeyBoardDelegate: class
{
func keyWasTapped(processedString: String)
func getCurrentTxtFieldValue() -> String
}
extension UIViewController
{
func hideKeyBoard()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyBoard)
)
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyBoard()
{
view.endEditing(true)
}
}
class KeyBoard: UIView
{
weak var delegate: KeyBoardDelegate?
var negationEnabled: Bool = false
@IBOutlet weak var button_negate: UIButton!
@IBOutlet weak var button_delete: UIButton!
@IBOutlet weak var button_decimal: UIButton!
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
initializeSubviews()
}
override init(frame: CGRect)
{
super.init(frame: frame)
initializeSubviews()
}
func initializeSubviews()
{
let xibFileName = "KeyBoard"
let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)?[0] as! UIView
self.addSubview(view)
view.frame = self.bounds
button_decimal.isUserInteractionEnabled = false
button_delete.isUserInteractionEnabled = false
}
@IBAction func keyTapped(_ sender: UIButton)
{
let txtFieldValue: String = Helper().unwrapString(optionalString: self.delegate?.getCurrentTxtFieldValue())
let titleLable: String = Helper().unwrapString(optionalString: sender.titleLabel?.text) //optional unwrap button label
print("before current text field \(txtFieldValue) \n button label \(titleLable)")
let processedString: String = textProcessor(stringToProcess: txtFieldValue, character: titleLable) //send the current text field value and the button label to process
let changedString: String = (processedString == txtFieldValue) ? txtFieldValue : processedString //check if anything has changed with processed text field
print("after processed string \(processedString)")
self.delegate?.keyWasTapped(processedString: changedString) //assign the new value to the focused text field and get the latest text field value
toggleButtonAvailability(txtField: changedString) //toggle "DEL" & "." accordingly
}
func toggleButtonAvailability(txtField: String)
{
button_delete.isUserInteractionEnabled = false
button_decimal.isUserInteractionEnabled = false
let stringHasDecimal: Bool = txtField.contains(".")
let isStringEmpty: Bool = txtField.count == 0
if(!isStringEmpty)
{
button_delete.isUserInteractionEnabled = true
if(!stringHasDecimal)
{
button_decimal.isUserInteractionEnabled = true
}
}
}
func textProcessor(stringToProcess: String, character: String) -> String
{
let stringHasDecimal: Bool = stringToProcess.contains(".")
let isStringEmpty: Bool = stringToProcess.count == 0
let isStringHasNegation: Bool = stringToProcess.contains("-")
let isPriorNegation: Bool = stringToProcess.last == "-"
var processedString = stringToProcess
if(character == "DEL")
{
if(!isStringEmpty)
{
processedString = String(stringToProcess.dropLast())
}
}
else if(character == ".")
{
if(!isStringEmpty && !stringHasDecimal && !isPriorNegation)
{
processedString = stringToProcess + character
}
}
else if(character == "+/-")
{
let negation: String = "-"
processedString = isStringHasNegation ? String(stringToProcess.dropFirst()) : (negation + stringToProcess)
}
else
{
processedString = stringToProcess + character
}
return processedString
}
}