-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardViewController.swift
More file actions
299 lines (256 loc) · 11.8 KB
/
KeyboardViewController.swift
File metadata and controls
299 lines (256 loc) · 11.8 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// KeyboardViewController.swift
// VInput Keyboard
//
// Created by McClish, Thomas on 10/27/16.
// Copyright © 2016 EECS481-VInput. All rights reserved.
//
import UIKit
import AVFoundation
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
var fullView: UIView!
var letterLabel: UILabel!
let singleTapRecognizer = UITapGestureRecognizer()
let doubleTapRecognizer = UITapGestureRecognizer()
let doubleTapTwoTouchRecognizer = UITapGestureRecognizer()
let swipeLeftRecognizer = UISwipeGestureRecognizer()
let swipeDownRecognizer = UISwipeGestureRecognizer()
let swipeRightRecognizer = UISwipeGestureRecognizer()
let swipeUpRecognizer = UISwipeGestureRecognizer()
//let panFromTopRecognizer = UIScreenEdgePanGestureRecognizer() - have to suppress opening notification center for this to work
let pinchRecognizer = UIPinchGestureRecognizer()
let shortHoldRecognizer = UILongPressGestureRecognizer()
let longHoldRecognizer = UILongPressGestureRecognizer()
var heightConstraint: NSLayoutConstraint?
let alphabet: [String] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var word: String = ""
var lIndex = 0
var rIndex = 25
var insertedPeriod: Bool = false
var utterance: AVSpeechUtterance!
let speechSynthesizer = AVSpeechSynthesizer()
var searching: Bool = false
var newWord: Bool = true
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0) {
return
}
view.removeConstraint(heightConstraint!)
heightConstraint!.constant = UIScreen.main.bounds.size.height
view.addConstraint(heightConstraint!)
print("--- Set main view height to", heightConstraint?.constant)
}
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
// Set up UIView over full area to accept gestures
fullView = UIView()
fullView.backgroundColor = UIColor.gray
fullView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(fullView)
fullView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
fullView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
fullView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
fullView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
// Default Next Keyboard button
nextKeyboardButton = UIButton(type: .system)
nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
nextKeyboardButton.sizeToFit()
nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)
fullView.addSubview(nextKeyboardButton)
nextKeyboardButton.leftAnchor.constraint(equalTo: fullView.leftAnchor).isActive = true
nextKeyboardButton.bottomAnchor.constraint(equalTo: fullView.bottomAnchor).isActive = true
// Add label for current midpoint letter
letterLabel = UILabel()
letterLabel.font = letterLabel.font.withSize(fullView.frame.height - nextKeyboardButton.frame.height)
letterLabel.adjustsFontSizeToFitWidth = true
letterLabel.translatesAutoresizingMaskIntoConstraints = false
fullView.addSubview(letterLabel)
letterLabel.centerXAnchor.constraint(equalTo: fullView.centerXAnchor).isActive = true
letterLabel.centerYAnchor.constraint(equalTo: fullView.centerYAnchor).isActive = true
letterLabel.textColor = UIColor.black
announceLetter()
// Set gesture recognizer targets and values
// singleTapRecognizer.numberOfTapsRequired = 1
// singleTapRecognizer.addTarget(self, action: #selector(onSingleTap))
// singleTapRecognizer.require(toFail: doubleTapRecognizer)
doubleTapRecognizer.numberOfTapsRequired = 2
doubleTapRecognizer.numberOfTouchesRequired = 1
doubleTapRecognizer.addTarget(self, action: #selector(onDoubleTap))
doubleTapRecognizer.require(toFail: doubleTapTwoTouchRecognizer)
doubleTapTwoTouchRecognizer.numberOfTapsRequired = 2
doubleTapTwoTouchRecognizer.numberOfTouchesRequired = 2
doubleTapTwoTouchRecognizer.addTarget(self, action: #selector(onDoubleTapTwoTouch))
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirection.left
swipeLeftRecognizer.addTarget(self, action: #selector(onSwipeLeft))
swipeDownRecognizer.direction = UISwipeGestureRecognizerDirection.down
swipeDownRecognizer.addTarget(self, action: #selector(onSwipeDown))
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirection.right
swipeRightRecognizer.addTarget(self, action: #selector(onSwipeRight))
swipeUpRecognizer.direction = UISwipeGestureRecognizerDirection.up
swipeUpRecognizer.addTarget(self, action: #selector(onSwipeUp))
//panFromTopRecognizer.edges.insert(UIRectEdge.top)
//panFromTopRecognizer.addTarget(self, action: #selector(onPanFromTop))
pinchRecognizer.addTarget(self, action: #selector(onPinch))
shortHoldRecognizer.minimumPressDuration = TimeInterval(1)
shortHoldRecognizer.allowableMovement = 50
shortHoldRecognizer.addTarget(self, action: #selector(onHold))
// Add gesture recognizers to fullView
fullView.addGestureRecognizer(doubleTapRecognizer)
fullView.addGestureRecognizer(doubleTapTwoTouchRecognizer)
fullView.addGestureRecognizer(singleTapRecognizer)
fullView.addGestureRecognizer(swipeLeftRecognizer)
fullView.addGestureRecognizer(swipeDownRecognizer)
fullView.addGestureRecognizer(swipeRightRecognizer)
fullView.addGestureRecognizer(swipeUpRecognizer)
//fullView.addGestureRecognizer(panFromTopRecognizer)
fullView.addGestureRecognizer(pinchRecognizer)
fullView.addGestureRecognizer(shortHoldRecognizer)
// TODO fix this to check for orientation and set constraint to desired value
heightConstraint = NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 256)
heightConstraint!.priority = 999.0
}
func onDoubleTap() {
// self.textDocumentProxy.documentContextBeforeInput!
cutOffSpeech()
let mid = Int(ceil(Double(rIndex - lIndex)/2)) + lIndex - 1
let text = "Left or right of " + alphabet[mid]
speak(textToSpeak: text)
}
// Holder for reading back word
func onDoubleTapTwoTouch() {
//TODO: Broken for now
cutOffSpeech()
for character in word.characters {
speak(textToSpeak: String(character))
}
}
func onSwipeLeft() {
searching = true
cutOffSpeech()
if !isDone() {
rIndex = Int(ceil(Double(rIndex - lIndex)/2)) + lIndex - 1
announceLetter()
}
}
func onSwipeDown() {
cutOffSpeech()
if searching {
restartSearch()
}
else {
speak(textToSpeak: "Deleting previous character", postDelay: TimeInterval(4))
self.textDocumentProxy.deleteBackward()
announceLetter()
}
}
func onSwipeRight() {
cutOffSpeech()
searching = true
if !isDone() {
lIndex += Int(ceil(Double(rIndex - lIndex)/2.0))
announceLetter()
}
}
func onSwipeUp() {
cutOffSpeech()
let mid = Int(ceil(Double(rIndex - lIndex)/2)) + lIndex - 1
self.textDocumentProxy.insertText(alphabet[mid])
word = newWord ? alphabet[mid] : word + alphabet[mid]
newWord = false
let text = alphabet[mid] + " inserted"
speak(textToSpeak: text, pitchMultiplier: 1.0, postDelay: TimeInterval(4))
restartSearch()
}
// func onPanFromTop() {
// self.dismissKeyboard()
// }
func onPinch() {
cutOffSpeech()
if pinchRecognizer.state == UIGestureRecognizerState.ended {
self.dismissKeyboard()
}
}
func onHold() {
// TODO: CLEAN UP
cutOffSpeech()
if shortHoldRecognizer.state == UIGestureRecognizerState.began {
self.textDocumentProxy.insertText(" ")
newWord = true
speak(textToSpeak: "Space inserted")
// TODO when Settings bundle is implemented, this behavior should be a setting
// if word == "" && !insertedPeriod {
// (textDocumentProxy as UIKeyInput).deleteBackward()
// (textDocumentProxy as UIKeyInput).insertText(". ")
// speak(textToSpeak: "period inserted")
// insertedPeriod = true
// }
// else {
// }
}
}
func isDone() -> Bool {
if(lIndex == rIndex)
{
// speak with a lower pitch when announcing a finalized letter,
// and put a one second delay after speech so call to rewrite
// doesn't immediately speak the next option
let text = "Swipe up to select " + String(alphabet[lIndex]) + ", swipe down to restart search"
speak(textToSpeak: text, pitchMultiplier: 0.75, postDelay: TimeInterval(0.5))
}
return lIndex == rIndex
}
func restartSearch() {
lIndex = 0
rIndex = 25
searching = false
insertedPeriod = false
announceLetter()
}
func announceLetter() {
let nextMid = Int(ceil(Double(rIndex - lIndex)/2)) + lIndex - 1
letterLabel.text = String(alphabet[nextMid])
let text = "Left or right of " + alphabet[nextMid]
speak(textToSpeak: text)
}
func speak(textToSpeak: String, pitchMultiplier: Float = 1.0, postDelay: TimeInterval = TimeInterval(0)) {
utterance = AVSpeechUtterance(string: textToSpeak)
// TODO some of these values should be exposed as options in the Settings bundle
utterance.pitchMultiplier = pitchMultiplier
//utterance.postUtteranceDelay = postDelay
utterance.rate = 0.5
speechSynthesizer.speak(utterance)
}
func cutOffSpeech() {
if speechSynthesizer.isSpeaking{
speechSynthesizer.stopSpeaking(at: AVSpeechBoundary.immediate)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(_ textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(_ textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
textColor = UIColor.white
} else {
textColor = UIColor.black
}
self.nextKeyboardButton.setTitleColor(textColor, for: [])
}
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
print("--- Rotated")
updateViewConstraints()
}
}