-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignUpViewController.swift
209 lines (174 loc) · 8.68 KB
/
SignUpViewController.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
//
// SignUpViewController.swift
// runway
//
// Created by Akshaya Jagadeesh on 7/25/20.
// Copyright © 2020 Akshaya Jagadeesh. All rights reserved.
//
import UIKit
import AVFoundation
import Firebase
import FirebaseDatabase
import FirebaseAuth
import FirebaseStorage
class SignUpViewController: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var fullNameField: UITextField!
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var passwordField: UITextField!
@IBOutlet weak var confirmPasswordField: UITextField!
@IBOutlet weak var passwordLength: UILabel!
@IBOutlet weak var confirmError: UILabel!
@IBOutlet weak var next1Outlet: UIButton!
@IBOutlet weak var otherError: UILabel!
var imagePicker: UIImagePickerController!
@IBOutlet weak var profileImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(changeProfilePic))
tapGestureRecognizer.numberOfTapsRequired = 1
profileImageView.addGestureRecognizer(tapGestureRecognizer)
profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
}
@IBAction func editProfile(_ sender: Any) {
self.present(imagePicker, animated: true, completion: nil)
print("tapped")
}
@objc func changeProfilePic () {
self.present(imagePicker, animated: true, completion: nil)
print("tapped")
}
var check = 0
func checkLength () {
if (passwordField?.text!.count)! < 7 && passwordField?.text != "" {
print(passwordField?.text!.count ?? 0)
passwordField.isHidden = false
//let utterance = AVSpeechUtterance(string: "Password should be atleast seven characters long, please try again.")
passwordField?.text = ""
confirmPasswordField?.text = ""
} else {
check += 1
}
}
func checkIfMatch () {
if confirmPasswordField?.text != passwordField?.text {
confirmError?.isHidden = false
//let utterance = AVSpeechUtterance(string: "The passwords do not match, please try again.")
confirmPasswordField?.text = ""
passwordField?.text = ""
} else {
confirmError?.isHidden = true
check += 1
}
}
func checkForEmpty () {
if emailField?.text == "" || fullNameField?.text == "" || passwordField?.text == "" || confirmPasswordField?.text == "" || usernameField?.text == "" {
//let utterance = AVSpeechUtterance(string: "All fields are required, please fill all of them before clicking next.")
} else {
check += 1
}
}
@IBAction func next1Btn(_ sender: UIButton) {
checkForEmpty()
checkLength()
checkIfMatch()
if check == 3 {
if let email = emailField?.text, let password = passwordField?.text, let username = usernameField?.text, let name = fullNameField?.text,let image = profileImageView.image{
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if error == nil && authResult != nil {
print("User created!")
self.dismiss(animated: false, completion: nil)
uploadProfilePic(image) { url in
if url != nil {
let changeRequest = Auth.auth().currentUser?.createProfileChangeRequest()
changeRequest?.displayName = username
changeRequest?.photoURL = url
changeRequest?.commitChanges { error in
if error == nil {
print("User display name changed!")
saveProfile(username: username, profileImageURL: url!, name: name) { success in
if success {
self.dismiss(animated: true, completion: nil)
} else {
self.reset()
self.otherError?.text = error!.localizedDescription
let utterance = AVSpeechUtterance(string: error!.localizedDescription + "please type a new username or password")
}
}
//self.dismiss(animated: false, completion: nil)
} else {
print("error: \(error!.localizedDescription)")
self.reset()
self.otherError?.text = error!.localizedDescription
let utterance = AVSpeechUtterance(string: error!.localizedDescription + "please type a new username or password")
}
}
} else {
//Error unable to upload profile image
self.reset()
self.otherError?.text = error!.localizedDescription
let utterance = AVSpeechUtterance(string: error!.localizedDescription + "please type a new username or password")
}
}
} else {
print("error: \(error!.localizedDescription)")
self.reset()
self.otherError?.text = error!.localizedDescription
let utterance = AVSpeechUtterance(string: error!.localizedDescription + "please type a new username or password")
}
}
}
func uploadProfilePic (_ image: UIImage, completion: @escaping ((_ url: URL?) -> ())) {
guard let uid = Auth.auth().currentUser?.uid else {return}
let storageRef = Storage.storage().reference().child("user/\(uid)")
guard let imageData = image.jpegData(compressionQuality: 0.75) else {return}
let metaData = StorageMetadata()
metaData.contentType = "img/jpg"
storageRef.putData(imageData, metadata: metaData) {metaData, error in
if error == nil, metaData != nil {
storageRef.downloadURL { url, error in
completion(url)
// success!
}
} else {
//failure
completion(nil)
}
}
}
func saveProfile (username: String, profileImageURL: URL, name: String, completion: @escaping ((_ success: Bool)->()) ) {
guard let uid = Auth.auth().currentUser?.uid else {return}
let databaseRef = Database.database().reference().child("users/profile/\(uid)")
let userObject = [
"username" : username,
"photoURL" : profileImageURL.absoluteString,
"name" : name
] as [String:Any]
databaseRef.setValue(userObject) { error, ref in
completion(error == nil)
}
}
}
check = 0
}
func reset() {
usernameField?.text = ""
passwordField?.text = ""
confirmPasswordField?.text = ""
}
}
extension SignUpViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.profileImageView.image = pickedImage
}
}
}