This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
LogRegPromptViewController.swift
399 lines (329 loc) · 13.5 KB
/
LogRegPromptViewController.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
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
//
// LogRegPromptViewController.swift
// Numu Tracker
//
// Created by Bradley Root on 9/4/17.
// Copyright © 2017 Numu Tracker. All rights reserved.
//
import Foundation
import UIKit
import Crashlytics
class LogRegPromptViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var topScrollView: UIScrollView!
@IBOutlet weak var middleScrollView: UIScrollView!
@IBOutlet weak var bottomScrollView: UIScrollView!
var topScrollDrection: Int = 0
var bottomScrollDrection: Int = 0
var middleScrollDrection: Int = 0
var arts: [String] = []
@IBOutlet weak var signUpFormView: UIView!
@IBOutlet weak var signUpEmailTextField: NumuTextField!
@IBOutlet weak var signUpPasswordTextField: NumuTextField!
@IBOutlet weak var signUpLabel: UILabel!
@IBOutlet weak var logInFormView: UIView!
@IBOutlet weak var logInEmailTextField: NumuTextField!
@IBOutlet weak var logInPasswordTextField: NumuTextField!
@IBOutlet weak var logInLabel: UILabel!
@IBOutlet weak var numuIntroLabel: UILabel!
@IBAction func logInButtonPree(_ sender: Any) {
var animationDuration = 0.5
// if sign up form is showing, hide it
if signUpFormView.alpha == 1 {
self.signUpFormView.alpha = 0
animationDuration = 0
}
if logInFormView.alpha == 0 {
UIView.animate(withDuration: animationDuration) {
self.logInFormView.alpha = 1
self.logInEmailTextField.becomeFirstResponder()
}
} else {
UIView.animate(withDuration: animationDuration) {
self.logInFormView.alpha = 0
}
self.view.endEditing(true)
}
}
@IBAction func signUpButtonPress(_ sender: Any) {
var animationDuration = 0.5
// if log in form is showing, hide it
if logInFormView.alpha == 1 {
self.logInFormView.alpha = 0
animationDuration = 0
}
if signUpFormView.alpha == 0 {
UIView.animate(withDuration: animationDuration) {
self.signUpFormView.alpha = 1
self.signUpEmailTextField.becomeFirstResponder()
}
} else {
UIView.animate(withDuration: animationDuration) {
self.signUpFormView.alpha = 0
}
self.view.endEditing(true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
isModalInPresentation = true
}
self.signUpPasswordTextField.delegate = self
self.signUpEmailTextField.delegate = self
self.logInEmailTextField.delegate = self
self.logInPasswordTextField.delegate = self
signUpFormView.alpha = 0
logInFormView.alpha = 0
let width = self.view.frame.size.width
var height: CGFloat = 0
var maxLength: CGFloat = 0
height = width / 2
maxLength = height * 6
self.topScrollView.isScrollEnabled = true
self.topScrollView.alwaysBounceHorizontal = true
self.topScrollView.contentSize.width = maxLength
self.middleScrollView.isScrollEnabled = true
self.middleScrollView.alwaysBounceHorizontal = true
self.middleScrollView.contentSize.width = maxLength
self.bottomScrollView.isScrollEnabled = true
self.bottomScrollView.alwaysBounceHorizontal = true
self.bottomScrollView.contentSize.width = maxLength
// Load list of recent releases
NumuClient.shared.getArt {[weak self](arts) in
self?.arts = arts
DispatchQueue.main.async(execute: {
if let topArtists = self?.arts[0..<12],
let midArtists = self?.arts[12..<18],
let botArtists = self?.arts[18..<30] {
self?.loadArts(scrollView: (self?.topScrollView)!, images: Array(topArtists))
self?.loadArts(scrollView: (self?.middleScrollView)!, images: Array(midArtists))
self?.loadArts(scrollView: (self?.bottomScrollView)!, images: Array(botArtists))
let width = self?.view.frame.size.width
var height: CGFloat = 0
height = width! / 3
maxLength = height * 12
//self?.bottomScrollView.setContentOffset(CGPoint(x:maxLength-width!,y:0), animated: false)
_ = Timer.scheduledTimer(
timeInterval: 0.025,
target: self!,
selector: #selector(self?.autoScroll),
userInfo: nil,
repeats: true)
}
})
}
Answers.logCustomEvent(withName: "Login / Signup Screen", customAttributes: nil)
}
override func viewWillAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func loadArts(scrollView: UIScrollView, images: [String]) {
// get width of screen
let width = self.view.frame.size.width
var height: CGFloat = 0
if scrollView.restorationIdentifier == "middle" {
height = width / 2
} else {
height = width / 4
}
let imageWidth: CGFloat = height
let imageHeight: CGFloat = height
var xPosition: CGFloat = 0
for image in images {
let imageURL = NSURL(string: image)
if let image = imageURL?.cachedImage {
let artImage = UIImageView()
artImage.image = image
artImage.frame.size.width = imageWidth
artImage.frame.size.height = imageHeight
artImage.frame.origin.x = xPosition
artImage.frame.origin.y = 0
scrollView.addSubview(artImage)
xPosition += imageWidth
} else {
imageURL?.fetchImage { image in
let artImage = UIImageView()
artImage.image = image
artImage.frame.size.width = imageWidth
artImage.frame.size.height = imageHeight
artImage.frame.origin.x = xPosition
artImage.frame.origin.y = 0
scrollView.addSubview(artImage)
xPosition += imageWidth
}
}
}
}
@objc func autoScroll() {
let scrollViewArray: [UIScrollView] = [self.topScrollView, self.middleScrollView, self.bottomScrollView]
for scrollView in scrollViewArray {
let width = self.view.frame.size.width
var height: CGFloat = 0
var maxLength: CGFloat = 0
let offset = scrollView.contentOffset
var newOffset: CGFloat = 0
let intervalPixels: CGFloat = 0.50
// Determine length:
if scrollView.restorationIdentifier == "middle" {
height = width / 2
maxLength = height * 6
} else {
height = width / 4
maxLength = height * 12
}
let maxOffset = maxLength - width
//print("Data: Max \(maxOffset) and \(offset.x)")
if offset.x >= maxOffset {
switch scrollView.restorationIdentifier! {
case "middle":
self.middleScrollDrection = 1
case "top":
self.topScrollDrection = 1
case "bottom":
self.bottomScrollDrection = 1
default:
print("Borked")
}
}
if offset.x <= 0 {
switch scrollView.restorationIdentifier! {
case "middle":
self.middleScrollDrection = 0
case "top":
self.topScrollDrection = 0
case "bottom":
self.bottomScrollDrection = 0
default:
print("Borked")
}
}
switch scrollView.restorationIdentifier! {
case "middle":
if self.middleScrollDrection == 0 {
newOffset = offset.x + intervalPixels
} else {
newOffset = offset.x - intervalPixels
}
case "top":
if self.topScrollDrection == 0 {
newOffset = offset.x + intervalPixels
} else {
newOffset = offset.x - intervalPixels
}
case "bottom":
if self.bottomScrollDrection == 0 {
newOffset = offset.x + intervalPixels
} else {
newOffset = offset.x - intervalPixels
}
default:
print("Borked")
}
scrollView.setContentOffset(CGPoint(x: newOffset, y: 0), animated: false)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.returnKeyType == .next {
if textField.restorationIdentifier == "signEmail" {
signUpPasswordTextField.becomeFirstResponder()
} else {
logInPasswordTextField.becomeFirstResponder()
}
return true
}
if textField.returnKeyType == .go {
if textField.restorationIdentifier == "signPassword" {
return self.goSignUp()
} else {
return self.goLogIn()
}
}
return false
}
func goLogIn() -> Bool {
self.logInLabel.text = "Logging in..."
let username = self.logInEmailTextField.text
let password = self.logInPasswordTextField.text
// Check Credentials
NumuClient.shared.authorizeLogIn(username: username!, password: password!) { (result) in
DispatchQueue.main.async(execute: {
if result == "1" {
// Login Success
self.logInLabel.text = "Logged in!"
defaults.logged = true
// Update interface elsewhere
NotificationCenter.default.post(name: .LoggedIn, object: self)
NotificationCenter.default.post(name: .UpdatedArtists, object: self)
// Close keyboard
self.logInPasswordTextField.resignFirstResponder()
// Log to Answers
Answers.logLogin(withMethod: "LogRegPrompt", success: true, customAttributes: nil)
// Pop viewcontroller
self.dismiss(animated: true, completion: nil)
} else {
// Login Failure
self.logInLabel.text = result
// Log to Answers
Answers.logLogin(withMethod: "LogRegPrompt", success: false, customAttributes: nil)
}
})
}
return true
}
func goSignUp() -> Bool {
self.signUpLabel.text = "Signing up...\nPlease wait..."
let username = self.signUpEmailTextField.text
let password = self.signUpPasswordTextField.text
let emailVerify = isValidEmail(testStr: username!)
var errorText: String = ""
var errorInt: Bool = false
if password!.isEmpty {
errorText = "Please enter a password."
errorInt = true
} else if password!.count < 8 {
errorText = "Password needs to be at least 8 characters."
errorInt = true
} else if !emailVerify {
errorText = "Please enter a valid email."
errorInt = true
}
if !errorInt {
// Check Credentials
NumuClient.shared.authorizeRegister(username: username!, password: password!) { (result) in
DispatchQueue.main.async(execute: {
if result == "1" {
// Registration Success
self.signUpLabel.text = "Registration Successful!"
defaults.logged = true
// Update interface elsewhere
NotificationCenter.default.post(name: .LoggedIn, object: self)
NotificationCenter.default.post(name: .UpdatedArtists, object: self)
// Close keyboard
self.logInPasswordTextField.resignFirstResponder()
// Log to Answers
Answers.logSignUp(withMethod: "LogRegPrompt", success: true, customAttributes: nil)
// Pop viewcontroller
self.dismiss(animated: true, completion: nil)
} else {
// Registration Failure
self.signUpLabel.text = result
// Log to Answers
Answers.logSignUp(withMethod: "LogRegPrompt", success: false, customAttributes: nil)
}
})
}
return true
} else {
self.signUpLabel.text = errorText
return false
}
}
func isValidEmail(testStr: String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: testStr)
}
}