Skip to content

Commit

Permalink
First pass at converting error dialog into an error screen for disabl…
Browse files Browse the repository at this point in the history
…ed application password.
  • Loading branch information
hafizrahman committed Oct 31, 2024
1 parent 6dd2616 commit cf69dbb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Yosemite
/// View model for `ApplicationPasswordAuthorizationWebViewController`.
///
final class ApplicationPasswordAuthorizationViewModel {
private let siteURL: String
let siteURL: String
private let stores: StoresManager

init(siteURL: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,20 @@ private extension ApplicationPasswordAuthorizationWebViewController {
guard let url = try await viewModel.fetchAuthURL() else {
DDLogError("⛔️ No authorization URL found for application passwords")
analytics.track(.applicationPasswordAuthorizationURLNotAvailable)
return showErrorAlert(message: Localization.applicationPasswordDisabled)

let errorUI = applicationPasswordDisabledUI(for: viewModel.siteURL)
// When the error view controller is popped, also pop the web view
errorUI.navigationItem.leftBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "chevron.backward"),
style: .plain,
target: self,
action: #selector(popBothControllers)
)

// Push instead of present
navigationController?.pushViewController(errorUI, animated: true)

return
}
loadAuthorizationPage(url: url)
} catch {
Expand All @@ -171,6 +184,12 @@ private extension ApplicationPasswordAuthorizationWebViewController {
}
}

@objc private func popBothControllers() {
// Pop back two view controllers to remove both error and web view
navigationController?.popViewController(animated: false)
navigationController?.popViewController(animated: true)
}

func loadAuthorizationPage(url: URL) {
let parameters: [URLQueryItem] = [
URLQueryItem(name: Constants.Query.appName, value: appName),
Expand Down Expand Up @@ -220,6 +239,14 @@ private extension ApplicationPasswordAuthorizationWebViewController {
}
present(alertController, animated: true)
}

/// The error screen to be displayed when the user tries to log in with site credentials
/// with application password disabled.
///
func applicationPasswordDisabledUI(for siteURL: String) -> UIViewController {
let viewModel = ApplicationPasswordDisabledViewModel(siteURL: siteURL)
return ULErrorViewController(viewModel: viewModel)
}
}

extension ApplicationPasswordAuthorizationWebViewController: WKNavigationDelegate {
Expand Down Expand Up @@ -254,6 +281,13 @@ extension ApplicationPasswordAuthorizationWebViewController: WKNavigationDelegat
}
}

extension ApplicationPasswordAuthorizationWebViewController: UIAdaptivePresentationControllerDelegate {
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
// This will be called when the error UI is dismissed
navigationController?.dismiss(animated: true)
}
}

private extension ApplicationPasswordAuthorizationWebViewController {
enum Constants {
static let successURL = "woocommerce://application-password"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import WordPressAuthenticator
/// modeling an error when application password is disabled.
///
struct ApplicationPasswordDisabledViewModel: ULErrorViewModel {
init(siteURL: String) {
init(siteURL: String,
authentication: Authentication = ServiceLocator.authenticationManager) {
self.siteURL = siteURL
self.authentication = authentication
}

let siteURL: String
let authentication: Authentication
let image: UIImage = .errorImage // TODO: update this if needed

var text: NSAttributedString {
Expand Down Expand Up @@ -55,6 +58,17 @@ struct ApplicationPasswordDisabledViewModel: ULErrorViewModel {
}
WebviewHelper.launch(Constants.applicationPasswordLink, with: viewController)
}

var rightBarButtonItemTitle: String? {
return Localization.helpButtonTitle
}

func didTapRightBarButtonItem(in viewController: UIViewController?) {
guard let viewController else {
return
}
authentication.presentSupport(from: viewController, screen: .noWooError)
}
}

private extension ApplicationPasswordDisabledViewModel {
Expand All @@ -78,6 +92,10 @@ private extension ApplicationPasswordDisabledViewModel {
"Log in with WordPress.com",
comment: "Button that will navigate to the authentication flow with WP.com"
)
static let helpButtonTitle = NSLocalizedString(
"Help",
comment: "Button that will navigate to the support area"
)
}
enum Constants {
static let applicationPasswordLink = "https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/"
Expand Down

0 comments on commit cf69dbb

Please sign in to comment.