-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditAccountViewController.swift
More file actions
152 lines (118 loc) · 6.28 KB
/
EditAccountViewController.swift
File metadata and controls
152 lines (118 loc) · 6.28 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
//
// 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: UITableViewController {
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
//@IBOutlet weak var email: UITextField!
@IBOutlet weak var emailErrorLabel: UILabel!
@IBOutlet weak var errorBlock: UIView!
@IBOutlet weak var errorBlockLabel: UILabel!
@IBOutlet var emailLabel: UILabel!
var firstNameDelegate: CustomTextFieldDelegate?
var lastNameDelegate: CustomTextFieldDelegate?
var emailDelegate: CustomTextFieldDelegate?
@IBAction func save(_ sender: AnyObject) {
self.view.endEditing(true)
errorBlock.isHidden = true
guard let first = firstName.text, isValidName(first),
let last = lastName.text, isValidName(last)
//let email = email.text, isValidEmail(email)
else {
firstNameDelegate?.textFieldDidEndEditing(firstName)
lastNameDelegate?.textFieldDidEndEditing(lastName)
//emailDelegate?.textFieldDidEndEditing(self.email)
return
}
let alertController = UIAlertController(title: "Save",
message: "Are you sure you want to save these changes?",
preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "No", style: .cancel) { (action) in }
alertController.addAction(cancelAction)
let okAction = UIAlertAction(title: "Yes", style: .default) { (action) in
HUD.show(.progress)
if let uid = Settings.sharedInstance.userId {
OGCloud.sharedInstance.changeAccountInfo(first, lastName: last, email: Settings.sharedInstance.userEmail!, 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{ error -> Void in
switch error {
case OGCloudError.authFailure:
self.errorBlockLabel.text = "Sorry, it looks like you aren't authorized to change this account's information!"
self.errorBlock.isHidden = false
case OGCloudError.tokenInvalid:
let alertController = UIAlertController(
title: "Uh oh!",
message: "It looks like your session has expired. Please log back in.",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
OGCloud.sharedInstance.logout()
.always{
self.performSegue(withIdentifier: "fromEditAccountToRegistration", sender: nil)
}
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
default:
self.errorBlockLabel.text = "Uh oh! Something went wrong changing your account info."
self.errorBlock.isHidden = false
}
HUD.hide()
}
} else {
self.errorBlockLabel.text = "Oh no! It looks like your account info might be out of date. Try logging out and logging back in."
self.errorBlock.isHidden = false
HUD.hide()
}
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
errorBlock.isHidden = true
emailLabel.textColor = Style.lightGreyColor
firstNameDelegate = CustomTextFieldDelegate(firstName, isValid: isValidName, inTableView: true)
lastNameDelegate = CustomTextFieldDelegate(lastName, isValid: isValidName, inTableView: true)
// emailDelegate = CustomTextFieldDelegate(email, isValid: isValidEmail,
// errorLabel: emailErrorLabel,
// inTableView: true)
OGCloud.sharedInstance.checkSession()
.then { _ -> Void in
self.firstName.text = Settings.sharedInstance.userFirstName
self.lastName.text = Settings.sharedInstance.userLastName
//self.email.text = Settings.sharedInstance.userEmail
self.emailLabel.text = Settings.sharedInstance.userEmail
}.catch { error -> Void in
let alertController = UIAlertController(title: "Uh oh!", message: "There was an issue getting your account information.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { (action) in
self.performSegue(withIdentifier: "unwindToSettings", sender: nil)
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
func isValidEmail(_ string: String?) -> Bool {
if let str = string {
return str.isValidEmail()
}
return false
}
func isValidName(_ string: String?) -> Bool {
if let str = string, str != "" {
return true
}
return false
}
}