Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
fix: loader keeps running on the 2U banner (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
mumer92 authored and saeedbashir committed Nov 24, 2021
1 parent 191a884 commit 12f0ba9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Source/AuthenticatedWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ protocol AuthenticatedWebViewControllerDelegate: AnyObject {
func didCompletionCalled(completion: Bool)
}

protocol WebViewNavigationResponseDelegate: AnyObject {
func handleHttpStatusCode(statusCode: OEXHTTPStatusCode) -> Bool
}

private class WKWebViewContentController : WebContentController {
fileprivate let webView: WKWebView

Expand Down Expand Up @@ -120,7 +124,7 @@ public class AuthenticatedWebViewController: UIViewController, WKUIDelegate, WKN
private let headerInsets : HeaderViewInsets
weak var webViewDelegate: WebViewNavigationDelegate?
weak var ajaxCallbackDelegate: AJAXCompletionCallbackDelegate?

weak var webViewNavigationResponseDelegate: WebViewNavigationResponseDelegate?
private lazy var configurations = environment.config.webViewConfiguration()

private var shouldListenForAjaxCallbacks = false
Expand Down Expand Up @@ -249,6 +253,11 @@ public class AuthenticatedWebViewController: UIViewController, WKUIDelegate, WKN
refreshAccessibility()
}

public func showError(with state: LoadState) {
loadController.state = state
refreshAccessibility()
}

// MARK: Header View

var headerView : UIView? {
Expand Down Expand Up @@ -333,6 +342,12 @@ public class AuthenticatedWebViewController: UIViewController, WKUIDelegate, WKN
public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

if let httpResponse = navigationResponse.response as? HTTPURLResponse, let statusCode = OEXHTTPStatusCode(rawValue: httpResponse.statusCode), let errorGroup = statusCode.errorGroup, state == .LoadingContent {

if webViewNavigationResponseDelegate?.handleHttpStatusCode(statusCode: statusCode) ?? false {
decisionHandler(.cancel)
return
}

switch errorGroup {
case .http4xx:
state = .NeedingSession
Expand Down Expand Up @@ -410,11 +425,12 @@ public class AuthenticatedWebViewController: UIViewController, WKUIDelegate, WKN
}

public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
showError(error: error as NSError?)
showError(error: error as NSError?)
}

public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
showError(error: error as NSError?)
guard !loadController.state.isError else { return }
showError(error: error as NSError?)
}

public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
Expand All @@ -430,19 +446,19 @@ public class AuthenticatedWebViewController: UIViewController, WKUIDelegate, WKN
completionHandler(.performDefaultHandling, nil)
}
}

public func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {

let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

alertController.addAction(UIAlertAction(title: Strings.ok, style: .default, handler: { action in
completionHandler(true)
}))

alertController.addAction(UIAlertAction(title: Strings.cancel, style: .cancel, handler: { action in
completionHandler(false)
}))

if let presenter = alertController.popoverPresentationController {
presenter.sourceView = view
presenter.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
Expand Down
33 changes: 33 additions & 0 deletions Source/BannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class BannerViewController: UIViewController, InterfaceOrientationOverriding {

super.init(nibName: nil, bundle: nil)
webController.webViewDelegate = self
webController.webViewNavigationResponseDelegate = self
self.title = title
}

Expand Down Expand Up @@ -84,6 +85,20 @@ class BannerViewController: UIViewController, InterfaceOrientationOverriding {
}
}

private func addCloseButton() {
navigationController?.isNavigationBarHidden = false

let closeButton = UIBarButtonItem(image: Icon.Close.imageWithFontSize(size: 20), style: .plain, target: nil, action: nil)
closeButton.accessibilityLabel = Strings.Accessibility.closeLabel
closeButton.accessibilityHint = Strings.Accessibility.closeHint
closeButton.accessibilityIdentifier = "BannerViewController:close-button"
navigationItem.rightBarButtonItem = closeButton

closeButton.oex_setAction { [weak self] in
self?.dismiss(animated: true)
}
}

override var shouldAutorotate: Bool {
return true
}
Expand Down Expand Up @@ -130,8 +145,26 @@ extension BannerViewController: WebViewNavigationDelegate {
}
}

extension BannerViewController: WebViewNavigationResponseDelegate {
func handleHttpStatusCode(statusCode: OEXHTTPStatusCode) -> Bool {
guard let errorGroup = statusCode.errorGroup else { return false }
switch errorGroup {
case .http4xx, .http5xx:
addCloseButton()

let state = LoadState.failed(error: nil, icon: .InternetError, message: Strings.networkNotAvailableMessageTrouble, attributedMessage: nil, accessibilityMessage: Strings.networkNotAvailableMessageTrouble, buttonInfo: MessageButtonInfo(title: Strings.reload) { [weak self] in
self?.loadRequest()
})

webController.showError(with: state)
return true
}
}
}

extension BannerViewController: AuthenticatedWebViewControllerRequireAuthentication {
func alwaysRequireAuth() -> Bool {
return authRequired
}
}

0 comments on commit 12f0ba9

Please sign in to comment.