-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AccountSettingsViewController.swift
427 lines (359 loc) · 20 KB
/
AccountSettingsViewController.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import Foundation
import UIKit
import WordPressShared
import WordPressFlux
func AccountSettingsViewController(account: WPAccount) -> ImmuTableViewController? {
guard let api = account.wordPressComRestApi else {
return nil
}
let service = AccountSettingsService(userID: account.userID.intValue, api: api)
return AccountSettingsViewController(accountSettingsService: service)
}
func AccountSettingsViewController(accountSettingsService: AccountSettingsService) -> ImmuTableViewController {
let controller = AccountSettingsController(accountSettingsService: accountSettingsService)
let viewController = ImmuTableViewController(controller: controller, style: .insetGrouped)
viewController.handler.automaticallyDeselectCells = true
return viewController
}
private class AccountSettingsController: SettingsController {
var trackingKey: String {
return "account_settings"
}
let title = NSLocalizedString("Account Settings", comment: "Account Settings Title")
var immuTableRows: [ImmuTableRow.Type] {
return [
TextRow.self,
EditableTextRow.self,
DestructiveButtonRow.self
]
}
// MARK: - Initialization
private let accountSettingsService: AccountSettingsService
private let accountService: AccountService
var settings: AccountSettings? {
didSet {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: ImmuTableViewController.modelChangedNotification), object: nil)
}
}
var noticeMessage: String? {
didSet {
NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: ImmuTableViewController.modelChangedNotification), object: nil)
}
}
private let alertHelper = DestructiveAlertHelper()
init(accountSettingsService: AccountSettingsService,
accountService: AccountService = AccountService(coreDataStack: ContextManager.sharedInstance())) {
self.accountSettingsService = accountSettingsService
self.accountService = accountService
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(AccountSettingsController.loadStatus), name: NSNotification.Name.AccountSettingsServiceRefreshStatusChanged, object: nil)
notificationCenter.addObserver(self, selector: #selector(AccountSettingsController.loadSettings), name: NSNotification.Name.AccountSettingsChanged, object: nil)
notificationCenter.addObserver(self, selector: #selector(AccountSettingsController.showSettingsChangeErrorMessage), name: NSNotification.Name.AccountSettingsServiceChangeSaveFailed, object: nil)
}
func refreshModel() {
accountSettingsService.refreshSettings()
}
@objc func loadStatus() {
noticeMessage = accountSettingsService.status.errorMessage ?? noticeForAccountSettings(accountSettingsService.settings)
}
@objc func loadSettings() {
settings = accountSettingsService.settings
// Status is affected by settings changes (for pending email), so let's load that as well
loadStatus()
}
// MARK: - ImmuTableViewController
func tableViewModelWithPresenter(_ presenter: ImmuTablePresenter) -> ImmuTable {
return mapViewModel(settings, service: accountSettingsService, presenter: presenter)
}
// MARK: - Model mapping
func mapViewModel(_ settings: AccountSettings?, service: AccountSettingsService, presenter: ImmuTablePresenter) -> ImmuTable {
let username = TextRow(
title: NSLocalizedString("Username", comment: "Account Settings Username label"),
value: settings?.username ?? ""
)
let editableUsername = EditableTextRow(
title: NSLocalizedString("Username", comment: "Account Settings Username label"),
value: settings?.username ?? "",
action: presenter.push(changeUsername(with: settings, service: service)),
fieldName: "username"
)
let email = EditableTextRow(
title: NSLocalizedString("Email", comment: "Account Settings Email label"),
value: settings?.emailForDisplay ?? "",
accessoryImage: emailAccessoryImage(),
action: presenter.push(editEmailAddress(settings, service: service)),
fieldName: "email"
)
var primarySiteName = settings.flatMap { service.primarySiteNameForSettings($0) } ?? ""
// If the primary site has no Site Title, then show the displayURL.
if primarySiteName.isEmpty {
let account = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.sharedInstance().mainContext)
primarySiteName = account?.defaultBlog?.displayURL as String? ?? ""
}
let primarySite = EditableTextRow(
title: NSLocalizedString("Primary Site", comment: "Primary Web Site"),
value: primarySiteName,
action: presenter.present(insideNavigationController(editPrimarySite(settings, service: service))),
fieldName: "primary_site"
)
let webAddress = EditableTextRow(
title: NSLocalizedString("Web Address", comment: "Account Settings Web Address label"),
value: settings?.webAddress ?? "",
action: presenter.push(editWebAddress(service)),
fieldName: "web_address"
)
let password = EditableTextRow(
title: Constants.title,
value: "",
action: presenter.push(changePassword(with: settings, service: service)),
fieldName: "password"
)
let closeAccount = DestructiveButtonRow(
title: NSLocalizedString("Close Account", comment: "Close account action label"),
action: closeAccountAction,
accessibilityIdentifier: "closeAccountButtonRow")
return ImmuTable(sections: [
ImmuTableSection(
rows: [
(settings?.usernameCanBeChanged ?? false) ? editableUsername : username,
email,
password,
primarySite,
webAddress
]),
ImmuTableSection(
rows: [
closeAccount
])
])
}
// MARK: - Actions
func editEmailAddress(_ settings: AccountSettings?, service: AccountSettingsService) -> (ImmuTableRow) -> SettingsTextViewController {
return { row in
let editableRow = row as! EditableTextRow
let hint = NSLocalizedString("Will not be publicly displayed.", comment: "Help text when editing email address")
let settingsViewController = self.controllerForEditableText(editableRow,
changeType: AccountSettingsChange.email,
hint: hint,
service: service)
settingsViewController.mode = .email
settingsViewController.notice = self.noticeForAccountSettings(settings)
settingsViewController.displaysActionButton = settings?.emailPendingChange ?? false
settingsViewController.actionText = NSLocalizedString("Revert Pending Change", comment: "Cancels a pending Email Change")
settingsViewController.onActionPress = {
service.saveChange(.emailRevertPendingChange)
}
return settingsViewController
}
}
func changePassword(with settings: AccountSettings?, service: AccountSettingsService) -> (ImmuTableRow) -> SettingsTextViewController {
return { row in
return ChangePasswordViewController(username: settings?.username ?? "") { [weak self] value in
DispatchQueue.main.async {
SVProgressHUD.show(withStatus: Constants.changingPassword)
service.updatePassword(value, finished: { (success, error) in
if success {
self?.refreshAccountDetails {
SVProgressHUD.showSuccess(withStatus: Constants.changedPasswordSuccess)
}
} else {
let errorMessage = error?.localizedDescription ?? Constants.changePasswordGenericError
SVProgressHUD.showError(withStatus: errorMessage)
}
})
}
}
}
}
func changeUsername(with settings: AccountSettings?, service: AccountSettingsService) -> (ImmuTableRow) -> ChangeUsernameViewController {
return { _ in
return ChangeUsernameViewController(service: service, settings: settings) { [weak self] username in
self?.refreshModel()
if let username = username {
let notice = Notice(title: String(format: Constants.usernameChanged, username))
ActionDispatcher.dispatch(NoticeAction.post(notice))
}
}
}
}
func refreshAccountDetails(finished: @escaping () -> Void) {
guard let account = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.shared.mainContext) else {
return
}
accountService.updateUserDetails(for: account, success: { () in
finished()
}, failure: { _ in
finished()
})
}
func editWebAddress(_ service: AccountSettingsService) -> (ImmuTableRow) -> SettingsTextViewController {
let hint = NSLocalizedString("Shown publicly when you comment on blogs.", comment: "Help text when editing web address")
return editText(AccountSettingsChange.webAddress, hint: hint, service: service)
}
func editPrimarySite(_ settings: AccountSettings?, service: AccountSettingsService) -> ImmuTableRowControllerGenerator {
return {
row in
let selectorViewController = BlogSelectorViewController(selectedBlogDotComID: settings?.primarySiteID as NSNumber?,
successHandler: { (dotComID: NSNumber?) in
if let dotComID = dotComID?.intValue {
WPAnalytics.trackSettingsChange(self.trackingKey, fieldName: "primary_site")
let change = AccountSettingsChange.primarySite(dotComID)
service.saveChange(change)
}
},
dismissHandler: nil)
selectorViewController.title = NSLocalizedString("Primary Site", comment: "Primary Site Picker's Title")
selectorViewController.displaysOnlyDefaultAccountSites = true
selectorViewController.displaysCancelButton = true
selectorViewController.dismissOnCompletion = true
selectorViewController.dismissOnCancellation = true
return selectorViewController
}
}
private var closeAccountAction: (ImmuTableRow) -> Void {
return { [weak self] _ in
guard let self = self else { return }
WPAnalytics.track(.accountCloseTapped, properties: ["has_atomic": self.hasAtomicSite])
switch self.hasAtomicSite {
case true:
self.showCloseAccountErrorAlert(message: self.localizedErrorMessageForAtomicSites)
case false:
self.showCloseAccountAlert()
}
}
}
private var hasAtomicSite: Bool {
let account = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.shared.mainContext)
return account?.hasAtomicSite() ?? false
}
private func showCloseAccountAlert() {
guard let value = settings?.username else {
return
}
let title = NSLocalizedString("Confirm Close Account", comment: "Close Account alert title")
let message = NSLocalizedString("\nTo confirm, please re-enter your username before closing.\n\n",
comment: "Message of Close Account confirmation alert")
let destructiveActionTitle = NSLocalizedString("Permanently Close Account",
comment: "Close Account confirmation action title")
let alert = alertHelper.makeAlertWithConfirmation(title: title, message: message, valueToConfirm: value, destructiveActionTitle: destructiveActionTitle, destructiveAction: closeAccount)
alert.presentFromRootViewController()
}
private func closeAccount() {
let status = NSLocalizedString("Closing account…", comment: "Overlay message displayed while closing account")
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show(withStatus: status)
accountSettingsService.closeAccount { [weak self] in
guard let self = self else { return }
switch $0 {
case .success:
WPAnalytics.track(.accountCloseCompleted, properties: ["status": "success"])
let status = NSLocalizedString("Account closed", comment: "Overlay message displayed when account successfully closed")
SVProgressHUD.showDismissibleSuccess(withStatus: status)
AccountHelper.logOutDefaultWordPressComAccount()
case .failure(let error):
let errorCode = self.errorCode(error) ?? "unknown"
WPAnalytics.track(.accountCloseCompleted, properties: ["status": "failure", "error_code": errorCode])
SVProgressHUD.dismiss()
DDLogError("Error closing account: \(error.localizedDescription)")
self.showCloseAccountErrorAlert(message: self.generateLocalizedMessage(error))
}
}
}
private func showCloseAccountErrorAlert(message: String) {
let title = NSLocalizedString("Couldn’t close account automatically",
comment: "Error title displayed when unable to close user account.")
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let contactSupportTitle = NSLocalizedString("Contact Support",
comment: "Title for a button displayed when unable to close user account due to having atomic site.")
alert.addActionWithTitle(contactSupportTitle, style: .default, handler: contactSupportAction)
let cancelAction = NSLocalizedString("Cancel", comment: "Alert dismissal title")
alert.addCancelActionWithTitle(cancelAction)
alert.presentFromRootViewController()
}
private func errorCode(_ error: Error) -> String? {
let userInfo = (error as NSError).userInfo
let errorCode = userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String
return errorCode
}
private func generateLocalizedMessage(_ error: Error) -> String {
let errorCode = errorCode(error)
switch errorCode {
case "unauthorized":
return NSLocalizedString("You're not authorized to close the account.",
comment: "Error message displayed when unable to close user account due to being unauthorized.")
case "atomic-site":
return localizedErrorMessageForAtomicSites
case "chargebacked-site":
return NSLocalizedString("This user account cannot be closed if there are unresolved chargebacks.",
comment: "Error message displayed when unable to close user account due to unresolved chargebacks.")
case "active-subscriptions":
return NSLocalizedString("This user account cannot be closed while it has active subscriptions.",
comment: "Error message displayed when unable to close user account due to having active subscriptions.")
case "active-memberships":
return NSLocalizedString("This user account cannot be closed while it has active purchases.",
comment: "Error message displayed when unable to close user account due to having active purchases.")
default:
return NSLocalizedString("An error occured while closing account.",
comment: "Default error message displayed when unable to close user account.")
}
}
private var localizedErrorMessageForAtomicSites: String {
// Based on https://github.com/Automattic/wp-calypso/pull/65780
NSLocalizedString(
"accountSettings.closeAccount.error.atomicSite",
value: "This user account cannot be closed immediately because it has active purchases. Please contact our support team to finish deleting the account.",
comment: "Error message displayed when unable to close user account due to having active atomic site."
)
}
private var contactSupportAction: ((UIAlertAction) -> Void) {
return { action in
if ZendeskUtils.zendeskEnabled {
guard let leafViewController = UIApplication.shared.leafViewController else {
return
}
ZendeskUtils.sharedInstance.showNewRequestIfPossible(from: leafViewController, with: .closeAccount) { [weak self] identityUpdated in
if identityUpdated {
self?.refreshModel()
}
}
} else {
guard let url = Constants.forumsURL else {
return
}
UIApplication.shared.open(url)
}
}
}
@objc fileprivate func showSettingsChangeErrorMessage(notification: NSNotification) {
guard let error = notification.userInfo?[NSUnderlyingErrorKey] as? NSError,
let errorMessage = error.userInfo[WordPressComRestApi.ErrorKeyErrorMessage] as? String else {
return
}
SVProgressHUD.showError(withStatus: errorMessage)
}
// MARK: - Private Helpers
fileprivate func noticeForAccountSettings(_ settings: AccountSettings?) -> String? {
guard settings?.emailPendingChange == true,
let pendingAddress = settings?.emailPendingAddress else {
return nil
}
let localizedNotice = NSLocalizedString("There is a pending change of your email to %@. Please check your inbox for a confirmation link.",
comment: "Displayed when there's a pending Email Change. The variable is the new email address.")
return String(format: localizedNotice, pendingAddress)
}
fileprivate func emailAccessoryImage() -> UIImage? {
guard settings?.emailPendingChange == true else {
return nil
}
return UIImage.gridicon(.noticeOutline).imageWithTintColor(.error)
}
// MARK: - Constants
enum Constants {
static let title = NSLocalizedString("Change Password", comment: "Account Settings Change password label")
static let changingPassword = NSLocalizedString("Changing password", comment: "Loader title displayed by the loading view while the password is changing")
static let changedPasswordSuccess = NSLocalizedString("Password changed successfully", comment: "Loader title displayed by the loading view while the password is changed successfully")
static let changePasswordGenericError = NSLocalizedString("There was an error changing the password", comment: "Text displayed when there is a failure loading the history.")
static let usernameChanged = NSLocalizedString("Username changed to %@", comment: "Message displayed in a Notice when the username has changed successfully. The placeholder is the new username.")
static let forumsURL = URL(string: "https://ios.forums.wordpress.org")
}
}