Skip to content

Commit c580d81

Browse files
committed
Merge pull request #17 from cemolcay/fix/cemolcay-custom-keyboard
Fix/cemolcay custom keyboard
2 parents 143d312 + 10784db commit c580d81

File tree

7 files changed

+632
-578
lines changed

7 files changed

+632
-578
lines changed

Keyboard/DefaultKeyboard/DefaultKeyboard.swift renamed to Keyboard/DefaultKeyboard/CustomKeyboard.swift

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// DefaultKeyboard.swift
2+
// CustomKeyboard.swift
33
// KeyboardLayoutEngine
44
//
55
// Created by Cem Olcay on 11/05/16.
@@ -8,24 +8,18 @@
88

99
import UIKit
1010

11-
// MARK: - DefaultKeyboardDelegate
12-
@objc public protocol DefaultKeyboardDelegate {
13-
optional func defaultKeyboardDidPressKeyButton(defaultKeyboard: DefaultKeyboard, key: String)
14-
optional func defaultKeyboardDidPressSpaceButton(defaultKeyboard: DefaultKeyboard)
15-
optional func defaultKeyboardDidPressBackspaceButton(defaultKeyboard: DefaultKeyboard)
16-
optional func defaultKeyboardDidPressGlobeButton(defaultKeyboard: DefaultKeyboard)
17-
optional func defaultKeyboardDidPressReturnButton(defaultKeyboard: DefaultKeyboard)
18-
optional func defaultKeyboardDidPressKeyboardButton(defaultKeyboard: DefaultKeyboard, keyboardButton: KeyboardButton)
11+
// MARK: - CustomKeyboardDelegate
12+
@objc public protocol CustomKeyboardDelegate {
13+
optional func customKeyboardKeyButtonPressed(customKeyboard: CustomKeyboard, key: String)
14+
optional func customKeyboardSpaceButtonPressed(customKeyboard: CustomKeyboard)
15+
optional func customKeyboardBackspaceButtonPressed(customKeyboard: CustomKeyboard)
16+
optional func customKeyboardGlobeButtonPressed(customKeyboard: CustomKeyboard)
17+
optional func customKeyboardReturnButtonPressed(customKeyboard: CustomKeyboard)
18+
optional func customKeyboardButtonPressed(customKeyboard: CustomKeyboard, keyboardButton: KeyboardButton)
1919
}
2020

21-
// MARK: - DefaultKeyboard
22-
public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
23-
public var uppercaseToggledLayout: KeyboardLayout!
24-
public var uppercaseLayout: KeyboardLayout!
25-
public var lowercaseLayout: KeyboardLayout!
26-
public var numbersLayout: KeyboardLayout!
27-
public var symbolsLayout: KeyboardLayout!
28-
21+
// MARK: - CustomKeyboard
22+
public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
2923
public var shiftToggleInterval: NSTimeInterval = 0.5
3024
private var shiftToggleTimer: NSTimer?
3125
private var shiftCanBeToggled: Bool = false
@@ -36,6 +30,51 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
3630
private var backspaceDeleteTimer: NSTimer?
3731
private var backspaceAutoDeleteModeTimer: NSTimer?
3832

33+
public var uppercaseToggledLayout: KeyboardLayout! {
34+
didSet {
35+
guard currentLayout != nil && oldValue != nil else { return }
36+
if currentLayout == oldValue {
37+
currentLayout = uppercaseToggledLayout
38+
}
39+
}
40+
}
41+
42+
public var uppercaseLayout: KeyboardLayout! {
43+
didSet {
44+
guard currentLayout != nil && oldValue != nil else { return }
45+
if currentLayout == oldValue {
46+
currentLayout = uppercaseLayout
47+
}
48+
}
49+
}
50+
51+
public var lowercaseLayout: KeyboardLayout! {
52+
didSet {
53+
guard currentLayout != nil && oldValue != nil else { return }
54+
if currentLayout == oldValue {
55+
currentLayout = lowercaseLayout
56+
}
57+
}
58+
}
59+
60+
public var numbersLayout: KeyboardLayout! {
61+
didSet {
62+
guard currentLayout != nil && oldValue != nil else { return }
63+
if currentLayout == oldValue {
64+
currentLayout = numbersLayout
65+
}
66+
}
67+
}
68+
69+
public var symbolsLayout: KeyboardLayout! {
70+
didSet {
71+
guard currentLayout != nil && oldValue != nil else { return }
72+
if currentLayout == oldValue {
73+
currentLayout = symbolsLayout
74+
}
75+
}
76+
}
77+
3978
private var lastLetterLayout: KeyboardLayout?
4079
private(set) var currentLayout: KeyboardLayout! {
4180
didSet {
@@ -46,13 +85,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
4685
}
4786
}
4887

49-
public override var frame: CGRect {
50-
didSet {
51-
currentLayout?.frame = frame
52-
}
53-
}
54-
55-
public weak var delegate: DefaultKeyboardDelegate?
88+
public weak var delegate: CustomKeyboardDelegate?
5689

5790
// MARK: Init
5891
public init() {
@@ -71,22 +104,36 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
71104
}
72105

73106
private func defaultInit() {
74-
uppercaseToggledLayout = DefaultKeyboardLayout.UppercaseToggled.keyboardLayout
75-
uppercaseLayout = DefaultKeyboardLayout.Uppercase.keyboardLayout
76-
lowercaseLayout = DefaultKeyboardLayout.Lowercase.keyboardLayout
77-
numbersLayout = DefaultKeyboardLayout.Numbers.keyboardLayout
78-
symbolsLayout = DefaultKeyboardLayout.Symbols.keyboardLayout
79-
107+
reload()
80108
currentLayout = uppercaseLayout
81109
addSubview(currentLayout)
82110
}
83111

112+
// MARK: Layout
113+
public override func layoutSubviews() {
114+
super.layoutSubviews()
115+
currentLayout?.frame = CGRect(
116+
x: 0,
117+
y: 0,
118+
width: frame.size.width,
119+
height: frame.size.height)
120+
}
121+
122+
// MARK: Reload
123+
public func reload() {
124+
uppercaseToggledLayout = CustomKeyboardLayout.UppercaseToggled.keyboardLayout
125+
uppercaseLayout = CustomKeyboardLayout.Uppercase.keyboardLayout
126+
lowercaseLayout = CustomKeyboardLayout.Lowercase.keyboardLayout
127+
numbersLayout = CustomKeyboardLayout.Numbers.keyboardLayout
128+
symbolsLayout = CustomKeyboardLayout.Symbols.keyboardLayout
129+
}
130+
84131
// MARK: Backspace Auto Delete
85132
private func startBackspaceAutoDeleteModeTimer() {
86133
backspaceAutoDeleteModeTimer = NSTimer.scheduledTimerWithTimeInterval(
87134
backspaceAutoDeleteModeInterval,
88135
target: self,
89-
selector: #selector(DefaultKeyboard.startBackspaceAutoDeleteMode),
136+
selector: #selector(CustomKeyboard.startBackspaceAutoDeleteMode),
90137
userInfo: nil,
91138
repeats: false)
92139
}
@@ -95,7 +142,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
95142
backspaceDeleteTimer = NSTimer.scheduledTimerWithTimeInterval(
96143
backspaceDeleteInterval,
97144
target: self,
98-
selector: #selector(DefaultKeyboard.autoDelete),
145+
selector: #selector(CustomKeyboard.autoDelete),
99146
userInfo: nil,
100147
repeats: true)
101148
}
@@ -116,7 +163,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
116163
}
117164

