Skip to content

Commit 2add79c

Browse files
committed
Fix UserDefault UniqueOutcome decodable clases migration
* Add OSUniqueOutcomeNotification migration to OSCachedUniqueOutcome
1 parent 709f059 commit 2add79c

File tree

6 files changed

+207
-3
lines changed

6 files changed

+207
-3
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
7A880F322404AE7B0081F5E8 /* OSInAppMessagePushPrompt.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A880F302404AE7B0081F5E8 /* OSInAppMessagePushPrompt.m */; };
100100
7A880F332404AE7B0081F5E8 /* OSInAppMessagePushPrompt.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A880F302404AE7B0081F5E8 /* OSInAppMessagePushPrompt.m */; };
101101
7A9173A2231971E5007848FA /* OneSignalReceiveReceiptsController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A9173A1231971E5007848FA /* OneSignalReceiveReceiptsController.m */; };
102+
7A94D8E1249ABF0000E90B40 /* OSUniqueOutcomeNotification.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A94D8E0249ABF0000E90B40 /* OSUniqueOutcomeNotification.m */; };
102103
7AA2848A2406FC6400C25D76 /* OSInAppMessageTag.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1F2D8E2406EFC5007799A9 /* OSInAppMessageTag.m */; };
103104
7AA2848B2406FC6500C25D76 /* OSInAppMessageTag.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A1F2D8E2406EFC5007799A9 /* OSInAppMessageTag.m */; };
104105
7AAA60662485D0310004FADE /* OSMigrationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 7AAA60652485D0090004FADE /* OSMigrationController.h */; };
@@ -497,6 +498,8 @@
497498
7A880F302404AE7B0081F5E8 /* OSInAppMessagePushPrompt.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessagePushPrompt.m; sourceTree = "<group>"; };
498499
7A9173A1231971E5007848FA /* OneSignalReceiveReceiptsController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalReceiveReceiptsController.m; sourceTree = "<group>"; };
499500
7A9173A3231971F8007848FA /* OneSignalReceiveReceiptsController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalReceiveReceiptsController.h; sourceTree = "<group>"; };
501+
7A94D8E0249ABF0000E90B40 /* OSUniqueOutcomeNotification.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSUniqueOutcomeNotification.m; sourceTree = "<group>"; };
502+
7A94D8E2249ABF0C00E90B40 /* OSUniqueOutcomeNotification.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSUniqueOutcomeNotification.h; sourceTree = "<group>"; };
500503
7AAA60652485D0090004FADE /* OSMigrationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSMigrationController.h; sourceTree = "<group>"; };
501504
7AAA60672485D0420004FADE /* OSMigrationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSMigrationController.m; sourceTree = "<group>"; };
502505
7ABAF9D02457C3570074DFA0 /* CommonAsserts.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CommonAsserts.h; sourceTree = "<group>"; };
@@ -831,6 +834,8 @@
831834
children = (
832835
7A5A8186248990DA002E07C8 /* OSIndirectNotification.h */,
833836
7A5A8184248990CD002E07C8 /* OSIndirectNotification.m */,
837+
7A94D8E2249ABF0C00E90B40 /* OSUniqueOutcomeNotification.h */,
838+
7A94D8E0249ABF0000E90B40 /* OSUniqueOutcomeNotification.m */,
834839
);
835840
path = Models;
836841
sourceTree = "<group>";
@@ -1642,6 +1647,7 @@
16421647
isa = PBXSourcesBuildPhase;
16431648
buildActionMask = 2147483647;
16441649
files = (
1650+
7A94D8E1249ABF0000E90B40 /* OSUniqueOutcomeNotification.m in Sources */,
16451651
7A1232B2235E1BE6002B6CE3 /* OneSignalUserDefaults.m in Sources */,
16461652
7AF8FDBD2332A5C200A19245 /* OSIndirectInfluence.m in Sources */,
16471653
7A880F2D23FB45FB0081F5E8 /* OSInAppMessageOutcome.m in Sources */,

iOS_SDK/OneSignalSDK/Source/OSMigrationController.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ of this software and associated documentation files (the "Software"), to deal
2828
#import <Foundation/Foundation.h>
2929
#import "OSMigrationController.h"
3030
#import "OSInfluenceDataRepository.h"
31+
#import "OSOutcomeEventsCache.h"
3132
#import "OSIndirectInfluence.h"
33+
#import "OSCachedUniqueOutcome.h"
3234
#import "OneSignal.h"
3335
#import "OneSignalUserDefaults.h"
3436
#import "OneSignalCommonDefines.h"
3537
#import "OneSignalHelper.h"
3638

3739
@interface OneSignal ()
3840
+ (OSInfluenceDataRepository *)influenceDataRepository;
41+
+ (OSOutcomeEventsCache *)outcomeEventsCache;
3942
@end
4043

4144
@implementation OSMigrationController
@@ -50,15 +53,25 @@ - (void)migrate {
5053
*/
5154
- (void)migrateToVersion_02_14_00_AndGreater {
5255
let influenceVersion = 21400;
56+
let uniqueCacheOutcomeVersion = 21403;
5357
let sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
5458
if (sdkVersion < influenceVersion) {
55-
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating from version : %d", sdkVersion]];
59+
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSIndirectNotification from version: %d", sdkVersion]];
5660

5761
[NSKeyedUnarchiver setClass:[OSIndirectInfluence class] forClassName:@"OSIndirectNotification"];
5862
NSArray<OSIndirectInfluence *> * indirectInfluenceData = [[OneSignal influenceDataRepository] lastNotificationsReceivedData];
5963
if (indirectInfluenceData)
6064
[[OneSignal influenceDataRepository] saveNotifications:indirectInfluenceData];
6165
}
66+
67+
if (sdkVersion < uniqueCacheOutcomeVersion) {
68+
[OneSignal onesignal_Log:ONE_S_LL_DEBUG message:[NSString stringWithFormat:@"Migrating OSUniqueOutcomeNotification from version: %d", sdkVersion]];
69+
70+
[NSKeyedUnarchiver setClass:[OSCachedUniqueOutcome class] forClassName:@"OSUniqueOutcomeNotification"];
71+
NSArray<OSCachedUniqueOutcome *> * attributedCacheUniqueOutcomeEvents = [[OneSignal outcomeEventsCache] getAttributedUniqueOutcomeEventSent];
72+
if (attributedCacheUniqueOutcomeEvents)
73+
[[OneSignal outcomeEventsCache] saveAttributedUniqueOutcomeEventNotificationIds:attributedCacheUniqueOutcomeEvents];
74+
}
6275
}
6376

6477
- (void)saveCurrentSDKVersion {

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ + (OSSessionManager*)sessionManager {
390390

391391
static OSOutcomeEventsCache *_outcomeEventsCache;
392392
+ (OSOutcomeEventsCache *)outcomeEventsCache {
393+
if (!_outcomeEventsCache)
394+
_outcomeEventsCache = [[OSOutcomeEventsCache alloc] init];
393395
return _outcomeEventsCache;
394396
}
395397

@@ -588,8 +590,7 @@ + (id)initWithLaunchOptions:(NSDictionary*)launchOptions
588590
initializationTime = [NSDate date];
589591

590592
// Outcomes init
591-
_outcomeEventsCache = [[OSOutcomeEventsCache alloc] init];
592-
_outcomeEventFactory = [[OSOutcomeEventsFactory alloc] initWithCache:_outcomeEventsCache];
593+
_outcomeEventFactory = [[OSOutcomeEventsFactory alloc] initWithCache:OneSignal.outcomeEventsCache];
593594
_outcomeEventsController = [[OneSignalOutcomeEventsController alloc] initWithSessionManager:OneSignal.sessionManager outcomeEventsFactory:_outcomeEventFactory];
594595

595596
if (appId && mShareLocation)

iOS_SDK/OneSignalSDK/UnitTests/MigrationTests.m

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
#import <XCTest/XCTest.h>
2929
#import "OSMigrationController.h"
3030
#import "OSInfluenceDataRepository.h"
31+
#import "OSOutcomeEventsCache.h"
3132
#import "OSIndirectInfluence.h"
3233
#import "OSIndirectNotification.h"
34+
#import "OSCachedUniqueOutcome.h"
35+
#import "OSUniqueOutcomeNotification.h"
3336
#import "OneSignalHelper.h"
3437
#import "UnitTestCommonMethods.h"
3538
#import "OneSignalUserDefaults.h"
@@ -42,13 +45,15 @@ @interface MigrationTests : XCTestCase
4245
@implementation MigrationTests {
4346
OSMigrationController *migrationController;
4447
OSInfluenceDataRepository *dataRepository;
48+
OSOutcomeEventsCache *outcomesCache;
4549
}
4650

4751
- (void)setUp {
4852
[super setUp];
4953
[UnitTestCommonMethods beforeEachTest:self];
5054
migrationController = [OSMigrationController new];
5155
dataRepository = [OSInfluenceDataRepository new];
56+
outcomesCache = [OSOutcomeEventsCache new];
5257
}
5358

5459
- (void)testNoIndirectInfluenceDataAvailableToMigrate {
@@ -120,4 +125,76 @@ - (void)testIndirectInfluenceToIndirectInfluenceMigration {
120125
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
121126
}
122127

128+
- (void)testNoAttributedUniqueOutcomeDataAvailableToMigrate {
129+
NSArray *uniqueOutcomes = [outcomesCache getAttributedUniqueOutcomeEventSent];
130+
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
131+
XCTAssertNil(uniqueOutcomes);
132+
XCTAssertEqual(0, sdkVersion);
133+
[migrationController migrate];
134+
135+
NSArray *uniqueOutcomesAfterMigration = [outcomesCache getAttributedUniqueOutcomeEventSent];
136+
XCTAssertNil(uniqueOutcomesAfterMigration);
137+
138+
NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
139+
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
140+
}
141+
142+
- (void)testUniqueOutcomeNotificationToCacheUniqueOutcomeMigration {
143+
NSString *notificationId = @"1234-5678-1234-5678-1234";
144+
NSString *outcome = @"test";
145+
NSNumber *timestamp = @(10);
146+
OSUniqueOutcomeNotification *uniqueOutcome = [[OSUniqueOutcomeNotification alloc] initWithParamsNotificationId:outcome notificationId:notificationId timestamp:timestamp];
147+
NSMutableArray *uniqueOutcomes = [NSMutableArray new];
148+
[uniqueOutcomes addObject:uniqueOutcome];
149+
150+
[outcomesCache saveAttributedUniqueOutcomeEventNotificationIds:uniqueOutcomes];
151+
152+
NSArray *lastUniqueOutcomeSaved = [outcomesCache getAttributedUniqueOutcomeEventSent];
153+
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
154+
XCTAssertEqual(1, [lastUniqueOutcomeSaved count]);
155+
XCTAssertEqual(0, sdkVersion);
156+
157+
[migrationController migrate];
158+
159+
NSArray *lastUniqueOutcomeAfterMigration = [outcomesCache getAttributedUniqueOutcomeEventSent];
160+
XCTAssertEqual(1, [lastUniqueOutcomeAfterMigration count]);
161+
162+
OSCachedUniqueOutcome *cachedUniqueOutcome = [lastUniqueOutcomeAfterMigration objectAtIndex:0];
163+
XCTAssertTrue([cachedUniqueOutcome.uniqueId isEqualToString:notificationId]);
164+
XCTAssertEqual(cachedUniqueOutcome.channel, NOTIFICATION);
165+
XCTAssertEqual([timestamp intValue], [cachedUniqueOutcome.timestamp intValue]);
166+
167+
NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
168+
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
169+
}
170+
171+
- (void)testCachedUniqueOutcomeToCachedUniqueOutcomeMigration {
172+
NSString *notificationId = @"1234-5678-1234-5678-1234";
173+
NSString *outcome = @"test";
174+
NSNumber *timestamp = @(10);
175+
OSCachedUniqueOutcome *cachedUniqueOutcome = [[OSCachedUniqueOutcome alloc] initWithParamsName:outcome uniqueId:notificationId timestamp:timestamp channel:NOTIFICATION];
176+
NSMutableArray *cacheUniqueOutcomes = [NSMutableArray new];
177+
[cacheUniqueOutcomes addObject:cachedUniqueOutcome];
178+
179+
[outcomesCache saveAttributedUniqueOutcomeEventNotificationIds:cacheUniqueOutcomes];
180+
NSArray *lastCachedUniqueOutcomes = [outcomesCache getAttributedUniqueOutcomeEventSent];
181+
NSInteger sdkVersion = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
182+
XCTAssertEqual(1, [lastCachedUniqueOutcomes count]);
183+
XCTAssertEqual(0, sdkVersion);
184+
185+
[migrationController migrate];
186+
187+
NSArray<OSCachedUniqueOutcome *> *lastCachedUniqueOutcomesAfterMigration = [outcomesCache getAttributedUniqueOutcomeEventSent];
188+
XCTAssertEqual(1, [lastCachedUniqueOutcomesAfterMigration count]);
189+
190+
OSCachedUniqueOutcome *cachedUniqueOutcomeAfterMigration = [lastCachedUniqueOutcomesAfterMigration objectAtIndex:0];
191+
XCTAssertTrue([cachedUniqueOutcomeAfterMigration.name isEqualToString:outcome]);
192+
XCTAssertTrue([cachedUniqueOutcomeAfterMigration.uniqueId isEqualToString:notificationId]);
193+
XCTAssertEqual(NOTIFICATION, cachedUniqueOutcomeAfterMigration.channel);
194+
XCTAssertEqual([timestamp intValue], [cachedUniqueOutcomeAfterMigration.timestamp intValue]);
195+
196+
NSInteger sdkVersionAfterMigration = [OneSignalUserDefaults.initShared getSavedIntegerForKey:OSUD_CACHED_SDK_VERSION defaultValue:0];
197+
XCTAssertEqual([[OneSignal sdk_version_raw] intValue], sdkVersionAfterMigration);
198+
}
199+
123200
@end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
Modified MIT License
3+
4+
Copyright 2019 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
@interface OSUniqueOutcomeNotification : NSObject
29+
30+
@property (nonatomic, readonly) NSString *name;
31+
@property (nonatomic, readonly) NSString *notificationId;
32+
@property (nonatomic, readonly) NSNumber *timestamp; // seconds
33+
34+
- (id)initWithParamsNotificationId:(NSString *)name notificationId:(NSString *)notificationId timestamp:(NSNumber *)timestamp;
35+
36+
@end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
Modified MIT License
3+
4+
Copyright 2019 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import "OSUniqueOutcomeNotification.h"
30+
31+
@implementation OSUniqueOutcomeNotification
32+
33+
- (id)initWithParamsNotificationId:(NSString *)name notificationId:(NSString *)notificationId timestamp:(NSNumber *)timestamp {
34+
_name = name;
35+
_notificationId = notificationId;
36+
_timestamp = timestamp;
37+
return self;
38+
}
39+
40+
- (void)encodeWithCoder:(NSCoder *)encoder {
41+
[encoder encodeObject:_name forKey:@"name"];
42+
[encoder encodeObject:_notificationId forKey:@"notificationId"];
43+
[encoder encodeInteger:[_timestamp integerValue] forKey:@"timestamp"];
44+
}
45+
46+
- (id)initWithCoder:(NSCoder *)decoder {
47+
if (self = [super init]) {
48+
_name = [decoder decodeObjectForKey:@"name"];
49+
_notificationId = [decoder decodeObjectForKey:@"notificationId"];
50+
_timestamp = [NSNumber numberWithLong:[decoder decodeIntegerForKey:@"timestamp"]];
51+
}
52+
return self;
53+
}
54+
55+
- (NSString *)description {
56+
return [NSString stringWithFormat:@"Name: %@ Notification Id: %@ Timestamp: %@", _name, _notificationId, _timestamp];
57+
}
58+
59+
- (BOOL)isEqual:(OSUniqueOutcomeNotification *)other {
60+
NSString *key = [NSString stringWithFormat:@"%@_%@", _name, _notificationId];
61+
NSString *otherKey = [NSString stringWithFormat:@"%@_%@", other.name, other.notificationId];
62+
return [key isEqualToString:otherKey];
63+
}
64+
65+
- (NSUInteger)hash {
66+
return [_notificationId hash];
67+
}
68+
69+
@end
70+
71+

0 commit comments

Comments
 (0)