-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountViewController.swift
More file actions
130 lines (97 loc) · 4.22 KB
/
Copy pathAccountViewController.swift
File metadata and controls
130 lines (97 loc) · 4.22 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
//
// AccountViewController.swift
// Belashi-iOS
//
// Created by Noah on 8/1/16.
// Copyright © 2016 AppDelegates. All rights reserved.
//
import UIKit
import PKHUD
struct SettingsOption {
let label: String
let image: String?
init(label: String) {
self.label = label
self.image = nil
}
init(label: String, image: String) {
self.label = label
self.image = image
}
}
class AccountViewController : AccountBaseViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
var tableView: UITableView!
let options = [
//SettingsOption(label: "Invite Friends", image: "ic_card_giftcard_white_18pt"),
SettingsOption(label: "Edit Account", image: "ic_perm_identity_white_18pt"),
//SettingsOption(label: "Add New Ourglass Device", image: "ic_queue_play_next_white_18pt"),
// SettingsOption(label: "Add/Manage Venues", image: "ic_add_location_white_18pt"),
SettingsOption(label: "Log Out", image: "ic_first_page_white_18pt")]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView = self.view as! UITableView
self.tableView.delegate = self
self.tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return options.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
switch options[indexPath.row].label {
case "Invite Friends":
inviteFriends()
case "Edit Account":
self.performSegue(withIdentifier: "fromAccountToEdit", sender: nil)
case "Log Out":
logout()
default:
break
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "settingsCell", for: indexPath) as! SettingsCell
cell.isUserInteractionEnabled = true
cell.label.text = options[indexPath.row].label
if let image = options[indexPath.row].image {
cell.icon.image = UIImage(named: image)
}
return cell
}
func logout() {
let alertController = UIAlertController(title: "Log out", message: "Are you sure you want to log out?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "No", style: .cancel) { (action) in
}
alertController.addAction(cancelAction)
let okAction = UIAlertAction(title: "Yes", style: .default) { (action) in
Asahi.sharedInstance.logout()
.then{ response -> Void in
Settings.sharedInstance.userAsahiJWT = nil
Settings.sharedInstance.userPassword = nil
HUD.flash(.labeledSuccess(title: "Logged out!", subtitle: ""), delay: 1.0, completion: { (_) in
self.performSegue(withIdentifier: "fromAccountToRegistration", sender: nil)
})
}
// TODO: is this how we should handle errors?
.catch{ err -> Void in
log.error("Error logging out")
Settings.sharedInstance.userAsahiJWT = nil
Settings.sharedInstance.userPassword = nil
HUD.flash(.labeledSuccess(title: "Logged out!", subtitle: ""), delay: 1.0, completion: { (_) in
self.performSegue(withIdentifier: "fromAccountToRegistration", sender: nil)
})
}
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
func inviteFriends() {
self.performSegue(withIdentifier: "fromAccountToInvite", sender: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}