-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditAccountViewController.swift
More file actions
168 lines (127 loc) · 5.9 KB
/
Copy pathEditAccountViewController.swift
File metadata and controls
168 lines (127 loc) · 5.9 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
//
// EditAccountViewController.swift
// Absinthe-iOS
//
// Created by Alyssa Torres on 10/14/16.
// Copyright © 2016 AppDelegates. All rights reserved.
//
import UIKit
import SwiftyJSON
import PKHUD
class EditAccountViewController: AccountBaseViewController {
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var emailGoodCheck: UIImageView!
@IBOutlet weak var saveButton: UIButton!
@IBAction func closeEditAccount(_ sender: AnyObject) {
self.navigationController!.popViewController(animated: true)
}
@IBAction func save(_ sender: AnyObject) {
self.view.endEditing(true)
guard let first = self.firstName.text
else {
showAlert("Oops!", message: "The information you put in is not valid.")
return
}
guard let last = self.lastName.text
else {
showAlert("Oops!", message: "The information you put in is not valid.")
return
}
guard let email = self.email.text
else {
showAlert("Oops!", message: "The information you put in is not valid.")
return
}
let alertController = UIAlertController(title: "Save Changes", message: "Are you sure you want to save changes to your account information?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "No", style: .cancel) { (action) in }
alertController.addAction(cancelAction)
// TODO: change account information with call to Asahi and store new info in Settings
let okAction = UIAlertAction(title: "Yes", style: .default) { (action) in
HUD.show(.progress)
if let uid = Settings.sharedInstance.userId {
Asahi.sharedInstance.changeAccountInfo(first, lastName: last, email: email, userId: uid)
.then{ response -> Void in
Settings.sharedInstance.userEmail = email
Settings.sharedInstance.userFirstName = first
Settings.sharedInstance.userLastName = last
HUD.flash(.success, delay:0.7)
}
.catch{ err -> Void in
HUD.hide()
self.showAlert("Unable to change account info", message: "")
}
} else {
HUD.hide()
self.showAlert("Unable to change account info", message: "")
}
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
self.emailGoodCheck.alpha = 0
self.saveButton.alpha = 0
self.firstName.text = Settings.sharedInstance.userFirstName
self.lastName.text = Settings.sharedInstance.userLastName
self.email.text = Settings.sharedInstance.userEmail
checkEmail()
Asahi.sharedInstance.checkAuthStatus()
.then{ response -> Void in
log.debug("Successfully checked auth")
if let first = response["firstName"].string {
Settings.sharedInstance.userFirstName = first
self.firstName.text = first
}
if let last = response["lastName"].string {
Settings.sharedInstance.userLastName = last
self.lastName.text = last
}
if let email = response["auth"]["email"].string {
Settings.sharedInstance.userEmail = email
self.email.text = email
self.checkEmail()
}
if let userId = response["id"].string {
Settings.sharedInstance.userId = userId
}
}
// TODO: how do we want to handle this error?
.catch{ err -> Void in
log.error("Error checking auth")
print(err)
let alertController = UIAlertController(title: "Error loading account information", message: "There was an issue loading your account information.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
NotificationCenter.default.addObserver(self, selector: #selector(checkEmail), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func checkEmail() {
if let email = self.email.text {
if email.isValidEmail() && self.emailGoodCheck.alpha == 0 {
fadeIn(self.emailGoodCheck)
fadeIn(self.saveButton)
}
if !email.isValidEmail() {
fadeOut(self.emailGoodCheck)
fadeOut(self.saveButton)
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}