Skip to content
Closed
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
17 changes: 5 additions & 12 deletions mParticle-Rokt-Swift/MPRoktLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class MPRoktLayout {
public var roktLayout: RoktLayout? = nil
let mparticle = MParticle.sharedInstance()

/// - Parameter sdkTriggered: Pass the current trigger value (e.g. from `@State var sdkTriggered`). When the host sets `sdkTriggered = true`, SwiftUI re-renders and passes the new value to MPRoktLayout and RoktLayout.
public init(
sdkTriggered: Binding<Bool>,
sdkTriggered: Bool,
viewName: String? = nil,
locationName: String = "",
attributes: [String: String],
Expand All @@ -35,14 +36,14 @@ public class MPRoktLayout {
dynamicPerformanceMarkers: [:]
)

MPRoktLayout.mpLog("Initializing MPRoktLayout with arguments sdkTriggered:\(sdkTriggered.wrappedValue), viewName:\(viewName ?? "nil"), locationName:\(locationName), attributes:\(attributes)")
confirmUser(attributes: attributes) { identifyCalled in
MPRoktLayout.mpLog("Initializing MPRoktLayout with arguments sdkTriggered:\(sdkTriggered), viewName:\(viewName ?? "nil"), locationName:\(locationName), attributes:\(attributes)")
confirmUser(attributes: attributes) { _ in
let preparedAttributes = MPKitRokt.prepareAttributes(attributes, filteredUser: Optional<FilteredMParticleUser>.none, performMapping: true)

// Log custom event for selectPlacements call
MPKitRokt.logSelectPlacementEvent(preparedAttributes)

MPRoktLayout.mpLog("Initializing RoktLayout with arguments sdkTriggered:\(sdkTriggered.wrappedValue), viewName: \(viewName ?? "nil"), locationName:\(locationName), attributes:\(preparedAttributes)")
MPRoktLayout.mpLog("Initializing RoktLayout with arguments sdkTriggered:\(sdkTriggered), viewName: \(viewName ?? "nil"), locationName:\(locationName), attributes:\(preparedAttributes)")
self.roktLayout = RoktLayout.init(
sdkTriggered: sdkTriggered,
viewName: viewName,
Expand All @@ -52,14 +53,6 @@ public class MPRoktLayout {
placementOptions: options,
onEvent: onEvent
)
// The Binding variable provided by the client allows us to trigger a re-render of the UI but we only want to do this if the value was true to start
if identifyCalled && sdkTriggered.wrappedValue {
MPRoktLayout.mpLog("Triggering Rokt Swift UI re-render")
DispatchQueue.main.async {
sdkTriggered.wrappedValue = false
sdkTriggered.wrappedValue = true
}
}
}
}

Expand Down
50 changes: 21 additions & 29 deletions mParticle_RoktTests/mParticle_Rokt_SwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutBasicInitialization() {
// Given
let sdkTriggered = Binding.constant(false)
let locationName = "test_location"
let attributes: [String: String] = ["key1": "value1", "key2": "value2"]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: locationName,
attributes: attributes
)
Expand All @@ -37,7 +36,6 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutInitializationWithAllParameters() {
// Given
let sdkTriggered = Binding.constant(true)
let viewName = "test_view"
let locationName = "test_location"
let attributes: [String: String] = ["user_id": "12345", "sandbox": "true"]
Expand All @@ -49,7 +47,7 @@ struct mParticle_Rokt_SwiftTests {

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: true,
viewName: viewName,
locationName: locationName,
attributes: attributes,
Expand All @@ -64,13 +62,12 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutInitializationWithEmptyAttributes() {
// Given
let sdkTriggered = Binding.constant(false)
let locationName = "empty_attributes_test"
let attributes: [String: String] = [:]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: locationName,
attributes: attributes
)
Expand All @@ -82,13 +79,12 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutInitializationWithSandboxAttribute() {
// Given
let sdkTriggered = Binding.constant(false)
let locationName = "sandbox_test"
let attributes: [String: String] = ["sandbox": "true", "user_id": "test_user"]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: locationName,
attributes: attributes
)
Expand Down Expand Up @@ -172,12 +168,11 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutIsSwiftUIView() {
// Given
let sdkTriggered = Binding.constant(false)
let attributes: [String: String] = ["test": "value"]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: "test",
attributes: attributes
)
Expand All @@ -191,13 +186,12 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutWithLongLocationName() {
// Given
let sdkTriggered = Binding.constant(false)
let longLocationName = String(repeating: "a", count: 1000)
let attributes: [String: String] = ["test": "value"]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: longLocationName,
attributes: attributes
)
Expand All @@ -209,7 +203,6 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutWithSpecialCharacters() {
// Given
let sdkTriggered = Binding.constant(false)
let locationName = "test_location_with_特殊字符_🎉"
let attributes: [String: String] = [
"unicode_key_🌟": "unicode_value_🎯",
Expand All @@ -218,7 +211,7 @@ struct mParticle_Rokt_SwiftTests {

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
locationName: locationName,
attributes: attributes
)
Expand All @@ -231,25 +224,26 @@ struct mParticle_Rokt_SwiftTests {

@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutSDKTriggeredStateChange() {
// Given
let sdkTriggered = Binding.constant(false)
// Given: sdkTriggered is now a value; host passes true/false and SwiftUI re-renders when state changes
let attributes: [String: String] = ["test": "value"]

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
// When: create with false
let layoutFalse = MPRoktLayout(
sdkTriggered: false,
locationName: "state_test",
attributes: attributes
)

// Then
#expect(layout.roktLayout != nil, "Layout should be created with initial state")

// When state changes
sdkTriggered.wrappedValue = true
// When: create with true (simulates host re-rendering after setting sdkTriggered = true)
let layoutTrue = MPRoktLayout(
sdkTriggered: true,
locationName: "state_test",
attributes: attributes
)

// Then
#expect(layout.roktLayout != nil, "Layout should handle state changes")
#expect(layoutFalse.roktLayout != nil, "Layout should be created with sdkTriggered false")
#expect(layoutTrue.roktLayout != nil, "Layout should be created with sdkTriggered true")
}

// MARK: - SelectPlacements Custom Event Tests
Expand Down Expand Up @@ -290,7 +284,6 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutAttributeProcessingIntegration() {
// Given
let sdkTriggered = Binding.constant(false)
let attributes: [String: String] = [
"user_id": "12345",
"email": "test@example.com",
Expand All @@ -299,7 +292,7 @@ struct mParticle_Rokt_SwiftTests {

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: false,
viewName: "integration_test",
locationName: "test_location",
attributes: attributes
Expand All @@ -312,7 +305,6 @@ struct mParticle_Rokt_SwiftTests {
@MainActor @available(iOS 15, *)
@Test func testMPRoktLayoutWithComplexConfiguration() {
// Given
let sdkTriggered = Binding.constant(true)
let viewName = "complex_config_test"
let locationName = "complex_location"
let attributes: [String: String] = [
Expand All @@ -331,7 +323,7 @@ struct mParticle_Rokt_SwiftTests {

// When
let layout = MPRoktLayout(
sdkTriggered: sdkTriggered,
sdkTriggered: true,
viewName: viewName,
locationName: locationName,
attributes: attributes,
Expand Down
Loading