Skip to content

Commit

Permalink
fix(Analytics): Making PinpointEndpointProfile a struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruisebas committed Jan 5, 2024
1 parent a0ee257 commit 604bbde
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension AWSPinpointAnalyticsPlugin {
}

Task {
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
currentEndpointProfile.addUserId(userId)
if let userProfile = userProfile {
currentEndpointProfile.addUserProfile(userProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ actor EndpointClient: EndpointClientBehaviour {
}

private func configure(endpointProfile: PinpointEndpointProfile) async -> PinpointEndpointProfile {
var endpointProfile = endpointProfile
var deviceToken: PinpointEndpointProfile.DeviceToken?
if let tokenData = Self.getStoredData(from: keychain, forKey: Constants.deviceTokenKey, fallbackTo: userDefaults) {
deviceToken = tokenData.asHexString()
Expand All @@ -109,6 +110,7 @@ actor EndpointClient: EndpointClientBehaviour {
}

private func updateEndpoint(with endpointProfile: PinpointEndpointProfile) async throws {
var endpointProfile = endpointProfile
endpointProfile.effectiveDate = Date()
let input = createUpdateInput(from: endpointProfile)
log.verbose("UpdateEndpointInput: \(input)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AWSPinpoint
import Foundation

@_spi(InternalAWSPinpoint)
public class PinpointEndpointProfile: Codable {
public struct PinpointEndpointProfile: Codable, Equatable {
typealias DeviceToken = String

var applicationId: String
Expand Down Expand Up @@ -45,11 +45,11 @@ public class PinpointEndpointProfile: Codable {
self.user = user
}

public func addUserId(_ userId: String) {
public mutating func addUserId(_ userId: String) {
user.userId = userId
}

public func addUserProfile(_ userProfile: UserProfile) {
public mutating func addUserProfile(_ userProfile: UserProfile) {
if let email = userProfile.email {
setCustomProperty(email, forKey: Constants.AttributeKeys.email)
}
Expand All @@ -75,18 +75,18 @@ public class PinpointEndpointProfile: Codable {
}
}

public func setAPNsToken(_ apnsToken: Data) {
public mutating func setAPNsToken(_ apnsToken: Data) {
deviceToken = apnsToken.asHexString()
}

private func addCustomProperties(_ properties: [String: UserProfilePropertyValue]?) {
private mutating func addCustomProperties(_ properties: [String: UserProfilePropertyValue]?) {
guard let properties = properties else { return }
for (key, value) in properties {
setCustomProperty(value, forKey: key)
}
}

private func addUserAttributes(_ attributes: [String: [String]]?) {
private mutating func addUserAttributes(_ attributes: [String: [String]]?) {
guard let attributes = attributes else { return }
let userAttributes = user.userAttributes ?? [:]
user.userAttributes = userAttributes.merging(
Expand All @@ -95,7 +95,7 @@ public class PinpointEndpointProfile: Codable {
)
}

private func setCustomProperty(_ value: UserProfilePropertyValue,
private mutating func setCustomProperty(_ value: UserProfilePropertyValue,
forKey key: String) {
if let value = value as? String {
attributes[key] = [value]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class EndpointClientTests: XCTestCase {
XCTAssertNotNil(keychain.dataValues[EndpointClient.Constants.endpointProfileKey])
XCTAssertNotNil(keychain.dataValues[EndpointClient.Constants.deviceTokenKey])
XCTAssertEqual(archiver.decodeCount, 1)
XCTAssertTrue(endpointProfile === storedEndpointProfile, "Expected stored PinpointEndpointProfile object")
XCTAssertNotEqual(endpointProfile, storedEndpointProfile, "Expected updated PinpointEndpointProfile object")
XCTAssertEqual(endpointProfile.applicationId, currentApplicationId)
XCTAssertEqual(endpointProfile.endpointId, currentEndpointId)
XCTAssertEqual(endpointProfile.deviceToken, newToken.asHexString())
Expand Down Expand Up @@ -104,7 +104,7 @@ class EndpointClientTests: XCTestCase {
XCTAssertEqual(keychain.dataForKeyCountMap[EndpointClient.Constants.endpointProfileKey], 1)
XCTAssertEqual(keychain.dataForKeyCountMap[EndpointClient.Constants.deviceTokenKey], 1)
XCTAssertEqual(archiver.decodeCount, 0)
XCTAssertFalse(endpointProfile === storedEndpointProfile, "Expected new PinpointEndpointProfile object")
XCTAssertNotEqual(endpointProfile, storedEndpointProfile, "Expected new PinpointEndpointProfile object")
XCTAssertEqual(endpointProfile.applicationId, currentApplicationId)
XCTAssertEqual(endpointProfile.endpointId, currentEndpointId)
XCTAssertEqual(endpointProfile.deviceToken, newToken?.asHexString())
Expand All @@ -128,7 +128,7 @@ class EndpointClientTests: XCTestCase {
func testUpdateEndpointProfile_withAPNsToken_withoutStoredToken_shouldSaveToken() async {
keychain.resetCounters()

let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
endpointId: "endpointId")
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
try? await endpointClient.updateEndpointProfile(with: endpoint)
Expand All @@ -142,7 +142,7 @@ class EndpointClientTests: XCTestCase {
storeToken("oldToken")
keychain.resetCounters()

let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
endpointId: "endpointId")
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
try? await endpointClient.updateEndpointProfile(with: endpoint)
Expand All @@ -156,7 +156,7 @@ class EndpointClientTests: XCTestCase {
storeToken(newTokenHex)
keychain.resetCounters()

let endpoint = PinpointEndpointProfile(applicationId: "applicationId",
var endpoint = PinpointEndpointProfile(applicationId: "applicationId",
endpointId: "endpointId")
endpoint.setAPNsToken(Data(hexString: newTokenHex)!)
try? await endpointClient.updateEndpointProfile(with: endpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import AppKit

extension AWSPinpointPushNotificationsPlugin {
public func identifyUser(userId: String, userProfile: UserProfile?) async throws {
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
currentEndpointProfile.addUserId(userId)
if let userProfile = userProfile {
currentEndpointProfile.addUserProfile(userProfile)
Expand All @@ -30,7 +30,7 @@ extension AWSPinpointPushNotificationsPlugin {
}

public func registerDevice(apnsToken: Data) async throws {
let currentEndpointProfile = await pinpoint.currentEndpointProfile()
var currentEndpointProfile = await pinpoint.currentEndpointProfile()
currentEndpointProfile.setAPNsToken(apnsToken)
do {
try await pinpoint.updateEndpoint(with: currentEndpointProfile,
Expand Down

0 comments on commit 604bbde

Please sign in to comment.