Skip to content

Commit

Permalink
removed dependency with sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Espinola committed Jul 4, 2024
1 parent e905125 commit 5546913
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 45 deletions.
83 changes: 52 additions & 31 deletions Sources/ATTNWebViewHandling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ final class ATTNWebViewHandler: NSObject, ATTNWebViewHandling {
}
}

private weak var sdk: ATTNSDK?
private weak var webViewProvider: ATTNWebViewProviding?
private var urlBuilder: ATTNCreativeUrlProviding

init(sdk: ATTNSDK, creativeUrlBuilder: ATTNCreativeUrlProviding = ATTNCreativeUrlProvider()) {
self.sdk = sdk
init(webViewProvider: ATTNWebViewProviding, creativeUrlBuilder: ATTNCreativeUrlProviding = ATTNCreativeUrlProvider()) {
self.webViewProvider = webViewProvider
self.urlBuilder = creativeUrlBuilder
}

Expand All @@ -50,22 +50,18 @@ final class ATTNWebViewHandler: NSObject, ATTNWebViewHandling {
creativeId: String? = nil,
handler: ATTNCreativeTriggerCompletionHandler? = nil
) {
guard let sdk = sdk else {
guard let webViewProvider = webViewProvider else {
Loggers.creative.debug("Not showing the Attentive creative because the iOS version is too old.")
sdk?.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
webViewProvider?.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
return
}

sdk.parentView = view
sdk.triggerHandler = handler
webViewProvider.parentView = view
webViewProvider.triggerHandler = handler

let domain = sdk.getDomain()
let mode = sdk.getMode()
let userIdentity = sdk.userIdentity
Loggers.creative.debug("Called showWebView in creativeSDK with domain: \(self.domain, privacy: .public)")

Loggers.creative.debug("Called showWebView in creativeSDK with domain: \(domain, privacy: .public)")

guard !ATTNSDK.isCreativeOpen else {
guard !isCreativeOpen else {
Loggers.creative.debug("Attempted to trigger creative, but creative is currently open. Taking no action")
return
}
Expand All @@ -76,7 +72,7 @@ final class ATTNWebViewHandler: NSObject, ATTNWebViewHandling {
configuration: ATTNCreativeUrlConfig(
domain: domain,
creativeId: creativeId,
skipFatigue: sdk.skipFatigueOnCreative,
skipFatigue: webViewProvider.skipFatigueOnCreative,
mode: mode.rawValue,
userIdentity: userIdentity
)
Expand All @@ -98,25 +94,27 @@ final class ATTNWebViewHandler: NSObject, ATTNWebViewHandling {
let userScript = WKUserScript(source: userScriptWithEventListener, injectionTime: .atDocumentStart, forMainFrameOnly: false)
configuration.userContentController.addUserScript(userScript)

sdk.webView = WKWebView(frame: view.frame, configuration: configuration)
webViewProvider.webView = WKWebView(frame: view.frame, configuration: configuration)

guard let webView = sdk.webView else { return }
guard let webView = webViewProvider.webView else { return }

webView.navigationDelegate = self
webView.load(request)

if mode == .debug {
sdk.parentView?.addSubview(webView)
webViewProvider.parentView?.addSubview(webView)
} else {
webView.isOpaque = false
webView.backgroundColor = .clear
}
}

func closeCreative() {
sdk?.removeWebView()
ATTNSDK.isCreativeOpen = false
sdk?.triggerHandler?(ATTNCreativeTriggerStatus.closed)
webViewProvider?.webView?.removeFromSuperview()
webViewProvider?.webView = nil

isCreativeOpen = false
webViewProvider?.triggerHandler?(ATTNCreativeTriggerStatus.closed)
Loggers.creative.debug("Successfully closed creative")
}
}
Expand Down Expand Up @@ -151,26 +149,26 @@ extension ATTNWebViewHandler: WKNavigationDelegate {
in: nil,
in: .defaultClient
) { [weak self] result in
guard let self = self, let sdk = self.sdk else { return }
guard let self = self, let webViewProvider = self.webViewProvider else { return }
guard case let .success(statusAny) = result else {
Loggers.creative.debug("No status returned from JS. Not showing WebView.")
self.sdk?.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
webViewProvider.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
return
}

switch ScriptStatus.getRawValue(from: statusAny) {
case .success:
Loggers.creative.debug("Found creative iframe, showing WebView.")
if sdk.getMode() == .production {
sdk.parentView?.addSubview(webView)
if self.mode == .production {
webViewProvider.parentView?.addSubview(webView)
}
sdk.triggerHandler?(ATTNCreativeTriggerStatus.opened)
webViewProvider.triggerHandler?(ATTNCreativeTriggerStatus.opened)
case .timeout:
Loggers.creative.error("Creative timed out. Not showing WebView.")
sdk.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
webViewProvider.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
case .unknown(let statusString):
Loggers.creative.error("Received unknown status: \(statusString). Not showing WebView")
sdk.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
webViewProvider.triggerHandler?(ATTNCreativeTriggerStatus.notOpened)
default: break
}
}
Expand Down Expand Up @@ -200,17 +198,40 @@ extension ATTNWebViewHandler: WKNavigationDelegate {

extension ATTNWebViewHandler: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
let messageBody = message.body as? String ?? ""
Loggers.creative.debug("Web event message: \(messageBody). isCreativeOpen: \(ATTNSDK.isCreativeOpen ? "YES" : "NO")")
let messageBody = message.body as? String ?? "'Empty'"
Loggers.creative.debug("Web event message: \(messageBody). isCreativeOpen: \(self.isCreativeOpen ? "YES" : "NO")")

if messageBody == "CLOSE" {
closeCreative()
} else if messageBody == "IMPRESSION" {
Loggers.creative.debug("Creative opened and generated impression event")
ATTNSDK.isCreativeOpen = true
} else if messageBody == String(format: "%@ true", Constants.visibilityEvent), ATTNSDK.isCreativeOpen {
isCreativeOpen = true
} else if messageBody == String(format: "%@ true", Constants.visibilityEvent), isCreativeOpen {
Loggers.creative.debug("Nav away from creative, closing")
closeCreative()
}
}
}

fileprivate extension ATTNWebViewHandler {
var domain: String {
webViewProvider?.getDomain() ?? ""
}

var mode: ATTNSDKMode {
webViewProvider?.getMode() ?? .production
}

var userIdentity: ATTNUserIdentity {
webViewProvider?.getUserIdentity() ?? .init()
}

var skipFatigueOnCreative: Bool {
webViewProvider?.skipFatigueOnCreative ?? false
}

var isCreativeOpen: Bool {
get { webViewProvider?.isCreativeOpen ?? false }
set { webViewProvider?.isCreativeOpen = newValue }
}
}
20 changes: 20 additions & 0 deletions Sources/ATTNWebViewProviding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ATTNWebViewProviding.swift
// attentive-ios-sdk-framework
//
// Created by Vladimir - Work on 2024-07-04.
//

import WebKit

protocol ATTNWebViewProviding: NSObjectProtocol {
var parentView: UIView? { get set }
var webView: WKWebView? { get set }
var skipFatigueOnCreative: Bool { get set }
var triggerHandler: ATTNCreativeTriggerCompletionHandler? { get set }
var isCreativeOpen: Bool { get set }

func getDomain() -> String
func getMode() -> ATTNSDKMode
func getUserIdentity() -> ATTNUserIdentity
}
27 changes: 13 additions & 14 deletions Sources/Public/SDK/ATTNSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public typealias ATTNCreativeTriggerCompletionHandler = (String) -> Void

@objc(ATTNSDK)
public final class ATTNSDK: NSObject {
// MARK: Static Properties
static var isCreativeOpen = false
var isCreativeOpen = false

// MARK: Instance Properties
var parentView: UIView?
Expand Down Expand Up @@ -41,7 +40,7 @@ public final class ATTNSDK: NSObject {

super.init()

self.webViewHandler = ATTNWebViewHandler(sdk: self)
self.webViewHandler = ATTNWebViewHandler(webViewProvider: self)
self.sendInfoEvent()
self.initializeSkipFatigueOnCreatives()
}
Expand Down Expand Up @@ -102,6 +101,15 @@ public final class ATTNSDK: NSObject {
}
}

// MARK: ATTNWebViewProviding
extension ATTNSDK: ATTNWebViewProviding {
func getDomain() -> String { domain }

func getMode() -> ATTNSDKMode { mode }

func getUserIdentity() -> ATTNUserIdentity { userIdentity }
}

// MARK: Private Helpers
fileprivate extension ATTNSDK {
func sendInfoEvent() {
Expand All @@ -121,22 +129,13 @@ fileprivate extension ATTNSDK {
extension ATTNSDK {
convenience init(domain: String, mode: ATTNSDKMode, urlBuilder: ATTNCreativeUrlProviding) {
self.init(domain: domain, mode: mode)
self.webViewHandler = ATTNWebViewHandler(sdk: self, creativeUrlBuilder: urlBuilder)
self.webViewHandler = ATTNWebViewHandler(webViewProvider: self, creativeUrlBuilder: urlBuilder)
}

convenience init(api: ATTNAPIProtocol, urlBuilder: ATTNCreativeUrlProviding? = nil) {
self.init(domain: api.domain)
self.api = api
guard let urlBuilder = urlBuilder else { return }
self.webViewHandler = ATTNWebViewHandler(sdk: self, creativeUrlBuilder: urlBuilder)
}

func getDomain() -> String { domain }

func getMode() -> ATTNSDKMode { mode }

func removeWebView() {
webView?.removeFromSuperview()
webView = nil
self.webViewHandler = ATTNWebViewHandler(webViewProvider: self, creativeUrlBuilder: urlBuilder)
}
}
4 changes: 4 additions & 0 deletions attentive-ios-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
FB35C1992C0E5365009FA048 /* ATTNInfoEvent+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB35C1982C0E5365009FA048 /* ATTNInfoEvent+Extension.swift */; };
FB35C19B2C0E53F9009FA048 /* ATTNCustomEvent+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB35C19A2C0E53F9009FA048 /* ATTNCustomEvent+Extension.swift */; };
FB4E3FE52C36F7BF004B8FF0 /* ATTNWebViewHandling.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB4E3FE42C36F7BF004B8FF0 /* ATTNWebViewHandling.swift */; };
FB4E3FE72C372C54004B8FF0 /* ATTNWebViewProviding.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB4E3FE62C372C54004B8FF0 /* ATTNWebViewProviding.swift */; };
FB56D4DA2C208BAD00AF7530 /* ATTNSDK+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB56D4D92C208BAD00AF7530 /* ATTNSDK+Extension.swift */; };
FB56D4DC2C208D6100AF7530 /* Boolean+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB56D4DB2C208D6100AF7530 /* Boolean+Extension.swift */; };
FB56D4DE2C208DC100AF7530 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB56D4DD2C208DC100AF7530 /* String+Extension.swift */; };
Expand Down Expand Up @@ -146,6 +147,7 @@
FB35C1982C0E5365009FA048 /* ATTNInfoEvent+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ATTNInfoEvent+Extension.swift"; sourceTree = "<group>"; };
FB35C19A2C0E53F9009FA048 /* ATTNCustomEvent+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ATTNCustomEvent+Extension.swift"; sourceTree = "<group>"; };
FB4E3FE42C36F7BF004B8FF0 /* ATTNWebViewHandling.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNWebViewHandling.swift; sourceTree = "<group>"; };
FB4E3FE62C372C54004B8FF0 /* ATTNWebViewProviding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ATTNWebViewProviding.swift; sourceTree = "<group>"; };
FB56D4D92C208BAD00AF7530 /* ATTNSDK+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ATTNSDK+Extension.swift"; sourceTree = "<group>"; };
FB56D4DB2C208D6100AF7530 /* Boolean+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Boolean+Extension.swift"; sourceTree = "<group>"; };
FB56D4DD2C208DC100AF7530 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -474,6 +476,7 @@
FBA9F9EF2C0A77AB00C65024 /* ATTNVisitorService.swift */,
FB35C17C2C0E039E009FA048 /* ATTNConstants.swift */,
FB4E3FE42C36F7BF004B8FF0 /* ATTNWebViewHandling.swift */,
FB4E3FE62C372C54004B8FF0 /* ATTNWebViewProviding.swift */,
FBA9F9FF2C0A77AB00C65024 /* Public */,
FBA9FA022C0A77AB00C65024 /* Resources */,
);
Expand Down Expand Up @@ -674,6 +677,7 @@
FBA9FA112C0A77AB00C65024 /* ATTNCart.swift in Sources */,
FBA9FA102C0A77AB00C65024 /* ATTNAddToCartEvent.swift in Sources */,
FB35C17B2C0E0353009FA048 /* ATTNIdentifierType.swift in Sources */,
FB4E3FE72C372C54004B8FF0 /* ATTNWebViewProviding.swift in Sources */,
FBA9FA0A2C0A77AB00C65024 /* ATTNCreativeUrlProvider.swift in Sources */,
FB65536C2C1B74A9008DB3B1 /* ATTNAPIProtocol.swift in Sources */,
FB35C17D2C0E039E009FA048 /* ATTNConstants.swift in Sources */,
Expand Down

0 comments on commit 5546913

Please sign in to comment.