From 488c9e349b09b187df65808c0e79dac06297b89f Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker <33703490+BrandonStalnaker@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:07:13 -0500 Subject: [PATCH] refactor: Refactor MPUserAttributeChange to Swift (#317) * refactor: Refactor MPUserAttributeChange to Swift * Simplify logic on Zoom pairing session with Brandon --------- Co-authored-by: Ben Baron --- UnitTests/MPMessageBuilderTests.m | 4 +- UnitTests/MPUserAttributeChangeTests.m | 4 +- mParticle-Apple-SDK.xcodeproj/project.pbxproj | 18 ++---- mParticle-Apple-SDK/Include/mParticle.h | 1 + mParticle-Apple-SDK/MPBackendController.m | 1 - mParticle-Apple-SDK/Utils/MPMessageBuilder.m | 1 - .../Utils/MPUserAttributeChange.h | 16 ----- .../Utils/MPUserAttributeChange.m | 62 ------------------- .../Utils/MPUserAttributeChange.swift | 48 ++++++++++++++ 9 files changed, 59 insertions(+), 96 deletions(-) delete mode 100644 mParticle-Apple-SDK/Utils/MPUserAttributeChange.h delete mode 100644 mParticle-Apple-SDK/Utils/MPUserAttributeChange.m create mode 100644 mParticle-Apple-SDK/Utils/MPUserAttributeChange.swift diff --git a/UnitTests/MPMessageBuilderTests.m b/UnitTests/MPMessageBuilderTests.m index fc8bfeb8..6c7caf20 100644 --- a/UnitTests/MPMessageBuilderTests.m +++ b/UnitTests/MPMessageBuilderTests.m @@ -1,6 +1,7 @@ #import #import #import "MPMessageBuilder.h" +#import "mParticle.h" #import "MPIConstants.h" #import "MPSession.h" #import "MPMessage.h" @@ -13,9 +14,8 @@ #import "MPCommerceEvent.h" #import "MPCommerceEvent+Dictionary.h" #import "NSDictionary+MPCaseInsensitive.h" -#import "MPUserAttributeChange.h" +#import "MParticleSwift.h" #import "MPPersistenceController.h" -#import "mParticle.h" #import "MPBaseTestCase.h" #import "MPStateMachine.h" #import "MParticleReachability.h" diff --git a/UnitTests/MPUserAttributeChangeTests.m b/UnitTests/MPUserAttributeChangeTests.m index d46d8a44..827d098c 100644 --- a/UnitTests/MPUserAttributeChangeTests.m +++ b/UnitTests/MPUserAttributeChangeTests.m @@ -1,5 +1,5 @@ #import -#import "MPUserAttributeChange.h" +#import "MParticleSwift.h" #import "MPBaseTestCase.h" @interface MPUserAttributeChangeTests : MPBaseTestCase @@ -34,7 +34,7 @@ - (void)testInstance { XCTAssertNil(userAttributeChange.timestamp); XCTAssertEqualObjects(userAttributeChange.userAttributes, userAttributes); XCTAssertNil(userAttributeChange.value); - XCTAssertEqualObjects(userAttributeChange.valueToLog, [NSNull null]); + XCTAssertNil(userAttributeChange.valueToLog); XCTAssertTrue(userAttributeChange.changed); XCTAssertTrue(userAttributeChange.deleted); XCTAssertTrue(userAttributeChange.isArray); diff --git a/mParticle-Apple-SDK.xcodeproj/project.pbxproj b/mParticle-Apple-SDK.xcodeproj/project.pbxproj index 4d8cc6d6..489eb8ab 100644 --- a/mParticle-Apple-SDK.xcodeproj/project.pbxproj +++ b/mParticle-Apple-SDK.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ 531BCF372B28A23400F5C573 /* MPIdentityCaching.m in Sources */ = {isa = PBXBuildFile; fileRef = 531BCF332B28A23400F5C573 /* MPIdentityCaching.m */; }; 531BCF3A2B28A83E00F5C573 /* MPIdentityCachingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 531BCF392B28A83E00F5C573 /* MPIdentityCachingTests.m */; }; 531BCF3B2B28A83E00F5C573 /* MPIdentityCachingTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 531BCF392B28A83E00F5C573 /* MPIdentityCachingTests.m */; }; + 534C11B62D08F53D00466F71 /* MPUserAttributeChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534C11B52D08F53D00466F71 /* MPUserAttributeChange.swift */; }; + 534C11B72D08F53D00466F71 /* MPUserAttributeChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534C11B52D08F53D00466F71 /* MPUserAttributeChange.swift */; }; 534CD25C29CE2877008452B3 /* NSNumber+MPFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2629CDFB1F00E7489F /* NSNumber+MPFormatter.swift */; }; 534CD25E29CE2BF1008452B3 /* MParticleSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 534CD25D29CE2BF1008452B3 /* MParticleSwift.h */; }; 534CD25F29CE2BF1008452B3 /* MParticleSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 534CD25D29CE2BF1008452B3 /* MParticleSwift.h */; }; @@ -160,14 +162,12 @@ 53A79BDA29CDFB2000E7489F /* MPDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B1529CDFB1F00E7489F /* MPDevice.m */; }; 53A79BDB29CDFB2000E7489F /* MParticleWebView.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1629CDFB1F00E7489F /* MParticleWebView.h */; }; 53A79BDC29CDFB2000E7489F /* MPLaunchInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1729CDFB1F00E7489F /* MPLaunchInfo.h */; }; - 53A79BDE29CDFB2000E7489F /* MPUserAttributeChange.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1929CDFB1F00E7489F /* MPUserAttributeChange.h */; }; 53A79BDF29CDFB2000E7489F /* MPMessageBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1A29CDFB1F00E7489F /* MPMessageBuilder.h */; }; 53A79BE229CDFB2000E7489F /* MPIUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B1D29CDFB1F00E7489F /* MPIUserDefaults.m */; }; 53A79BE329CDFB2000E7489F /* MPBracket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B1E29CDFB1F00E7489F /* MPBracket.cpp */; }; 53A79BE829CDFB2000E7489F /* MPUploadBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B2329CDFB1F00E7489F /* MPUploadBuilder.h */; }; 53A79BE929CDFB2000E7489F /* MPApplication.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B2429CDFB1F00E7489F /* MPApplication.h */; settings = {ATTRIBUTES = (Public, ); }; }; 53A79BEB29CDFB2000E7489F /* NSNumber+MPFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2629CDFB1F00E7489F /* NSNumber+MPFormatter.swift */; }; - 53A79BEC29CDFB2000E7489F /* MPUserAttributeChange.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2729CDFB1F00E7489F /* MPUserAttributeChange.m */; }; 53A79BEE29CDFB2000E7489F /* MPLaunchInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2929CDFB1F00E7489F /* MPLaunchInfo.m */; }; 53A79BEF29CDFB2000E7489F /* MParticleWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2A29CDFB1F00E7489F /* MParticleWebView.m */; }; 53A79BF029CDFB2000E7489F /* MPDevice.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B2B29CDFB1F00E7489F /* MPDevice.h */; }; @@ -382,7 +382,6 @@ 53A79D5129CE23F700E7489F /* MParticleReachability.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B0029CDFB1F00E7489F /* MParticleReachability.h */; }; 53A79D5329CE23F700E7489F /* MPCustomModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B3029CDFB1F00E7489F /* MPCustomModule.h */; }; 53A79D5529CE23F700E7489F /* MPPersistenceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79ABB29CDFB1E00E7489F /* MPPersistenceController.h */; }; - 53A79D5729CE23F700E7489F /* MPUserAttributeChange.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1929CDFB1F00E7489F /* MPUserAttributeChange.h */; }; 53A79D5829CE23F700E7489F /* MPIdentityDTO.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79AA029CDFB1E00E7489F /* MPIdentityDTO.h */; }; 53A79D5929CE23F700E7489F /* MPMessageBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79B1A29CDFB1F00E7489F /* MPMessageBuilder.h */; }; 53A79D5A29CE23F700E7489F /* MPURLRequestBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 53A79AA829CDFB1E00E7489F /* MPURLRequestBuilder.h */; }; @@ -466,7 +465,6 @@ 53A79DB929CE23F700E7489F /* MPKitContainer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B5029CDFB1F00E7489F /* MPKitContainer.mm */; }; 53A79DBA29CE23F700E7489F /* MPUploadBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B1229CDFB1F00E7489F /* MPUploadBuilder.m */; }; 53A79DBC29CE23F700E7489F /* MPBaseProjection.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B5329CDFB1F00E7489F /* MPBaseProjection.m */; }; - 53A79DBE29CE23F700E7489F /* MPUserAttributeChange.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79B2729CDFB1F00E7489F /* MPUserAttributeChange.m */; }; 53A79DBF29CE23F700E7489F /* MPIdentityApiManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 53A79AA229CDFB1E00E7489F /* MPIdentityApiManager.m */; }; 53B28FB22C938C26009072FC /* MPLocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53B28FB12C938C26009072FC /* MPLocationManager.swift */; }; 53B28FB32C938C26009072FC /* MPLocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53B28FB12C938C26009072FC /* MPLocationManager.swift */; }; @@ -529,6 +527,7 @@ 531BCF322B28A23400F5C573 /* MPIdentityCaching.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MPIdentityCaching.h; sourceTree = ""; }; 531BCF332B28A23400F5C573 /* MPIdentityCaching.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPIdentityCaching.m; sourceTree = ""; }; 531BCF392B28A83E00F5C573 /* MPIdentityCachingTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPIdentityCachingTests.m; sourceTree = ""; }; + 534C11B52D08F53D00466F71 /* MPUserAttributeChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPUserAttributeChange.swift; sourceTree = ""; }; 534CD25D29CE2BF1008452B3 /* MParticleSwift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MParticleSwift.h; sourceTree = ""; }; 534CD2AA29CE2CE1008452B3 /* mParticle-Apple-SDK-NoLocationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "mParticle-Apple-SDK-NoLocationTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 5399DDB72CA727E1006526E1 /* MPZip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPZip.swift; sourceTree = ""; }; @@ -603,14 +602,12 @@ 53A79B1529CDFB1F00E7489F /* MPDevice.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDevice.m; sourceTree = ""; }; 53A79B1629CDFB1F00E7489F /* MParticleWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MParticleWebView.h; sourceTree = ""; }; 53A79B1729CDFB1F00E7489F /* MPLaunchInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPLaunchInfo.h; sourceTree = ""; }; - 53A79B1929CDFB1F00E7489F /* MPUserAttributeChange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPUserAttributeChange.h; sourceTree = ""; }; 53A79B1A29CDFB1F00E7489F /* MPMessageBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPMessageBuilder.h; sourceTree = ""; }; 53A79B1D29CDFB1F00E7489F /* MPIUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPIUserDefaults.m; sourceTree = ""; }; 53A79B1E29CDFB1F00E7489F /* MPBracket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MPBracket.cpp; sourceTree = ""; }; 53A79B2329CDFB1F00E7489F /* MPUploadBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPUploadBuilder.h; sourceTree = ""; }; 53A79B2429CDFB1F00E7489F /* MPApplication.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPApplication.h; sourceTree = ""; }; 53A79B2629CDFB1F00E7489F /* NSNumber+MPFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSNumber+MPFormatter.swift"; sourceTree = ""; }; - 53A79B2729CDFB1F00E7489F /* MPUserAttributeChange.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPUserAttributeChange.m; sourceTree = ""; }; 53A79B2929CDFB1F00E7489F /* MPLaunchInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPLaunchInfo.m; sourceTree = ""; }; 53A79B2A29CDFB1F00E7489F /* MParticleWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MParticleWebView.m; sourceTree = ""; }; 53A79B2B29CDFB1F00E7489F /* MPDevice.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDevice.h; sourceTree = ""; }; @@ -1016,18 +1013,17 @@ 53A79B1529CDFB1F00E7489F /* MPDevice.m */, 53A79B1629CDFB1F00E7489F /* MParticleWebView.h */, 53A79B1729CDFB1F00E7489F /* MPLaunchInfo.h */, - 53A79B1929CDFB1F00E7489F /* MPUserAttributeChange.h */, 53A79B1A29CDFB1F00E7489F /* MPMessageBuilder.h */, 53A79B1D29CDFB1F00E7489F /* MPIUserDefaults.m */, 53A79B1E29CDFB1F00E7489F /* MPBracket.cpp */, 53A79B2329CDFB1F00E7489F /* MPUploadBuilder.h */, 53A79B2629CDFB1F00E7489F /* NSNumber+MPFormatter.swift */, - 53A79B2729CDFB1F00E7489F /* MPUserAttributeChange.m */, 53A79B2929CDFB1F00E7489F /* MPLaunchInfo.m */, 53A79B2A29CDFB1F00E7489F /* MParticleWebView.m */, 53A79B2B29CDFB1F00E7489F /* MPDevice.h */, 53A79B2C29CDFB1F00E7489F /* MPBracket.h */, 53FDD1BC2AE871AF003D5FA1 /* MPIHasher.swift */, + 534C11B52D08F53D00466F71 /* MPUserAttributeChange.swift */, ); path = Utils; sourceTree = ""; @@ -1365,7 +1361,6 @@ 53A79BC629CDFB2000E7489F /* MParticleReachability.h in Headers */, 53A79BF429CDFB2000E7489F /* MPCustomModule.h in Headers */, 53A79B8629CDFB2000E7489F /* MPPersistenceController.h in Headers */, - 53A79BDE29CDFB2000E7489F /* MPUserAttributeChange.h in Headers */, 53A79B6E29CDFB2000E7489F /* MPIdentityDTO.h in Headers */, 53A79BDF29CDFB2000E7489F /* MPMessageBuilder.h in Headers */, 53A79B7429CDFB2000E7489F /* MPURLRequestBuilder.h in Headers */, @@ -1458,7 +1453,6 @@ 53A79D5129CE23F700E7489F /* MParticleReachability.h in Headers */, 53A79D5329CE23F700E7489F /* MPCustomModule.h in Headers */, 53A79D5529CE23F700E7489F /* MPPersistenceController.h in Headers */, - 53A79D5729CE23F700E7489F /* MPUserAttributeChange.h in Headers */, 53A79D5829CE23F700E7489F /* MPIdentityDTO.h in Headers */, 53A79D5929CE23F700E7489F /* MPMessageBuilder.h in Headers */, 53A79D5A29CE23F700E7489F /* MPURLRequestBuilder.h in Headers */, @@ -1777,6 +1771,7 @@ 53A79BF829CDFB2000E7489F /* MPNotificationController.m in Sources */, 53A79C0829CDFB2100E7489F /* MPEvent.m in Sources */, 53A79B6729CDFB2000E7489F /* MPAliasResponse.m in Sources */, + 534C11B72D08F53D00466F71 /* MPUserAttributeChange.swift in Sources */, 53A79C1229CDFB2100E7489F /* MPKitFilter.m in Sources */, 53A79B7829CDFB2000E7489F /* MPURLConnectionAssociate.m in Sources */, 53A79C0529CDFB2100E7489F /* MPTransactionAttributes.m in Sources */, @@ -1791,7 +1786,6 @@ 53A79C0E29CDFB2100E7489F /* MPKitContainer.mm in Sources */, 53A79BD729CDFB2000E7489F /* MPUploadBuilder.m in Sources */, 53A79C1129CDFB2100E7489F /* MPBaseProjection.m in Sources */, - 53A79BEC29CDFB2000E7489F /* MPUserAttributeChange.m in Sources */, 53A79B7029CDFB2000E7489F /* MPIdentityApiManager.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1947,6 +1941,7 @@ 53A79DA929CE23F700E7489F /* MPAppNotificationHandler.m in Sources */, 53A79DAA29CE23F700E7489F /* MPNotificationController.m in Sources */, 53A79DAB29CE23F700E7489F /* MPEvent.m in Sources */, + 534C11B62D08F53D00466F71 /* MPUserAttributeChange.swift in Sources */, 53A79DAD29CE23F700E7489F /* MPAliasResponse.m in Sources */, 53A79DAE29CE23F700E7489F /* MPKitFilter.m in Sources */, 53A79DAF29CE23F700E7489F /* MPURLConnectionAssociate.m in Sources */, @@ -1961,7 +1956,6 @@ 53A79DB929CE23F700E7489F /* MPKitContainer.mm in Sources */, 53A79DBA29CE23F700E7489F /* MPUploadBuilder.m in Sources */, 53A79DBC29CE23F700E7489F /* MPBaseProjection.m in Sources */, - 53A79DBE29CE23F700E7489F /* MPUserAttributeChange.m in Sources */, 53A79DBF29CE23F700E7489F /* MPIdentityApiManager.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/mParticle-Apple-SDK/Include/mParticle.h b/mParticle-Apple-SDK/Include/mParticle.h index ac80c58f..5fda36b8 100644 --- a/mParticle-Apple-SDK/Include/mParticle.h +++ b/mParticle-Apple-SDK/Include/mParticle.h @@ -40,6 +40,7 @@ NS_ASSUME_NONNULL_BEGIN @class MPSideloadedKit; +@class MPKitContainer; /** An SDK session. diff --git a/mParticle-Apple-SDK/MPBackendController.m b/mParticle-Apple-SDK/MPBackendController.m index 21202d79..6ffea3e4 100644 --- a/mParticle-Apple-SDK/MPBackendController.m +++ b/mParticle-Apple-SDK/MPBackendController.m @@ -21,7 +21,6 @@ #import "MPCommerceEvent.h" #import "MPCommerceEvent+Dictionary.h" #import "MPKitContainer.h" -#import "MPUserAttributeChange.h" #import "MPURLRequestBuilder.h" #import "MPListenerController.h" #import "MParticleWebView.h" diff --git a/mParticle-Apple-SDK/Utils/MPMessageBuilder.m b/mParticle-Apple-SDK/Utils/MPMessageBuilder.m index a00572d6..eec2c262 100644 --- a/mParticle-Apple-SDK/Utils/MPMessageBuilder.m +++ b/mParticle-Apple-SDK/Utils/MPMessageBuilder.m @@ -8,7 +8,6 @@ #import "MPCommerceEvent+Dictionary.h" #import "MPILogger.h" #import "NSDictionary+MPCaseInsensitive.h" -#import "MPUserAttributeChange.h" #import "MPPersistenceController.h" #import "MPApplication.h" #import "mParticle.h" diff --git a/mParticle-Apple-SDK/Utils/MPUserAttributeChange.h b/mParticle-Apple-SDK/Utils/MPUserAttributeChange.h deleted file mode 100644 index 340088e6..00000000 --- a/mParticle-Apple-SDK/Utils/MPUserAttributeChange.h +++ /dev/null @@ -1,16 +0,0 @@ -#import - -@interface MPUserAttributeChange : NSObject - -@property (nonatomic, strong, nonnull) NSString *key; -@property (nonatomic, strong, nullable) NSDate *timestamp; -@property (nonatomic, strong, nullable) NSDictionary *userAttributes; -@property (nonatomic, strong, nullable) id value; -@property (nonatomic, strong, nullable) id valueToLog; -@property (nonatomic, readonly) BOOL changed; -@property (nonatomic) BOOL deleted; -@property (nonatomic) BOOL isArray; - -- (nullable instancetype)initWithUserAttributes:(nullable NSDictionary *)userAttributes key:(nonnull NSString *)key value:(nullable id)value; - -@end diff --git a/mParticle-Apple-SDK/Utils/MPUserAttributeChange.m b/mParticle-Apple-SDK/Utils/MPUserAttributeChange.m deleted file mode 100644 index 06907b0e..00000000 --- a/mParticle-Apple-SDK/Utils/MPUserAttributeChange.m +++ /dev/null @@ -1,62 +0,0 @@ -#import "MPUserAttributeChange.h" -#import "MPIConstants.h" - -@implementation MPUserAttributeChange - -@synthesize valueToLog = _valueToLog; - -- (nullable instancetype)initWithUserAttributes:(nullable NSDictionary *)userAttributes key:(nonnull NSString *)key value:(nullable id)value { - Class NSStringClass = [NSString class]; - Class NSArrayClass = [NSArray class]; - BOOL validKey = !MPIsNull(key) && [key isKindOfClass:NSStringClass]; - BOOL isValueAnArray = [value isKindOfClass:NSArrayClass]; - - NSAssert(validKey, @"'key' must be a string."); - NSAssert(value == nil || (value != nil && ([value isKindOfClass:NSStringClass] || [value isKindOfClass:[NSNumber class]] || isValueAnArray) || (NSNull *)value == [NSNull null]), @"'value' must be either nil, string, number, or array of strings."); - - if (!validKey || (!userAttributes && !value)) { - return nil; - } - - self = [super init]; - if (self) { - _userAttributes = userAttributes; - _key = key; - _value = value; - _changed = YES; - _deleted = NO; - - id existingValue = userAttributes[key]; - if (existingValue) { - _isArray = [existingValue isKindOfClass:NSArrayClass] || isValueAnArray; - - BOOL isExistingValueNull = (NSNull *)existingValue == [NSNull null]; - BOOL isNewValueNull = (NSNull *)value == [NSNull null]; - if (isNewValueNull) { - _changed = !isExistingValueNull; - } else if (value) { - _changed = isExistingValueNull || ![existingValue isEqual:value]; - } else { - _changed = YES; - } - } else { - _isArray = isValueAnArray; - } - } - - return self; -} - -- (id)valueToLog { - if (!_valueToLog) { - _valueToLog = _value && !_deleted ? _value : [NSNull null]; - } - - return _valueToLog; -} - -- (void)setValueToLog:(id)valueToLog { - _valueToLog = valueToLog ? valueToLog : [NSNull null]; -} - -@end diff --git a/mParticle-Apple-SDK/Utils/MPUserAttributeChange.swift b/mParticle-Apple-SDK/Utils/MPUserAttributeChange.swift new file mode 100644 index 00000000..601b1e5c --- /dev/null +++ b/mParticle-Apple-SDK/Utils/MPUserAttributeChange.swift @@ -0,0 +1,48 @@ +// +// MPUserAttributeChange.swift +// mParticle-Apple-SDK +// +// Created by Brandon Stalnaker on 12/9/24. +// + +import Foundation + +@objc public class MPUserAttributeChange : NSObject { + + @objc public var key: String + @objc public var timestamp: Date? + @objc public var userAttributes: [String : Any]? + @objc public var value: Any? + @objc public var valueToLog: Any? + @objc public private(set) var changed: Bool + @objc public var deleted: Bool + @objc public var isArray: Bool + + @objc public init?(userAttributes: [String : Any]? = nil, key: String, value: Any?) { + guard value == nil || value is [Any] || value is NSNull || value is String || value is NSNumber else { + return nil + } + + if userAttributes == nil && value == nil { + return nil + } + + self.key = key + self.value = value + self.deleted = false + self.valueToLog = value + self.userAttributes = userAttributes + + let existingValue = userAttributes?[key] + self.isArray = (value is [Any] || existingValue is [Any]) + self.changed = !equals(existingValue, value) + super.init() + } +} + +fileprivate func equals(_ x : Any?, _ y : Any?) -> Bool { + guard let x = x as? AnyHashable, let y = y as? AnyHashable else { + return false + } + return x == y +}