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

[ios] fix presnetTop bug, refactor event compiling, dispatch nativebrik event when its set #24

Merged
merged 4 commits into from
Mar 25, 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
2 changes: 1 addition & 1 deletion Nativebrik.podspec
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading