diff --git a/Nativebrik.podspec b/Nativebrik.podspec index 4b614cb..b46aafe 100644 --- a/Nativebrik.podspec +++ b/Nativebrik.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Nativebrik' - s.version = '0.5.1' + s.version = '0.5.2' s.summary = 'Nativebrik SDK' s.description = <<-DESC Nativebrik SDK for iOS. diff --git a/ios/Nativebrik/Nativebrik/component/action-listener.swift b/ios/Nativebrik/Nativebrik/component/action-listener.swift index 437523d..52999e6 100644 --- a/ios/Nativebrik/Nativebrik/component/action-listener.swift +++ b/ios/Nativebrik/Nativebrik/component/action-listener.swift @@ -52,25 +52,7 @@ func configureOnClickGesture(target: UIView, action: Selector, context: UIBlockC let gesture = ClickListener(target: target, action: action) gesture.onClick = { if let event = event { - let compiledPayload = event.payload?.map { property -> Property in - let value = compile(property.value ?? "", context.getVariable()) - return Property( - name: property.name, - value: value, - ptype: property.ptype - ) - } - - let compiledEvent = UIBlockEventDispatcher( - name: event.name, - destinationPageId: event.destinationPageId, - deepLink: event.deepLink, - payload: compiledPayload, - httpRequest: event.httpRequest, - httpResponseAssertion: event.httpResponseAssertion - ) - - context.dipatch(event: compiledEvent) + context.dipatch(event: event) } } if event != nil { diff --git a/ios/Nativebrik/Nativebrik/component/page.swift b/ios/Nativebrik/Nativebrik/component/page.swift index 1dfe676..468482b 100644 --- a/ios/Nativebrik/Nativebrik/component/page.swift +++ b/ios/Nativebrik/Nativebrik/component/page.swift @@ -141,28 +141,32 @@ class PageView: UIView { base: self?.data, self?.container.createVariableForTemplate(data: nil, properties: self?.props) ) + + // compile event + let deepLink = dispatchedEvent.deepLink + let name = dispatchedEvent.name + let compiledEvent = UIBlockEventDispatcher( + name: (name != nil) ? compile(name ?? "", variable) : nil, + destinationPageId: dispatchedEvent.destinationPageId, + deepLink: (deepLink != nil) ? compile(deepLink ?? "", variable) : nil, + payload: dispatchedEvent.payload?.map({ prop in + return Property( + name: prop.name ?? "", + value: compile(prop.value ?? "", variable), + ptype: prop.ptype ?? PropertyType.STRING + ) + }), + httpRequest: dispatchedEvent.httpRequest, + httpResponseAssertion: dispatchedEvent.httpResponseAssertion + ) - let assertion = dispatchedEvent.httpResponseAssertion + let assertion = compiledEvent.httpResponseAssertion let handleEvent = { () -> () in DispatchQueue.main.async { - let event = UIBlockEventDispatcher( - name: dispatchedEvent.name, - destinationPageId: dispatchedEvent.destinationPageId, - deepLink: dispatchedEvent.deepLink, - payload: dispatchedEvent.payload?.map({ prop in - return Property( - name: prop.name ?? "", - value: compile(prop.value ?? "", variable), - ptype: prop.ptype ?? PropertyType.STRING - ) - }), - httpRequest: dispatchedEvent.httpRequest, - httpResponseAssertion: dispatchedEvent.httpResponseAssertion - ) - parentEventManager?.dispatch(event: event) + parentEventManager?.dispatch(event: compiledEvent) } } - if let httpRequest = dispatchedEvent.httpRequest { + if let httpRequest = compiledEvent.httpRequest { Task { Task.detached { [weak self] in let result = await self?.container.sendHttpRequest( diff --git a/ios/Nativebrik/Nativebrik/component/renderer/modal-component.swift b/ios/Nativebrik/Nativebrik/component/renderer/modal-component.swift index b2fd3d8..ad93285 100644 --- a/ios/Nativebrik/Nativebrik/component/renderer/modal-component.swift +++ b/ios/Nativebrik/Nativebrik/component/renderer/modal-component.swift @@ -72,7 +72,11 @@ class ModalComponentViewController: UIViewController { } func presentToTop(_ viewController: UIViewController) { - self.view.window?.rootViewController?.present(viewController, animated: true) + guard let root = self.view.window?.rootViewController else { + return + } + let top = findTopPresenting(root) + top.present(viewController, animated: true) } @objc func dismissModal() { diff --git a/ios/Nativebrik/Nativebrik/sdk.swift b/ios/Nativebrik/Nativebrik/sdk.swift index 7cb475a..ce33d3c 100644 --- a/ios/Nativebrik/Nativebrik/sdk.swift +++ b/ios/Nativebrik/Nativebrik/sdk.swift @@ -9,7 +9,7 @@ import Foundation import SwiftUI import Combine -public let nativebrikSdkVersion = "0.5.1" +public let nativebrikSdkVersion = "0.5.2" public let isNativebrikAvailable: Bool = { if #available(iOS 15.0, *) { return true @@ -22,7 +22,9 @@ func openLink(_ event: ComponentEvent) -> Void { guard let link = event.deepLink else { return } - let url = URL(string: link)! + guard let url = URL(string: link) else { + return + } if UIApplication.shared.canOpenURL(url) { if #available(iOS 10.0, *) { UIApplication.shared.open(url, options: [:]) @@ -32,12 +34,23 @@ func openLink(_ event: ComponentEvent) -> Void { } } +func createDispatchNativebrikEvent(_ client: NativebrikClient) -> (_ event: ComponentEvent) -> Void { + return { event in + guard let name = event.name else { + return + } + if name.isEmpty { + return + } + client.experiment.dispatch(NativebrikEvent(name)) + } +} + class Config { let projectId: String var url: String = "https://nativebrik.com/client" var trackUrl: String = "https://track.nativebrik.com/track/v1" var cdnUrl: String = "https://cdn.nativebrik.com" - static let defaultListeners: [((_ event: ComponentEvent) -> Void)] = [openLink] var eventListeners: [((_ event: ComponentEvent) -> Void)] = [] init() { @@ -46,22 +59,25 @@ class Config { init( projectId: String, - onEvent: ((_ event: ComponentEvent) -> Void)? = nil + onEvents: [((_ event: ComponentEvent) -> Void)?] = [] ) { self.projectId = projectId - if let onEvent = onEvent { - self.eventListeners.append(onEvent) + onEvents.forEach { onEvent in + if let onEvent = onEvent { + self.eventListeners.append(onEvent) + } } } + func addEventListner(_ onEvent: @escaping (_ event: ComponentEvent) -> Void) { + self.eventListeners.append(onEvent) + } + func dispatchUIBlockEvent(event: UIBlockEventDispatcher) { let e = convertEvent(event) for listener in eventListeners { listener(e) } - for listener in Config.defaultListeners { - listener(e) - } } } @@ -106,7 +122,10 @@ public class NativebrikClient: ObservableObject { httpRequestInterceptor: NativebrikHttpRequestInterceptor? = nil ) { let user = NativebrikUser() - let config = Config(projectId: projectId, onEvent: onEvent) + let config = Config(projectId: projectId, onEvents: [ + openLink, + onEvent + ]) let persistentContainer = createNativebrikCoreDataHelper() self.user = user self.config = config @@ -118,6 +137,8 @@ public class NativebrikClient: ObservableObject { ) self.overlayVC = OverlayViewController(user: self.user, container: self.container) self.experiment = NativebrikExperiment(container: self.container, overlay: self.overlayVC) + + config.addEventListner(createDispatchNativebrikEvent(self)) } }