Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 45d79b2

Browse files
authored
Merge pull request #525 from wordpress-mobile/try/no-wp-error
Add new methods to WPAuthenticatorDelegate to allow host apps to handle errors
2 parents a8b598e + a94a620 commit 45d79b2

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

WordPressAuthenticator.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressAuthenticator"
3-
s.version = "1.31.0-beta.1"
3+
s.version = "1.31.0-beta.2"
44
s.summary = "WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps."
55

66
s.description = <<-DESC

WordPressAuthenticator/Authenticator/WordPressAuthenticatorDelegateProtocol.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public protocol WordPressAuthenticatorDelegate: class {
6868
///
6969
func shouldPresentLoginEpilogue(isJetpackLogin: Bool) -> Bool
7070

71+
/// Indicates the Host app wants to handle and display a given error.
72+
///
73+
func shouldHandleError(_ error: Error) -> Bool
74+
75+
/// Signals the Host app that there is an error that needs to be handled.
76+
///
77+
func handleError(_ error: Error, onCompletion: @escaping (UIViewController) -> Void)
78+
7179
/// Indicates if the Signup Epilogue should be displayed.
7280
///
7381
func shouldPresentSignupEpilogue() -> Bool

WordPressAuthenticator/Signin/LoginViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ open class LoginViewController: NUXViewController, LoginFacadeDelegate {
189189
/// Overridden here to direct these errors to the login screen's error label
190190
dynamic open func displayRemoteError(_ error: Error) {
191191
configureViewLoading(false)
192-
193192
let err = error as NSError
194193
guard err.code != 403 else {
195194
let message = LocalizedText.loginError

WordPressAuthenticator/Unified Auth/View Related/Site Address/SiteAddressViewController.swift

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ final class SiteAddressViewController: LoginViewController {
140140
}
141141
}
142142

143+
override func displayRemoteError(_ error: Error) {
144+
guard authenticationDelegate.shouldHandleError(error) else {
145+
super.displayRemoteError(error)
146+
return
147+
}
148+
149+
authenticationDelegate.handleError(error) { customUI in
150+
self.navigationController?.pushViewController(customUI, animated: true)
151+
}
152+
}
153+
143154
/// Reload the tableview and show errors, if any.
144155
///
145156
override func displayError(message: String, moveVoiceOverFocus: Bool = false) {
@@ -351,6 +362,19 @@ private extension SiteAddressViewController {
351362
}
352363
}
353364

365+
/// Push a custom view controller, provided by a host app, to the navigation stack
366+
func pushCustomUI(_ customUI: UIViewController) {
367+
/// Assign the help button of the newly injected UI to the same help button we are currently displaying
368+
/// We are making a somewhat big assumption here: the chrome of the new UI we insert would look like the UI
369+
/// WPAuthenticator is already displaying. Which is risky, but also kind of makes sense, considering
370+
/// we are also pushing that injected UI to the current navigation controller.
371+
if WordPressAuthenticator.shared.delegate?.supportActionEnabled == true {
372+
customUI.navigationItem.rightBarButtonItems = self.navigationItem.rightBarButtonItems
373+
}
374+
375+
self.navigationController?.pushViewController(customUI, animated: true)
376+
}
377+
354378
// MARK: - Private Constants
355379

356380
/// Rows listed in the order they were created.
@@ -419,6 +443,16 @@ private extension SiteAddressViewController {
419443

420444
let err = self.originalErrorOrError(error: error as NSError)
421445

446+
/// Check if the host app wants to provide custom UI to handle the error.
447+
/// If it does, insert the custom UI provided by the host app and exit early
448+
if self.authenticationDelegate.shouldHandleError(err) {
449+
self.authenticationDelegate.handleError(err) { customUI in
450+
self.pushCustomUI(customUI)
451+
}
452+
453+
return
454+
}
455+
422456
if let xmlrpcValidatorError = err as? WordPressOrgXMLRPCValidatorError {
423457
self.displayError(message: xmlrpcValidatorError.localizedDescription, moveVoiceOverFocus: true)
424458

@@ -480,14 +514,7 @@ private extension SiteAddressViewController {
480514

481515
self.showWPUsernamePassword()
482516
case let .injectViewController(customUI):
483-
/// Assign the help button of the newly injected UI to the same help button we are currently displaying
484-
/// We are making a somewhat big assumption here: the chrome of the new UI we insert would look like the UI
485-
/// WPAuthenticator is already displaying. Which is risky, but also kind of makes sense, considering
486-
/// we are also pushing that injected UI to the current navigation controller.
487-
if WordPressAuthenticator.shared.delegate?.supportActionEnabled == true {
488-
customUI.navigationItem.rightBarButtonItems = self.navigationItem.rightBarButtonItems
489-
}
490-
self.navigationController?.pushViewController(customUI, animated: true)
517+
self.pushCustomUI(customUI)
491518
}
492519
})
493520
}

0 commit comments

Comments
 (0)