Skip to content

Commit 0e2cc10

Browse files
Merge branch 'development' into fix/Expose-RoktLayout-Object
2 parents 565703d + 1696f39 commit 0e2cc10

2 files changed

Lines changed: 81 additions & 45 deletions

File tree

mParticle-Rokt-Swift/MPRoktLayout.swift

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import mParticle_Apple_SDK
1717
import mParticle_Rokt
1818

1919
@available(iOS 15, *)
20-
public struct MPRoktLayout: View {
21-
public var roktLayout: RoktLayout
20+
public class MPRoktLayout {
21+
public var roktLayout: RoktLayout? = nil
22+
let mparticle = MParticle.sharedInstance()
2223

2324
public init(
2425
sdkTriggered: Binding<Bool>,
@@ -28,19 +29,72 @@ public struct MPRoktLayout: View {
2829
config: RoktConfig? = nil,
2930
onEvent: ((RoktEvent) -> Void)? = nil
3031
) {
31-
let preparedAttributes = MPKitRokt.prepareAttributes(attributes, filteredUser: Optional<FilteredMParticleUser>.none, performMapping: true)
32+
confirmUser(attributes: attributes) {
33+
let preparedAttributes = MPKitRokt.prepareAttributes(attributes, filteredUser: Optional<FilteredMParticleUser>.none, performMapping: true)
3234

33-
self.roktLayout = RoktLayout.init(
34-
sdkTriggered: sdkTriggered,
35-
viewName: viewName,
36-
locationName: locationName,
37-
attributes: preparedAttributes,
38-
config: config,
39-
onEvent: onEvent
40-
)
35+
self.roktLayout = RoktLayout.init(
36+
sdkTriggered: sdkTriggered,
37+
viewName: viewName,
38+
locationName: locationName,
39+
attributes: preparedAttributes,
40+
config: config,
41+
onEvent: onEvent
42+
)
43+
}
4144
}
42-
43-
public var body: some View {
44-
return self.roktLayout.body
45+
46+
func confirmUser(
47+
attributes: [String: String]?,
48+
completion: @escaping () -> Void
49+
) {
50+
guard let user = mparticle.identity.currentUser else {
51+
completion()
52+
return
53+
}
54+
let email = attributes?["email"]
55+
let hashedEmail = attributes?["emailsha256"]
56+
57+
let userEmailIdentity = user.identities[NSNumber(value: MPIdentity.email.rawValue)]
58+
let userHashedEmailIdentity = user.identities[NSNumber(value: MPIdentity.other.rawValue)]
59+
60+
let emailMismatch = email != nil && email != userEmailIdentity
61+
let hashedEmailMismatch = hashedEmail != nil && hashedEmail != userHashedEmailIdentity
62+
63+
if emailMismatch || hashedEmailMismatch {
64+
// If there is an existing email or hashed email but it doesn't match what was passed in, warn the customer
65+
if emailMismatch {
66+
print("The existing email on the user (\(userEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(email ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
67+
}
68+
if hashedEmailMismatch {
69+
print("The existing hashed email on the user (\(userHashedEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
70+
}
71+
72+
syncIdentities(user: user, email: email, hashedEmail: hashedEmail, completion: completion)
73+
} else {
74+
completion()
75+
}
76+
}
77+
78+
func syncIdentities(
79+
user: MParticleUser,
80+
email: String?,
81+
hashedEmail: String?,
82+
completion: @escaping () -> Void
83+
) {
84+
let identityRequest = MPIdentityApiRequest(user: user)
85+
identityRequest.setIdentity(email, identityType: .email)
86+
identityRequest.setIdentity(hashedEmail, identityType: .other)
87+
88+
mparticle.identity.identify(identityRequest) {apiResult, error in
89+
if let error = error {
90+
print("Failed to sync email from selectPlacement to user: \(error)")
91+
completion()
92+
} else {
93+
if let identities = apiResult?.user.identities {
94+
print("Updated user identity based off selectPlacement's attributes: \(identities)")
95+
}
96+
completion()
97+
}
98+
}
4599
}
46100
}

mParticle_RoktTests/mParticle_Rokt_SwiftTests.swift

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct mParticle_Rokt_SwiftTests {
3131
)
3232

3333
// Then
34-
#expect(layout.body != nil, "Layout body should not be nil")
34+
#expect(layout.roktLayout != nil, "Layout.roktLayout should not be nil")
3535
}
3636

3737
@MainActor @available(iOS 15, *)
@@ -58,7 +58,7 @@ struct mParticle_Rokt_SwiftTests {
5858
)
5959

6060
// Then
61-
#expect(layout.body != nil, "Layout body should not be nil")
61+
#expect(layout.roktLayout != nil, "Layout.roktLayout should not be nil")
6262
}
6363

6464
@MainActor @available(iOS 15, *)
@@ -76,7 +76,7 @@ struct mParticle_Rokt_SwiftTests {
7676
)
7777

7878
// Then
79-
#expect(layout.body != nil, "Layout should handle empty attributes")
79+
#expect(layout.roktLayout != nil, "Layout should handle empty attributes")
8080
}
8181

8282
@MainActor @available(iOS 15, *)
@@ -94,7 +94,7 @@ struct mParticle_Rokt_SwiftTests {
9494
)
9595

9696
// Then
97-
#expect(layout.body != nil, "Layout should handle sandbox attribute")
97+
#expect(layout.roktLayout != nil, "Layout should handle sandbox attribute")
9898
}
9999

100100
// MARK: - Attribute Preparation Tests
@@ -183,25 +183,7 @@ struct mParticle_Rokt_SwiftTests {
183183
)
184184

185185
// Then
186-
#expect(layout is any View, "MPRoktLayout should conform to SwiftUI View protocol")
187-
}
188-
189-
@MainActor @available(iOS 15, *)
190-
@Test func testMPRoktLayoutBodyProperty() {
191-
// Given
192-
let sdkTriggered = Binding.constant(false)
193-
let attributes: [String: String] = ["test": "value"]
194-
195-
// When
196-
let layout = MPRoktLayout(
197-
sdkTriggered: sdkTriggered,
198-
locationName: "test",
199-
attributes: attributes
200-
)
201-
202-
// Then
203-
let body = layout.body
204-
#expect(body != nil, "Layout body should be accessible")
186+
#expect(layout.roktLayout is any View, "MPRoktLayout should conform to SwiftUI View protocol")
205187
}
206188

