Skip to content

Commit

Permalink
Merge pull request #20 from plaidev/android-1
Browse files Browse the repository at this point in the history
[ios] impl arguments feature for embedding
  • Loading branch information
RyosukeCla authored Mar 21, 2024
2 parents 05ac1d7 + 515f572 commit 4cb3085
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
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.4.2'
s.version = '0.5.0'
s.summary = 'Nativebrik SDK'
s.description = <<-DESC
Nativebrik SDK for iOS.
Expand Down
8 changes: 7 additions & 1 deletion ios/Nativebrik/Nativebrik/data/cotnainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class ContainerImpl: Container {
private let databaseRepository: DatabaseRepository
private let httpRequestRepository: HttpRequestRepository

private let arguments: Any?

init(config: Config, user: NativebrikUser, persistentContainer: NSPersistentContainer, intercepter: NativebrikHttpRequestInterceptor? = nil) {
self.config = config
self.user = user
Expand All @@ -88,13 +90,15 @@ class ContainerImpl: Container {
self.formRepository = nil
self.databaseRepository = DatabaseRepositoryImpl(persistentContainer: persistentContainer)
self.httpRequestRepository = HttpRequestRepositoryImpl(intercepter: intercepter)

self.arguments = nil
}

// should be refactored.
// this is because, i wanted to initialize form instance for each component, not to share the same instance from every components.
// this is called when component is instantiated.
// bad code.
init(_ container: ContainerImpl) {
init(_ container: ContainerImpl, arguments: Any?) {
self.config = container.config
self.user = container.user
self.persistentContainer = container.persistentContainer
Expand All @@ -104,6 +108,7 @@ class ContainerImpl: Container {
self.formRepository = FormRepositoryImpl()
self.databaseRepository = container.databaseRepository
self.httpRequestRepository = container.httpRequestRepository
self.arguments = arguments
}

func handleEvent(_ it: UIBlockEventDispatcher) {
Expand All @@ -116,6 +121,7 @@ class ContainerImpl: Container {
data: data,
properties: properties,
form: self.formRepository?.getFormData(),
arguments: self.arguments,
projectId: self.config.projectId
)
}
Expand Down
3 changes: 3 additions & 0 deletions ios/Nativebrik/Nativebrik/data/variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func _createVariableForTemplate(
data: Any? = nil,
properties: [Property]? = nil,
form: [String: Any]? = nil,
arguments: Any? = nil,
projectId: String? = nil
) -> Any {
let userData: [String:Any] = (user != nil) ? [
Expand All @@ -34,6 +35,7 @@ func _createVariableForTemplate(
"user": (userData.isEmpty ? nil : userData) as Any,
"props": (propertiesData.isEmpty ? nil : propertiesData) as Any,
"form": (formData?.isEmpty == true ? nil : formData) as Any,
"args": arguments as Any,
"data": data as Any,
"project": projectData as Any,
]
Expand All @@ -48,6 +50,7 @@ func _mergeVariable(base: Any?, _ overlay: Any?) -> Any? {
"user": overlay?["user"] ?? base["user"] as Any,
"props": overlay?["props"] ?? base["props"] as Any,
"form": overlay?["form"] ?? base["form"] as Any,
"args": overlay?["args"] ?? base["args"] as Any,
"data": overlay?["data"] ?? base["data"] as Any,
"project": overlay?["project"] ?? base["project"] as Any,
]
Expand Down
16 changes: 8 additions & 8 deletions ios/Nativebrik/Nativebrik/remote-config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,30 @@ public class RemoteConfigVariant {

public func getAsView(
_ key: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil
) -> some View {
let componentId = self.get(key)
return EmbeddingSwiftView(
experimentId: self.experimentId,
componentId: componentId,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.modalViewController,
onEvent: onEvent
)
}

public func getAsView<V: View>(
_ key: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil,
@ViewBuilder content: (@escaping (_ phase: AsyncEmbeddingPhase) -> V)
) -> some View {
let componentId = self.get(key)
return EmbeddingSwiftView.init<V>(
experimentId: self.experimentId,
componentId: componentId,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.modalViewController,
onEvent: onEvent,
content: content
Expand All @@ -109,7 +109,7 @@ public class RemoteConfigVariant {

public func getAsUIView(
_ key: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil
) -> UIView? {
guard let componentId = self.get(key) else {
Expand All @@ -118,7 +118,7 @@ public class RemoteConfigVariant {
let uiview = EmbeddingUIView(
experimentId: self.experimentId,
componentId: componentId,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.modalViewController,
onEvent: onEvent,
fallback: nil
Expand All @@ -128,7 +128,7 @@ public class RemoteConfigVariant {

public func getAsUIView(
_ key: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil,
content: @escaping (_ phase: EmbeddingPhase) -> UIView
) -> UIView? {
Expand All @@ -138,7 +138,7 @@ public class RemoteConfigVariant {
let uiview = EmbeddingUIView(
experimentId: self.experimentId,
componentId: componentId,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.modalViewController,
onEvent: onEvent,
fallback: content
Expand Down
20 changes: 10 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.4.2"
public let nativebrikSdkVersion = "0.5.0"
public let isNativebrikAvailable: Bool = {
if #available(iOS 15.0, *) {
return true
Expand Down Expand Up @@ -152,23 +152,23 @@ public class NativebrikExperiment {

public func embedding(
_ id: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil
) -> some View {
if !isNativebrikAvailable {
return AnyView(EmptyView())
}
return AnyView(EmbeddingSwiftView(
experimentId: id,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.overlayVC.modalViewController,
onEvent: onEvent
))
}

public func embedding<V: View>(
_ id: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil,
@ViewBuilder content: (@escaping (_ phase: AsyncEmbeddingPhase) -> V)
) -> some View {
Expand All @@ -178,7 +178,7 @@ public class NativebrikExperiment {
return AnyView(EmbeddingSwiftView.init<V>(
experimentId: id,
componentId: nil,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.overlayVC.modalViewController,
onEvent: onEvent,
content: content
Expand All @@ -187,15 +187,15 @@ public class NativebrikExperiment {

public func embeddingUIView(
_ id: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil
) -> UIView {
if !isNativebrikAvailable {
return UIView()
}
return EmbeddingUIView(
experimentId: id,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.overlayVC.modalViewController,
onEvent: onEvent,
fallback: nil
Expand All @@ -204,7 +204,7 @@ public class NativebrikExperiment {

public func embeddingUIView(
_ id: String,
arguments: [String:Any?]? = nil,
arguments: Any? = nil,
onEvent: ((_ event: ComponentEvent) -> Void)? = nil,
content: @escaping (_ phase: EmbeddingPhase) -> UIView
) -> UIView {
Expand All @@ -213,7 +213,7 @@ public class NativebrikExperiment {
}
return EmbeddingUIView(
experimentId: id,
container: ContainerImpl(self.container as! ContainerImpl),
container: ContainerImpl(self.container as! ContainerImpl, arguments: arguments),
modalViewController: self.overlayVC.modalViewController,
onEvent: onEvent,
fallback: content
Expand All @@ -230,7 +230,7 @@ public class NativebrikExperiment {
}
let _ = RemoteConfig(
experimentId: id,
container: ContainerImpl(self.container as! ContainerImpl),
container: self.container,
modalViewController: self.overlayVC.modalViewController,
phase: phase
)
Expand Down

0 comments on commit 4cb3085

Please sign in to comment.