Skip to content

Commit

Permalink
Merge branch 'as/refactor-attempt-1' into as/chnl-7046-empty-check
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaysubra committed Apr 3, 2024
2 parents 9b688f3 + 1986a38 commit e87364e
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions Sources/KlaviyoSwift/KlaviyoState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,24 @@ struct KlaviyoState: Equatable, Codable {
}

mutating func updateEmail(email: String) {
if email.isEmpty || email == self.email {
logDevWarning(for: "email")
return
if email.isNotEmptyOrSame(as: self.email, identifier: "email") {
self.email = email
enqueueProfileOrTokenRequest()
}

self.email = email
enqueueProfileOrTokenRequest()
}

mutating func updateExternalId(externalId: String) {
if externalId.isEmpty || externalId == self.externalId {
logDevWarning(for: "externalId")
return
if externalId.isNotEmptyOrSame(as: self.externalId, identifier: "external Id") {
self.externalId = externalId
enqueueProfileOrTokenRequest()
}

self.externalId = externalId
enqueueProfileOrTokenRequest()
}

mutating func updatePhoneNumber(phoneNumber: String) {
if phoneNumber.isEmpty || phoneNumber == self.phoneNumber {
logDevWarning(for: "phone number")
return
if phoneNumber.isNotEmptyOrSame(as: self.phoneNumber, identifier: "phone number") {
self.phoneNumber = phoneNumber
enqueueProfileOrTokenRequest()
}

self.phoneNumber = phoneNumber
enqueueProfileOrTokenRequest()
}

mutating func enqueueProfileOrTokenRequest() {
Expand Down Expand Up @@ -185,28 +176,19 @@ struct KlaviyoState: Equatable, Codable {
}

mutating func updateStateWithProfile(profile: Profile) {
if let profileEmail = profile.email {
if !profileEmail.isEmpty, profileEmail != email {
email = profileEmail
} else {
logDevWarning(for: "email")
}
if let profileEmail = profile.email,
profileEmail.isNotEmptyOrSame(as: self.email, identifier: "email") {
email = profileEmail
}

if let profilePhoneNumber = profile.phoneNumber {
if !profilePhoneNumber.isEmpty, profilePhoneNumber != phoneNumber {
phoneNumber = profilePhoneNumber
} else {
logDevWarning(for: "phone number")
}
if let profilePhoneNumber = profile.phoneNumber,
profilePhoneNumber.isNotEmptyOrSame(as: self.phoneNumber, identifier: "phone number") {
phoneNumber = profilePhoneNumber
}

if let profileExternalId = profile.externalId {
if !profileExternalId.isEmpty, profileExternalId != externalId {
externalId = profileExternalId
} else {
logDevWarning(for: "external id")
}
if let profileExternalId = profile.externalId,
profileExternalId.isNotEmptyOrSame(as: self.externalId, identifier: "external id") {
externalId = profileExternalId
}
}

Expand Down Expand Up @@ -521,3 +503,14 @@ extension Profile {
return profile
}
}

extension String {
fileprivate func isNotEmptyOrSame(as state: String?, identifier: String) -> Bool {
let incoming = self
if incoming.isEmpty || incoming == state {
logDevWarning(for: identifier)
}

return !incoming.isEmpty && incoming != state
}
}

0 comments on commit e87364e

Please sign in to comment.