Skip to content

Commit

Permalink
Merge pull request #24 from plaidev/android-1
Browse files Browse the repository at this point in the history
[ios] fix presnetTop bug, refactor event compiling, dispatch nativebrik event when its set
  • Loading branch information
RyosukeCla committed Mar 25, 2024
2 parents adf7d9d + 61478e1 commit 85c2ac0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 48 deletions.
2 changes: 1 addition & 1 deletion 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.
Expand Down
20 changes: 1 addition & 19 deletions ios/Nativebrik/Nativebrik/component/action-listener.swift
Expand Up @@ -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 {
Expand Down
38 changes: 21 additions & 17 deletions ios/Nativebrik/Nativebrik/component/page.swift
Expand Up @@ -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(
Expand Down
Expand Up @@ -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() {
Expand Down
41 changes: 31 additions & 10 deletions ios/Nativebrik/Nativebrik/sdk.swift
Expand Up @@ -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
Expand All @@ -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: [:])
Expand All @@ -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() {
Expand All @@ -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)
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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))
}
}

Expand Down

0 comments on commit 85c2ac0

Please sign in to comment.