207189
// MARK: - Parameter Validation Tests
@@ -212,16 +194,16 @@ struct mParticle_Rokt_SwiftTests {
212194
let sdkTriggered = Binding.constant(false)
213195
let longLocationName = String(repeating: "a", count: 1000)
214196
let attributes: [String: String] = ["test": "value"]
215-
197+
216198
// When
217199
let layout = MPRoktLayout(
218200
sdkTriggered: sdkTriggered,
219201
locationName: longLocationName,
220202
attributes: attributes
221203
)
222-
204+
223205
// Then
224-
#expect(layout.body != nil, "Layout should handle long location names")
206+
#expect(layout.roktLayout != nil, "Layout should handle long location names")
225207
}
226208

227209
@MainActor @available(iOS 15, *)
@@ -242,7 +224,7 @@ struct mParticle_Rokt_SwiftTests {
242224
)
243225

244226
// Then
245-
#expect(layout.body != nil, "Layout should handle special characters and unicode")
227+
#expect(layout.roktLayout != nil, "Layout should handle special characters and unicode")
246228
}
247229

248230
// MARK: - State Management Tests
@@ -261,13 +243,13 @@ struct mParticle_Rokt_SwiftTests {
261243
)
262244

263245
// Then
264-
#expect(layout.body != nil, "Layout should be created with initial state")
246+
#expect(layout.roktLayout != nil, "Layout should be created with initial state")
265247

266248
// When state changes
267249
sdkTriggered.wrappedValue = true
268250

269251
// Then
270-
#expect(layout.body != nil, "Layout should handle state changes")
252+
#expect(layout.roktLayout != nil, "Layout should handle state changes")
271253
}
272254

273255
// MARK: - Integration Tests
@@ -291,7 +273,7 @@ struct mParticle_Rokt_SwiftTests {
291273
)
292274

293275
// Then
294-
#expect(layout.body != nil, "Layout should properly integrate attribute processing")
276+
#expect(layout.roktLayout == nil, "Layout should attempt to identify user attributes and fail")
295277
}
296278

297279
@MainActor @available(iOS 15, *)
@@ -325,6 +307,6 @@ struct mParticle_Rokt_SwiftTests {
325307
)
326308

327309
// Then
328-
#expect(layout.body != nil, "Layout should handle complex configurations")
310+
#expect(layout.roktLayout != nil, "Layout should handle complex configurations")
329311
}
330312
}

0 commit comments

Comments
 (0)