This repository has been archived by the owner on May 6, 2024. It is now read-only.
/
AccountViewController.swift
217 lines (181 loc) · 8.12 KB
/
AccountViewController.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
//
// AccountViewController.swift
// edX
//
// Created by Salman on 15/08/2017.
// Copyright © 2017 edX. All rights reserved.
//
import UIKit
import MessageUI
fileprivate enum AccountviewOptions : Int {
case Profile,
UserSettings,
SubmitFeedback,
Logout
static let accountOptions = [Profile, UserSettings, SubmitFeedback, Logout]
}
class AccountViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, InterfaceOrientationOverriding {
private let contentView = UIView()
private let tableView = UITableView()
private let versionLabel = UILabel()
typealias Environment = OEXAnalyticsProvider & OEXConfigProvider & OEXSessionProvider & OEXStylesProvider & OEXRouterProvider
fileprivate let environment: Environment
init(environment: Environment) {
self.environment = environment
super.init(nibName: nil, bundle :nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = Strings.userAccount
view.backgroundColor = environment.styles.standardBackgroundColor()
view.addSubview(contentView)
contentView.addSubview(tableView)
contentView.addSubview(versionLabel)
configureViews()
addCloseButton()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
environment.analytics.trackScreen(withName: AnalyticsDisplayName.AccountSettings.rawValue)
}
func configureViews() {
tableView.estimatedRowHeight = 40
tableView.rowHeight = UITableView.automaticDimension
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView()
tableView.backgroundColor = UIColor.clear
tableView.register(AccountViewCell.self, forCellReuseIdentifier: AccountViewCell.identifier)
let textStyle = OEXMutableTextStyle(weight: .normal, size: .base, color : OEXStyles.shared().neutralBlack())
textStyle.alignment = NSTextAlignment.center
versionLabel.attributedText = textStyle.attributedString(withText: Strings.versionDisplay(number: Bundle.main.oex_displayVersionString(), environment: ""))
versionLabel.accessibilityIdentifier = "AccountViewController:version-label"
tableView.accessibilityIdentifier = "AccountViewController:table-view"
addConstraints()
}
private func addCloseButton() {
if (isModal()) { //isModal check if the view is presented then add close button
let closeButton = UIBarButtonItem(title: Strings.close, style: .plain, target: nil, action: nil)
closeButton.accessibilityLabel = Strings.Accessibility.closeLabel
closeButton.accessibilityHint = Strings.Accessibility.closeHint
closeButton.accessibilityIdentifier = "AccountViewController:close-button"
navigationItem.rightBarButtonItem = closeButton
closeButton.oex_setAction { [weak self] in
self?.dismiss(animated: true, completion: nil)
}
}
}
func addConstraints() {
contentView.snp.makeConstraints { make in
make.edges.equalTo(safeEdges)
}
tableView.snp.makeConstraints { make in
make.top.equalTo(contentView)
make.leading.equalTo(contentView)
make.trailing.equalTo(contentView)
make.bottom.equalTo(versionLabel.snp.top)
}
versionLabel.snp.makeConstraints { make in
make.top.equalTo(tableView.snp.bottom)
make.leading.equalTo(contentView)
make.trailing.equalTo(contentView)
make.bottom.equalTo(contentView).inset(20)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AccountviewOptions.accountOptions.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Configure the cell...
let cell = tableView.dequeueReusableCell(withIdentifier: AccountViewCell.identifier, for: indexPath) as! AccountViewCell
cell.separatorInset = UIEdgeInsets.zero
cell.accessoryType = accessoryType(option: AccountviewOptions.accountOptions[indexPath.row])
cell.title = optionTitle(option: AccountviewOptions.accountOptions[indexPath.row])
cell.accessibilityIdentifier = "AccountViewController:table-cell"
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let option = AccountviewOptions(rawValue: indexPath.row) {
switch option {
case .UserSettings:
environment.router?.showMySettings(controller: self)
case .Profile:
guard environment.config.profilesEnabled, let currentUserName = environment.session.currentUser?.username else { break }
environment.router?.showProfileForUsername(controller: self, username: currentUserName, editable: true)
case .SubmitFeedback:
launchEmailComposer()
case .Logout:
OEXFileUtility.nukeUserPIIData()
dismiss(animated: true, completion: { [weak self] in
self?.environment.router?.logout()
})
}
}
tableView.deselectRow(at: indexPath, animated: true)
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.row == AccountviewOptions.Profile.rawValue && !environment.config.profilesEnabled) {
return 0
}
return tableView.estimatedRowHeight
}
private func accessoryType(option: AccountviewOptions) -> UITableViewCell.AccessoryType{
switch option {
case .SubmitFeedback, .Logout:
return .none
default:
return .disclosureIndicator
}
}
private func optionTitle(option: AccountviewOptions) -> String? {
switch option {
case .UserSettings :
return Strings.settings
case .Profile:
guard environment.config.profilesEnabled else { break }
return Strings.UserAccount.profile
case .SubmitFeedback:
return Strings.SubmitFeedback.optionTitle
case .Logout:
return Strings.logout
}
return nil
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .allButUpsideDown
}
}
extension AccountViewController : MFMailComposeViewControllerDelegate {
func launchEmailComposer() {
if !MFMailComposeViewController.canSendMail() {
UIAlertController().showAlert(withTitle: Strings.emailAccountNotSetUpTitle, message: Strings.emailAccountNotSetUpMessage, onViewController: self)
} else {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.navigationBar.tintColor = OEXStyles.shared().navigationItemTintColor()
mail.setSubject(Strings.SubmitFeedback.messageSubject)
mail.setMessageBody(EmailTemplates.supportEmailMessageTemplate(), isHTML: false)
if let fbAddress = environment.config.feedbackEmailAddress() {
mail.setToRecipients([fbAddress])
}
present(mail, animated: true, completion: nil)
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)
}
}