Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script and webview updates #20

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/UI/DeskproButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ public class DeskproButton: UIButton {
}
}

/// Main app colors.
extension UIColor {
static let deskproLightBlue = UIColor(red: 58/255.0, green: 141/255.0, blue: 222/255.0, alpha: 1.0)
static let deskproDarkBlue = UIColor(red: 5/255.0, green: 100/255.0, blue: 192/255.0, alpha: 1.0)
}
63 changes: 23 additions & 40 deletions Sources/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ protocol WebViewDelegate: AnyObject {
func eventOccured(_ event: DeskproEvent)
}

/// Main app colors.
extension UIColor {
static let deskproLightBlue = UIColor(red: 58/255.0, green: 141/255.0, blue: 222/255.0, alpha: 1.0)
static let deskproDarkBlue = UIColor(red: 5/255.0, green: 100/255.0, blue: 192/255.0, alpha: 1.0)
}

/// The ViewController hosting WebView for DeskPro Messenger functionality. It manages the WebView, handles WebView configuration, and sets up communication with JavaScript. It also handles WebView lifecycle events and error handling.
final class CustomWebView: UIViewController {

Expand Down Expand Up @@ -47,14 +41,6 @@ final class CustomWebView: UIViewController {
super.viewWillDisappear(true)
delegate?.webViewDismissed()
}

/// Triggered when device changes the interface style to light or dark.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 12.0, *) {
webView.scrollView.backgroundColor = traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}
}

/// Basic WebView setup.
private final func setupWebView() {
Expand All @@ -78,9 +64,6 @@ final class CustomWebView: UIViewController {
} else {
activityIndicator.style = .whiteLarge
}
if #available(iOS 12.0, *) {
activityIndicator.color = traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}

view.addSubview(activityIndicator)
}
Expand All @@ -99,12 +82,17 @@ final class CustomWebView: UIViewController {
final func loadUrlInWebView() {
if let url = url {
let request = URLRequest(url: url)
clearCookies()
webView.load(request)
} else {
dprint("Url not provided")
}
}

private final func clearCookies() {
HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)
}

private final func showActivityIndicator(show: Bool) {
if show {
activityIndicator.startAnimating()
Expand Down Expand Up @@ -133,17 +121,15 @@ final class CustomWebView: UIViewController {

extension CustomWebView: WKNavigationDelegate {

/// We are allowing users to swipe back to navigate, but only when they leave the chat, e.g. when they tap the "Powered by Deskpro" label which leads to the Deskpro web.
final func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let host = navigationAction.request.url?.host {
if host.contains("deskprodemo") {
webView.allowsBackForwardNavigationGestures = false
} else {
webView.allowsBackForwardNavigationGestures = true
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard case .linkActivated = navigationAction.navigationType,
let url = navigationAction.request.url
else {
decisionHandler(.allow)
return
}

decisionHandler(.allow)
decisionHandler(.cancel)
UIApplication.shared.open(url)
}

final func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Expand All @@ -152,9 +138,6 @@ extension CustomWebView: WKNavigationDelegate {

DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.showActivityIndicator(show: false)
if #available(iOS 12.0, *) {
webView.scrollView.backgroundColor = self?.traitCollection.userInterfaceStyle == .dark ? .deskproDarkBlue : .deskproLightBlue
}
}
}

Expand Down Expand Up @@ -307,7 +290,7 @@ private enum PostMessageFunctions: String {
private enum InjectionScripts {

static let initAndOpenScript = """
window.DESKPRO_MESSENGER_OPTIONS = {
window.DpMessengerOptions = {
showLauncherButton: false,
openOnInit: true,
userInfo: window.webkit.messageHandlers.iosListener.postMessage("\(PostMessageFunctions.getUserInfo)"),
Expand All @@ -317,16 +300,16 @@ private enum InjectionScripts {
urlCacheableConfig: undefined,
};

window.DESKPRO_MESSENGER_CONNECTION = {
window.DpMessengerConnection = {
parentMethods: {
ready: async (messengerId) => {
const data = await window.DESKPRO_MESSENGER_CONNECTION.childMethods?.init(messengerId, {
showLauncherButton: DESKPRO_MESSENGER_OPTIONS.showLauncherButton,
userInfo: window.DESKPRO_MESSENGER_OPTIONS?.userInfo,
launcherButtonConfig: DESKPRO_MESSENGER_OPTIONS.launcherButtonConfig,
messengerAppConfig: DESKPRO_MESSENGER_OPTIONS.messengerAppConfig,
const data = await window.DpMessengerConnection.childMethods?.init(messengerId, {
showLauncherButton: DpMessengerOptions.showLauncherButton,
userInfo: window.DpMessengerOptions?.userInfo,
launcherButtonConfig: DpMessengerOptions.launcherButtonConfig,
messengerAppConfig: DpMessengerOptions.messengerAppConfig,
parentViewDimensions: "fullscreen",
open: DESKPRO_MESSENGER_OPTIONS.openOnInit,
open: DpMessengerOptions.openOnInit,
});

if (data) {
Expand All @@ -335,7 +318,7 @@ private enum InjectionScripts {

const deviceToken = await window.webkit.messageHandlers.iosListener.postMessage("\(PostMessageFunctions.getDeviceToken)");

window.DESKPRO_MESSENGER_CONNECTION.childMethods?.setDeviceToken(messengerId, {
window.DpMessengerConnection.childMethods?.setDeviceToken(messengerId, {
token: deviceToken
});
},
Expand Down Expand Up @@ -365,6 +348,6 @@ private enum InjectionScripts {
"""

static let logoutScript = """
window.DESKPRO_MESSENGER_CONNECTION.childMethods.logout("1");
window.DpMessengerConnection.childMethods.logout("1");
"""
}
Loading