-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SupportTableViewController.swift
338 lines (271 loc) · 13.2 KB
/
SupportTableViewController.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import UIKit
import WordPressAuthenticator
class SupportTableViewController: UITableViewController {
// MARK: - Properties
var sourceTag: WordPressSupportSourceTag?
// If set, the Zendesk views will be shown from this view instead of in the navigation controller.
// Specifically for Me > Help & Support on the iPad.
var showHelpFromViewController: UIViewController?
private var tableHandler: ImmuTableViewHandler?
private let userDefaults = UserDefaults.standard
// MARK: - Init
override init(style: UITableView.Style) {
super.init(style: style)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
required convenience init() {
self.init(style: .grouped)
}
// MARK: - View
override func viewDidLoad() {
super.viewDidLoad()
WPAnalytics.track(.openedSupport)
setupNavBar()
setupTable()
checkForAutomatticEmail()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadViewModel()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
createUserActivity()
}
@objc func showFromTabBar() {
let navigationController = UINavigationController.init(rootViewController: self)
if WPDeviceIdentification.isiPad() {
navigationController.modalTransitionStyle = .crossDissolve
navigationController.modalPresentationStyle = .formSheet
}
let tabBarController = WPTabBarController.sharedInstance()
if let presentedVC = tabBarController?.presentedViewController {
presentedVC.present(navigationController, animated: true)
} else {
tabBarController?.present(navigationController, animated: true)
}
}
// MARK: - Button Actions
@IBAction func dismissPressed(_ sender: AnyObject) {
dismiss(animated: true)
}
}
// MARK: - Private Extension
private extension SupportTableViewController {
func registerObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(refreshNotificationIndicator(_:)), name: .ZendeskPushNotificationReceivedNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(refreshNotificationIndicator(_:)), name: .ZendeskPushNotificationClearedNotification, object: nil)
}
func setupNavBar() {
title = LocalizedText.viewTitle
if isModal() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: LocalizedText.closeButton,
style: WPStyleGuide.barButtonStyleForBordered(),
target: self,
action: #selector(SupportTableViewController.dismissPressed(_:)))
}
}
func setupTable() {
ImmuTable.registerRows([SwitchRow.self,
NavigationItemRow.self,
TextRow.self,
HelpRow.self,
SupportEmailRow.self],
tableView: tableView)
tableHandler = ImmuTableViewHandler(takeOver: self)
reloadViewModel()
WPStyleGuide.configureColors(for: view, andTableView: tableView)
// remove empty cells
tableView.tableFooterView = UIView()
registerObservers()
}
// MARK: - Table Model
func tableViewModel() -> ImmuTable {
// Help Section
var helpSectionRows = [ImmuTableRow]()
helpSectionRows.append(HelpRow(title: LocalizedText.wpHelpCenter, action: helpCenterSelected()))
if ZendeskUtils.zendeskEnabled {
helpSectionRows.append(HelpRow(title: LocalizedText.contactUs, action: contactUsSelected()))
helpSectionRows.append(HelpRow(title: LocalizedText.myTickets, action: myTicketsSelected(), showIndicator: ZendeskUtils.showSupportNotificationIndicator))
helpSectionRows.append(SupportEmailRow(title: LocalizedText.contactEmail,
value: ZendeskUtils.userSupportEmail() ?? LocalizedText.emailNotSet,
action: supportEmailSelected()))
} else {
helpSectionRows.append(HelpRow(title: LocalizedText.wpForums, action: contactUsSelected()))
}
let helpSection = ImmuTableSection(
headerText: nil,
rows: helpSectionRows,
footerText: LocalizedText.helpFooter)
// Information Section
let versionRow = TextRow(title: LocalizedText.version, value: Bundle.main.shortVersionString())
let switchRow = SwitchRow(title: LocalizedText.extraDebug,
value: userDefaults.bool(forKey: UserDefaultsKeys.extraDebug),
onChange: extraDebugToggled())
let logsRow = NavigationItemRow(title: LocalizedText.activityLogs, action: activityLogsSelected())
let informationSection = ImmuTableSection(
headerText: nil,
rows: [versionRow, switchRow, logsRow],
footerText: LocalizedText.informationFooter)
// Create and return table
return ImmuTable(sections: [helpSection, informationSection])
}
@objc func refreshNotificationIndicator(_ notification: Foundation.Notification) {
reloadViewModel()
}
func reloadViewModel() {
tableHandler?.viewModel = tableViewModel()
}
// MARK: - Row Handlers
func helpCenterSelected() -> ImmuTableAction {
return { [unowned self] row in
self.tableView.deselectSelectedRowWithAnimation(true)
if ZendeskUtils.zendeskEnabled {
guard let controllerToShowFrom = self.controllerToShowFrom() else {
return
}
ZendeskUtils.sharedInstance.showHelpCenterIfPossible(from: controllerToShowFrom, with: self.sourceTag)
} else {
guard let url = Constants.appSupportURL else {
return
}
UIApplication.shared.open(url)
}
}
}
func contactUsSelected() -> ImmuTableAction {
return { [unowned self] row in
self.tableView.deselectSelectedRowWithAnimation(true)
if ZendeskUtils.zendeskEnabled {
guard let controllerToShowFrom = self.controllerToShowFrom() else {
return
}
ZendeskUtils.sharedInstance.showNewRequestIfPossible(from: controllerToShowFrom, with: self.sourceTag)
} else {
guard let url = Constants.forumsURL else {
return
}
UIApplication.shared.open(url)
}
}
}
func myTicketsSelected() -> ImmuTableAction {
return { [unowned self] row in
ZendeskUtils.pushNotificationRead()
self.tableView.deselectSelectedRowWithAnimation(true)
guard let controllerToShowFrom = self.controllerToShowFrom() else {
return
}
ZendeskUtils.sharedInstance.showTicketListIfPossible(from: controllerToShowFrom, with: self.sourceTag)
}
}
func supportEmailSelected() -> ImmuTableAction {
return { [unowned self] row in
self.tableView.deselectSelectedRowWithAnimation(true)
guard let controllerToShowFrom = self.controllerToShowFrom() else {
return
}
WPAnalytics.track(.supportIdentityFormViewed)
ZendeskUtils.sharedInstance.showSupportEmailPrompt(from: controllerToShowFrom) { success in
guard success else {
return
}
// Tracking when the dialog's "OK" button is pressed, not necessarily
// if the value changed.
WPAnalytics.track(.supportIdentitySet)
self.reloadViewModel()
self.checkForAutomatticEmail()
}
}
}
/// Zendesk does not allow agents to submit tickets, and displays a 'Message failed to send' error upon attempt.
/// If the user email address is a8c, display a warning.
///
func checkForAutomatticEmail() {
guard let email = ZendeskUtils.userSupportEmail(),
(Constants.automatticEmails.first { email.contains($0) }) != nil else {
return
}
let alert = UIAlertController(title: "Warning",
message: "Automattic email account detected. Please log in with a non-Automattic email to submit or view support tickets.",
preferredStyle: .alert)
let cancel = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
func extraDebugToggled() -> (_ newValue: Bool) -> Void {
return { [unowned self] newValue in
self.userDefaults.set(newValue, forKey: UserDefaultsKeys.extraDebug)
WPLogger.configureLoggerLevelWithExtraDebug()
}
}
func activityLogsSelected() -> ImmuTableAction {
return { [unowned self] row in
let activityLogViewController = ActivityLogViewController()
self.navigationController?.pushViewController(activityLogViewController, animated: true)
}
}
// MARK: - ImmuTableRow Struct
struct HelpRow: ImmuTableRow {
static let cell = ImmuTableCell.class(WPTableViewCellIndicator.self)
let title: String
let showIndicator: Bool
let action: ImmuTableAction?
init(title: String, action: @escaping ImmuTableAction, showIndicator: Bool = false) {
self.title = title
self.showIndicator = showIndicator
self.action = action
}
func configureCell(_ cell: UITableViewCell) {
let cell = cell as! WPTableViewCellIndicator
cell.textLabel?.text = title
WPStyleGuide.configureTableViewCell(cell)
cell.textLabel?.textColor = WPStyleGuide.wordPressBlue()
cell.showIndicator = showIndicator
}
}
struct SupportEmailRow: ImmuTableRow {
static let cell = ImmuTableCell.class(WPTableViewCellValue1.self)
let title: String
let value: String
let action: ImmuTableAction?
func configureCell(_ cell: UITableViewCell) {
cell.textLabel?.text = title
cell.detailTextLabel?.text = value
WPStyleGuide.configureTableViewCell(cell)
cell.textLabel?.textColor = WPStyleGuide.wordPressBlue()
}
}
// MARK: - Helpers
func controllerToShowFrom() -> UIViewController? {
return showHelpFromViewController ?? navigationController ?? nil
}
// MARK: - Localized Text
struct LocalizedText {
static let viewTitle = NSLocalizedString("Support", comment: "View title for Support page.")
static let closeButton = NSLocalizedString("Close", comment: "Dismiss the current view")
static let wpHelpCenter = NSLocalizedString("WordPress Help Center", comment: "Option in Support view to launch the Help Center.")
static let contactUs = NSLocalizedString("Contact Us", comment: "Option in Support view to contact the support team.")
static let wpForums = NSLocalizedString("WordPress Forums", comment: "Option in Support view to view the Forums.")
static let myTickets = NSLocalizedString("My Tickets", comment: "Option in Support view to access previous help tickets.")
static let helpFooter = NSLocalizedString("Visit the Help Center to get answers to common questions, or contact us for more help.", comment: "Support screen footer text displayed when Zendesk is enabled.")
static let version = NSLocalizedString("Version", comment: "Label in Support view displaying the app version.")
static let extraDebug = NSLocalizedString("Extra Debug", comment: "Option in Support view to enable/disable adding extra information to support ticket.")
static let activityLogs = NSLocalizedString("Activity Logs", comment: "Option in Support view to see activity logs.")
static let informationFooter = NSLocalizedString("The Extra Debug feature includes additional information in activity logs, and can help us troubleshoot issues with the app.", comment: "Support screen footer text explaining the Extra Debug feature.")
static let contactEmail = NSLocalizedString("Contact Email", comment: "Support email label.")
static let emailNotSet = NSLocalizedString("Not Set", comment: "Display value for Support email field if there is no user email address.")
}
// MARK: - User Defaults Keys
struct UserDefaultsKeys {
static let extraDebug = "extra_debug"
}
// MARK: - Constants
struct Constants {
static let appSupportURL = URL(string: "https://apps.wordpress.com/support")
static let forumsURL = URL(string: "https://ios.forums.wordpress.org")
static let automatticEmails = ["@automattic.com", "@a8c.com"]
}
}