118165
internal func autoDelete() {
119-
delegate?.defaultKeyboardDidPressBackspaceButton?(self)
166+
delegate?.customKeyboardBackspaceButtonPressed?(self)
120167
}
121168

122169
// MARK: Shift Toggle
@@ -125,7 +172,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
125172
shiftToggleTimer = NSTimer.scheduledTimerWithTimeInterval(
126173
shiftToggleInterval,
127174
target: self,
128-
selector: #selector(DefaultKeyboard.invalidateShiftToggleTimer),
175+
selector: #selector(CustomKeyboard.invalidateShiftToggleTimer),
129176
userInfo: nil,
130177
repeats: false)
131178
}
@@ -138,31 +185,31 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
138185

139186
// MARK: KeyboardLayoutDelegate
140187
public func keyboardLayoutDidPressButton(keyboardLayout: KeyboardLayout, keyboardButton: KeyboardButton) {
141-
delegate?.defaultKeyboardDidPressKeyboardButton?(self, keyboardButton: keyboardButton)
188+
delegate?.customKeyboardButtonPressed?(self, keyboardButton: keyboardButton)
142189
invalidateBackspaceAutoDeleteModeTimer()
143190
invalidateBackspaceDeleteTimer()
144191
if keyboardLayout == currentLayout {
145192
switch keyboardButton.type {
146193
case .Key(let key):
147-
delegate?.defaultKeyboardDidPressKeyButton?(self, key: key)
194+
delegate?.customKeyboardKeyButtonPressed?(self, key: key)
148195
if uppercaseOnce {
149196
uppercaseOnce = false
150197
currentLayout = lowercaseLayout
151198
}
152199
default:
153200
if let id = keyboardButton.identifier,
154-
let identifier = DefaultKeyboardIdentifier(rawValue: id) {
201+
let identifier = CustomKeyboardIdentifier(rawValue: id) {
155202
switch identifier {
156203
case .Space:
157-
delegate?.defaultKeyboardDidPressSpaceButton?(self)
204+
delegate?.customKeyboardSpaceButtonPressed?(self)
158205
uppercaseOnce = false
159206
case .Backspace:
160-
delegate?.defaultKeyboardDidPressBackspaceButton?(self)
207+
delegate?.customKeyboardBackspaceButtonPressed?(self)
161208
uppercaseOnce = false
162209
case .Globe:
163-
delegate?.defaultKeyboardDidPressGlobeButton?(self)
210+
delegate?.customKeyboardGlobeButtonPressed?(self)
164211
case .Return:
165-
delegate?.defaultKeyboardDidPressReturnButton?(self)
212+
delegate?.customKeyboardReturnButtonPressed?(self)
166213
case .Letters:
167214
currentLayout = uppercaseLayout
168215
uppercaseOnce = true
@@ -204,7 +251,7 @@ public class DefaultKeyboard: UIView, KeyboardLayoutDelegate {
204251
invalidateBackspaceAutoDeleteModeTimer()
205252
invalidateBackspaceDeleteTimer()
206253
if keyboardLayout == currentLayout {
207-
if keyboardButton.identifier == DefaultKeyboardIdentifier.Backspace.rawValue {
254+
if keyboardButton.identifier == CustomKeyboardIdentifier.Backspace.rawValue {
208255
startBackspaceAutoDeleteModeTimer()
209256
}
210257
}

0 commit comments

Comments
 (0)