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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472//
// ViewController.swift
// T-Squared for Georgia Tech
//
// Created by Cal on 8/26/15.
// Copyright ยฉ 2015 Cal Stephens. All rights reserved.
//
import UIKit
import Foundation
var TSAuthenticatedReader: TSReader!
let TSLogoutNotification = "edu.gatech.cal.logout"
let TSDismissWebViewNotification = "edu.gatech.cal.dismissWeb"
class LoginViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var formCenter: NSLayoutConstraint!
@IBOutlet weak var backgroundBottom: NSLayoutConstraint!
@IBOutlet weak var formBlurView: UIVisualEffectView!
@IBOutlet weak var formView: UIView!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var loggingInText: UILabel!
var originalLoggingInText: NSAttributedString!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var saveCredentialsSwitch: UISwitch!
@IBOutlet weak var containerLeading: NSLayoutConstraint!
@IBOutlet weak var webViewTop: NSLayoutConstraint!
@IBOutlet var tapRecognizer: UITapGestureRecognizer!
var classesViewController: ClassesViewController!
var browserViewController: TSWebView!
@IBOutlet var activityCircle: UIView!
//MARK: - Preparing the View Controller
override func viewDidLoad() {
//transition from old password scheme
let data = NSUserDefaults.standardUserDefaults()
if let savedUsername = data.stringForKey("edu.gatech.cal.username"),
let savedPassword = data.stringForKey("edu.gatech.cal.password") {
saveCredentials(username: savedUsername, password: savedPassword)
}
//nil-out the current values and close up that hole
data.setValue(nil, forKey: "edu.gatech.cal.username")
data.setValue(nil, forKey: "edu.gatech.cal.password")
}
override func viewWillAppear(animated: Bool) {
if animated { return }
if TSAuthenticatedReader != nil { return } //we're already authenticated
//double check we aren't already authenticated
let rootPage = "http://t-square.gatech.edu/portal/pda/"
let pageContents = HttpClient.contentsOfPage(rootPage, postNotificationOnError: false)?.toHTML
var validConnection = true
if let contents = pageContents {
if contents.containsString("Log Out") {
//we're still on a valid connection, but the authenticated reader doesn't exist
//recreate the reader
if let (username, password) = savedCredentials() {
TSAuthenticatedReader = TSReader(username: username, password: password, initialPage: nil)
return
}
else {
//the reader doesn't exist, but we don't have stored credentials
//clear cookies and then prompt for login again
HttpClient.clearCookies()
}
}
} else {
//there were no page contents, meaning the network is unavailable
validConnection = false
delay(0.5) { self.syncronizedNetworkErrorRecieved() }
}
self.formCenter.constant = self.view.frame.height / 2.0
self.webViewTop.constant = UIScreen.mainScreen().bounds.height
self.containerLeading.constant = UIScreen.mainScreen().bounds.width
self.view.layoutIfNeeded()
originalLoggingInText = loggingInText.attributedText!
let currentUsername = TSLaunchedUsername
usernameField.text = currentUsername
if let (savedUsername, savedPassword) = savedCredentials() where currentUsername == nil || currentUsername == "" || currentUsername == savedUsername {
usernameField.text = savedUsername
passwordField.text = savedPassword
if validConnection {
animateFormSubviewsWithDuration(0.0, hidden: true)
animateActivityIndicator(on: true)
doLogin(newLogin: false)
}
}
UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
self.formCenter.constant = 10.0
self.view.layoutIfNeeded()
}, completion: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showLogoutAlert", name: TSLogoutNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "dismissWebView", name: TSDismissWebViewNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHeightChanged:", name: UIKeyboardWillChangeFrameNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "centerForm", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "networkErrorRecieved", name: TSNetworkErrorNotification, object: nil)
activityCircle.hidden = false
activityCircle.layer.cornerRadius = 25.0
activityCircle.layer.masksToBounds = true
activityCircle.transform = CGAffineTransformMakeScale(0.0, 0.0)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let classes = segue.destinationViewController as? ClassesViewController {
classes.loginController = self
classesViewController = classes
}
if let browser = segue.destinationViewController as? TSWebView {
browser.loginController = self
self.browserViewController = browser
}
}
//MARK: - Animating and processing form input
func keyboardHeightChanged(notification: NSNotification) {
//ignore if the classes controller is visible
if self.containerLeading.constant == 0.0 { return }
if let userInfo = notification.userInfo,
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue {
let keyboardHeight = keyboardFrame().height
let newConstant = -10 - (keyboardHeight / (is4S() ? 2.5 : 3.0))
if newConstant == formCenter.constant { return }
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
self.formCenter.constant = newConstant
self.backgroundBottom.constant = keyboardHeight - 100.0
self.view.layoutIfNeeded()
}, completion: nil)
}
}
func centerForm() {
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
self.formCenter.constant = -10.0
self.backgroundBottom.constant = 0.0
self.view.layoutIfNeeded()
}, completion: nil)
}
@IBAction func switchToPasswordInput(sender: AnyObject) {
passwordField.becomeFirstResponder()
}
@IBAction func loginPressed(sender: AnyObject) {
doLogin(newLogin: true)
}
func doLogin(newLogin newLogin: Bool) {
if usernameField.text! == "" || passwordField.text! == "" {
shakeView(formView)
self.animateFormSubviewsWithDuration(0.5, hidden: false)
self.animateActivityIndicator(on: false)
self.setSavedCredentials(correct: false)
return
}
//center the form
self.passwordField.resignFirstResponder()
self.usernameField.resignFirstResponder()
centerForm()
//easter-egg
if self.usernameField.text! == "gburdell1927" {
self.passwordField.text = ""
let alert = UIAlertController(title: "Nice Try, you trickster", message: "George Burdell, is that you???", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "You found me out", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
//animate the activity indicator
animateFormSubviewsWithDuration(0.5, hidden: true)
delay(0.2) { self.animateActivityIndicator(on: true) }
//attempt to authenticate
dispatch_async(TSNetworkQueue, {
TSReader.authenticatedReader(user: self.usernameField.text!, password: self.passwordField.text!, isNewLogin: newLogin, completion: { reader in
if let reader = reader {
TSAuthenticatedReader = reader
self.animateFormSubviewsWithDuration(0.5, hidden: false)
self.animateActivityIndicator(on: false)
self.setSavedCredentials(correct: true)
self.presentClassesView(reader)
}
else {
self.passwordField.text = ""
shakeView(self.formView)
self.animateFormSubviewsWithDuration(0.5, hidden: false)
self.animateActivityIndicator(on: false)
self.setSavedCredentials(correct: false)
}
})
})
}
func networkErrorRecieved() {
sync {
self.syncronizedNetworkErrorRecieved()
}
}
func syncronizedNetworkErrorRecieved() {
self.animateFormSubviewsWithDuration(0.5, hidden: false)
self.animateActivityIndicator(on: false)
let existingUsername = self.usernameField.text
let existingPassword = self.passwordField.text
//prevent the username and password from being cleared out
delay(0.5) {
self.usernameField.text = existingUsername
self.passwordField.text = existingPassword
}
let alert = UIAlertController(title: "Couldn't connect to T-Square", message: "Are you connected to the internet?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .Destructive, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: .Default, handler: { _ in openSettings() }))
self.presentViewController(alert, animated: true, completion: nil)
self.classesViewController.DISABLE_PUSHES = true
}
func setSavedCredentials(correct correct: Bool) {
if !correct || !saveCredentialsSwitch.on {
saveCredentials(username: nil, password: nil)
}
else {
saveCredentials(username: usernameField.text, password: passwordField.text)
}
}
func animateFormSubviewsWithDuration(duration: Double, hidden: Bool) {
for subview in formView.subviews {
if !(subview is UIVisualEffectView) && subview.tag != -1 {
UIView.animateWithDuration(duration) {
subview.alpha = hidden ? 0.0 : (subview.tag == 1 ? 0.3 : 1.0)
}
}
}
UIView.animateWithDuration(duration) {
self.loggingInText.alpha = hidden ? 1.0 : 0.0
}
let loggingInString = originalLoggingInText.mutableCopy() as! NSMutableAttributedString
loggingInString.replaceCharactersInRange(NSMakeRange(14, 8), withString: usernameField.text!)
self.loggingInText.attributedText = loggingInString
}
func animateActivityIndicator(on on: Bool) {
var originalIndicatorPosition: CGPoint {
return CGPointMake(CGRectGetMidX(formView.frame) - activityIndicator.frame.width / 2.0, (CGRectGetMidY(formView.frame) - activityIndicator.frame.height / 2.0) + 50.0)
}
if on {
let originalPosition = originalIndicatorPosition
let animationStart = CGPointMake(originalPosition.x, originalPosition.y + 50)
activityIndicator.frame.origin = animationStart
UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
self.activityIndicator.frame.origin = originalPosition
self.activityIndicator.alpha = 1.0
}, completion: nil)
}
else {
let originalPosition = originalIndicatorPosition
let animationEnd = CGPointMake(originalPosition.x, originalPosition.y + 50)
UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.0, options: [], animations: {
self.activityIndicator.frame.origin = animationEnd
self.activityIndicator.alpha = 0.0
}, completion: nil)
}
}
@IBAction func defocusTextViews(sender: UITapGestureRecognizer) {
let touch = sender.locationInView(self.view)
let blurFrame = self.view.convertRect(formBlurView.frame, fromView: formBlurView.superview!)
if !CGRectContainsPoint(blurFrame, touch) {
usernameField.resignFirstResponder()
passwordField.resignFirstResponder()
centerForm()
}
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
//MARK: - Switching Views
func presentClassesView(reader: TSReader) {
tapRecognizer.enabled = false
var classes: [Class] = []
var loadAttempt = 0
while classes.count == 0 && loadAttempt < 15 && !reader.actuallyHasNoClasses {
loadAttempt++
classes = reader.getActiveClasses()
if classes.count == 0 {
reader.checkIfHasNoClasses()
}
if loadAttempt > 4 {
HttpClient.clearCookies()
}
}
if loadAttempt >= 15 {
//present an alert and then exit the app if the classes aren't loading
//this means a failed authentication looked like it passed
//AKA I have no idea what happened
let message = "This happens every now and then. Please restart T-Squared and try again. If this keeps happening, please send an email to cal@calstephens.tech"
let alert = UIAlertController(title: "There was a problem logging you in.", message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Restart", style: .Default, handler: { _ in
//crash
let null: Int! = nil
null.threeCharacterString()
}))
self.presentViewController(alert, animated: true, completion: nil)
self.setSavedCredentials(correct: false)
return
}
classesViewController.classes = classes
classesViewController.reloadTable()
//open to the shortcut item if there is one queued
if let delegate = UIApplication.sharedApplication().delegate as? AppDelegate {
delegate.openShortcutItemIfPresent()
}
animatePresentClassesView()
classesViewController.loadAnnouncements(reloadClasses: false)
}
func animatePresentClassesView() {
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
self.formView.alpha = 0.0
self.formView.transform = CGAffineTransformMakeScale(0.6, 0.6)
self.containerLeading.constant = 0.0
self.view.layoutIfNeeded()
}, completion: nil)
}
func unpresentClassesView() {
unpresentClassesView(0.5)
}
func unpresentClassesView(duration: Double) {
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
self.formView.alpha = 1.0
self.formView.transform = CGAffineTransformMakeScale(1.0, 1.0)
self.containerLeading.constant = self.view.frame.width
self.view.layoutIfNeeded()
}, completion: nil)
}
//logout from gatech login service and trash saved passwords
func showLogoutAlert() {
let alert = UIAlertController(title: nil, message: "Are you sure you want to log out?", preferredStyle: iPad() ? .Alert : .ActionSheet)
alert.addAction(UIAlertAction(title: "Log out", style: .Destructive, handler: { _ in self.logout() }))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
func logout() {
self.classesViewController.announcements = []
self.classesViewController.classes = nil
NSURLCache.sharedURLCache().removeAllCachedResponses()
HttpClient.clearCookies()
HttpClient.sessionID = nil
HttpClient.authFormPost = nil
HttpClient.authLTPost = nil
TSAuthenticatedReader = nil
self.usernameField.text = ""
self.passwordField.text = ""
self.setSavedCredentials(correct: false)
NSUserDefaults.standardUserDefaults().setValue(nil, forKey: TSClassOpenCountKey)
Class.updateShortcutItems()
unpresentClassesView()
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
if self.containerLeading.constant != 0 {
delay(0.01) {
self.containerLeading.constant = self.view.frame.width
self.view.layoutIfNeeded()
}
}
if self.webViewTop.constant != 0 {
delay(0.01) {
self.webViewTop.constant = self.view.frame.height
self.view.layoutIfNeeded()
}
}
}
//MARK: - Methods that shouldn't be in this View Controller
var webViewVisible = false
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return webViewVisible ? .LightContent : .Default
}
func presentWebViewWithURL(URL: NSURL, title: String) {
webViewVisible = true
browserViewController.openLink("\(URL)")
presentWebViewWithTitle(title)
}
func presentWebViewWithText(text: String, title: String) {
webViewVisible = true
browserViewController.renderText(text)
presentWebViewWithTitle(title)
}
private func presentWebViewWithTitle(title: String) {
browserViewController.previousURL = nil
browserViewController.titleLabel.text = title
UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: [], animations: {
self.webViewTop.constant = 0.0
self.setNeedsStatusBarAppearanceUpdate()
self.view.layoutIfNeeded()
}, completion: nil)
}
func dismissWebView() {
dismissWebView(0.5)
}
func dismissWebView(duration: Double) {
webViewVisible = false
postNotification(TSPerformingNetworkActivityNotification, object: false)
UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: [], animations: {
self.webViewTop.constant = self.view.frame.height
self.view.layoutIfNeeded()
self.setNeedsStatusBarAppearanceUpdate()
}, completion: nil)
}
}