Skip to content

Commit 816968f

Browse files
authored
feat(inbox): Callback for when Inbox was synced via forceContentUpdate LP-6294 (#89)
1 parent 1753345 commit 816968f

File tree

8 files changed

+149
-4
lines changed

8 files changed

+149
-4
lines changed

Example/Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191
9CB691DE1F15FFF5002D83D8 /* batch_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CB691DD1F15FFF5002D83D8 /* batch_response.json */; };
9292
9CB691DF1F15FFFF002D83D8 /* batch_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CB691DD1F15FFF5002D83D8 /* batch_response.json */; };
9393
9CB691E11F1601BA002D83D8 /* EventCallbackManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CB691E01F1601BA002D83D8 /* EventCallbackManagerTest.m */; };
94-
B08587C9AF0FCE5835E853EA /* Pods_Leanplum_SDK_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7849230A3D5C6DE5929ED1C8 /* Pods_Leanplum_SDK_Tests.framework */; };
94+
9CD04D941F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */; };
95+
9CD04D951F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */ = {isa = PBXBuildFile; fileRef = 9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */; };
9596
B5CCCA2D1D74B64D005ADEE1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA2C1D74B64D005ADEE1 /* main.m */; };
9697
B5CCCA301D74B64D005ADEE1 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA2F1D74B64D005ADEE1 /* AppDelegate.m */; };
9798
B5CCCA331D74B64D005ADEE1 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B5CCCA321D74B64D005ADEE1 /* ViewController.m */; };
@@ -192,7 +193,7 @@
192193
873B8AEA1B1F5CCA007FD442 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
193194
9CB691DD1F15FFF5002D83D8 /* batch_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = batch_response.json; sourceTree = "<group>"; };
194195
9CB691E01F1601BA002D83D8 /* EventCallbackManagerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EventCallbackManagerTest.m; sourceTree = "<group>"; };
195-
9EE2D840C30124E373ADF721 /* Pods_Leanplum_tvOS_SDK_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Leanplum_tvOS_SDK_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
196+
9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = variables_with_newsfeed_response.json; sourceTree = "<group>"; };
196197
A54ACCFCB36AE8C874708A7F /* libPods-Leanplum-SDK_Example-Leanplum-SDK_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Leanplum-SDK_Example-Leanplum-SDK_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
197198
B5CCCA291D74B64D005ADEE1 /* Leanplum-tvOS-SDK_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Leanplum-tvOS-SDK_Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
198199
B5CCCA2C1D74B64D005ADEE1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -325,6 +326,7 @@
325326
07E5C89C1F052B7800A4B092 /* state_response.json */,
326327
07E5C89E1F052B7800A4B092 /* track_event_response.json */,
327328
07E5C89F1F052B7800A4B092 /* variables_response.json */,
329+
9CD04D931F8EB6190033AB4A /* variables_with_newsfeed_response.json */,
328330
);
329331
path = Responses;
330332
sourceTree = "<group>";
@@ -605,6 +607,7 @@
605607
07E5C8DE1F052DC400A4B092 /* newsfeed_response.json in Resources */,
606608
07E5C8F61F052DD000A4B092 /* TiedPriorities2.json in Resources */,
607609
07E5C8DF1F052DC400A4B092 /* registration_response.json in Resources */,
610+
9CD04D951F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */,
608611
9CB691DF1F15FFFF002D83D8 /* batch_response.json in Resources */,
609612
07E5C8EA1F052DCC00A4B092 /* MainAppIcon.png in Resources */,
610613
07E5C8E21F052DC400A4B092 /* start_variables_response.json in Resources */,
@@ -649,6 +652,7 @@
649652
07E5C8D61F052B7800A4B092 /* regionData.json in Resources */,
650653
07E5C8D81F052B7800A4B092 /* SingleMessage.json in Resources */,
651654
07E5C8CB1F052B7800A4B092 /* simple_start_response.json in Resources */,
655+
9CD04D941F8EE7E30033AB4A /* variables_with_newsfeed_response.json in Resources */,
652656
07E5C8C01F052B7800A4B092 /* MainAppIcon.png in Resources */,
653657
07E5C8C11F052B7800A4B092 /* test.file in Resources */,
654658
07E5C8CF1F052B7800A4B092 /* track_event_response.json in Resources */,

Example/Tests/Classes/MessagesTest.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ - (void)runInAppMessagePrioritizationTest:(NSDictionary *)messageConfigs
134134
withFilter:self.mockFilter
135135
fromMessageId:self.mockFromMessageId
136136
withContextualValues:self.mockContextualValues];
137-
138-
NSLog(@"IDS: %@", calledMessageIds);
139137

140138
XCTAssertTrue([calledMessageIds isEqualToSet:expectedMessageIds]);
141139
}

Example/Tests/Classes/NewsfeedTest.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,73 @@ - (void)test_disableFetching
374374
XCTAssertTrue(timedOut == 0);
375375
}
376376

377+
- (void)test_onForceContentUpdate {
378+
XCTAssertTrue([LeanplumHelper start_development_test]);
379+
380+
// FCU without sync
381+
id<OHHTTPStubsDescriptor> stub = [OHHTTPStubs stubRequestsPassingTest:
382+
^BOOL(NSURLRequest * _Nonnull request) {
383+
return [request.URL.host isEqualToString:API_HOST];
384+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest *request) {
385+
NSString *response_file = OHPathForFile(@"variables_response.json",
386+
self.class);
387+
return [OHHTTPStubsResponse responseWithFileAtPath:response_file
388+
statusCode:200
389+
headers:@{@"Content-Type":@"application/json"}];
390+
}];
391+
392+
XCTestExpectation *responseExpectation1 = [self expectationWithDescription:@"response1"];
393+
[[Leanplum inbox] onForceContentUpdate:^void(BOOL success){
394+
XCTAssertTrue(success);
395+
[responseExpectation1 fulfill];
396+
}];
397+
398+
[Leanplum forceContentUpdate:nil];
399+
[self waitForExpectationsWithTimeout:2 handler:nil];
400+
[OHHTTPStubs removeStub:stub];
401+
[[Leanplum inbox] reset];
402+
403+
// FCU with sync
404+
stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
405+
return [request.URL.host isEqualToString:API_HOST];
406+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest *request) {
407+
NSString *response_file = OHPathForFile(@"variables_with_newsfeed_response.json",
408+
self.class);
409+
return [OHHTTPStubsResponse responseWithFileAtPath:response_file
410+
statusCode:200
411+
headers:@{@"Content-Type":@"application/json"}];
412+
}];
413+
414+
XCTestExpectation *responseExpectation2 = [self expectationWithDescription:@"response2"];
415+
[[Leanplum inbox] onForceContentUpdate:^void(BOOL success){
416+
XCTAssertTrue(success);
417+
[responseExpectation2 fulfill];
418+
}];
419+
420+
[Leanplum forceContentUpdate:nil];
421+
[self waitForExpectationsWithTimeout:2 handler:nil];
422+
[OHHTTPStubs removeStub:stub];
423+
[[Leanplum inbox] reset];
424+
425+
// FCU without internet connection
426+
stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
427+
return [request.URL.host isEqualToString:API_HOST];;
428+
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
429+
NSError *error = [NSError errorWithDomain:NSURLErrorDomain
430+
code:NSURLErrorBadServerResponse userInfo:nil];
431+
return [OHHTTPStubsResponse responseWithError:error];
432+
}];
433+
434+
XCTestExpectation *responseExpectation3 = [self expectationWithDescription:@"response3"];
435+
[[Leanplum inbox] onForceContentUpdate:^void(BOOL success){
436+
XCTAssertFalse(success);
437+
[responseExpectation3 fulfill];
438+
}];
439+
440+
[Leanplum forceContentUpdate:nil];
441+
[self waitForExpectationsWithTimeout:2 handler:nil];
442+
[OHHTTPStubs removeStub:stub];
443+
[[Leanplum inbox] reset];
444+
}
445+
377446
@end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"response": [{
3+
"success": true
4+
}, {
5+
"success": true
6+
}, {
7+
"vars": {
8+
9+
},
10+
"interfaceRules": [
11+
12+
],
13+
"variants": [
14+
15+
],
16+
"regions": {
17+
18+
},
19+
"syncNewsfeed": true,
20+
"success": true,
21+
"messages": {
22+
23+
},
24+
"interfaceEvents": [
25+
26+
]
27+
}]
28+
}

Leanplum-SDK/Classes/LPInbox.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
* This block is used when you define a callback.
9797
*/
9898
typedef void (^LeanplumInboxChangedBlock)(void);
99+
typedef void (^LeanplumInboxSyncedBlock)(BOOL success);
99100

100101
@interface LPInbox : NSObject
101102

@@ -150,6 +151,13 @@ typedef void (^LeanplumInboxChangedBlock)(void);
150151
*/
151152
- (void)onChanged:(LeanplumInboxChangedBlock)block;
152153

154+
/**
155+
* Block to call when forceContentUpdate was called.
156+
* Returns true if syncing was successful.
157+
* Note: use onChanged: for UI.
158+
*/
159+
- (void)onForceContentUpdate:(LeanplumInboxSyncedBlock)block;
160+
153161
/**
154162
@{
155163
* Adds a responder to be executed when an event happens.

Leanplum-SDK/Classes/LPInbox.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ - (void)reset
386386
_didLoad = NO;
387387
_inboxChangedBlocks = nil;
388388
_inboxChangedResponders = nil;
389+
_inboxSyncedBlocks = nil;
389390
updatingLock = [[NSObject alloc] init];
390391
_downloadedImageUrls = [NSMutableSet new];
391392
}
@@ -403,6 +404,15 @@ - (void)triggerInboxChanged
403404
LP_END_USER_CODE
404405
}
405406

407+
- (void)triggerInboxSyncedWithStatus:(BOOL)success
408+
{
409+
LP_BEGIN_USER_CODE
410+
for (LeanplumInboxSyncedBlock block in _inboxSyncedBlocks.copy) {
411+
block(success);
412+
}
413+
LP_END_USER_CODE
414+
}
415+
406416
#pragma mark - LPInbox methods
407417

408418
- (void)downloadMessages
@@ -450,13 +460,18 @@ - (void)downloadMessages
450460
[Leanplum onceVariablesChangedAndNoDownloadsPending:^{
451461
LP_END_USER_CODE
452462
[self updateMessages:messages unreadCount:unreadCount];
463+
[self triggerInboxSyncedWithStatus:YES];
453464
LP_BEGIN_USER_CODE
454465
}];
455466
} else {
456467
[self updateMessages:messages unreadCount:unreadCount];
468+
[self triggerInboxSyncedWithStatus:YES];
457469
}
458470
LP_END_TRY
459471
}];
472+
[req onError:^(NSError *error) {
473+
[self triggerInboxSyncedWithStatus:NO];
474+
}];
460475
[req sendIfConnected];
461476
LP_END_TRY
462477
}
@@ -521,6 +536,10 @@ - (LPInboxMessage *)messageForId:(NSString *)messageId
521536

522537
- (void)onChanged:(LeanplumInboxChangedBlock)block
523538
{
539+
if (!block) {
540+
return;
541+
}
542+
524543
LP_TRY
525544
if (!_inboxChangedBlocks) {
526545
_inboxChangedBlocks = [NSMutableArray array];
@@ -532,6 +551,20 @@ - (void)onChanged:(LeanplumInboxChangedBlock)block
532551
}
533552
}
534553

554+
- (void)onForceContentUpdate:(LeanplumInboxSyncedBlock)block
555+
{
556+
if (!block) {
557+
return;
558+
}
559+
560+
LP_TRY
561+
if (!_inboxSyncedBlocks) {
562+
_inboxSyncedBlocks = [NSMutableArray array];
563+
}
564+
[_inboxSyncedBlocks addObject:[block copy]];
565+
LP_END_TRY
566+
}
567+
535568
- (void)addInboxChangedResponder:(id)responder withSelector:(SEL)selector
536569
{
537570
if (!_inboxChangedResponders) {

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,8 @@ + (void)forceContentUpdate:(LeanplumVariablesChangedBlock)block
22592259
}
22602260
if ([response[LP_KEY_SYNC_INBOX] boolValue]) {
22612261
[[self inbox] downloadMessages];
2262+
} else {
2263+
[[self inbox] triggerInboxSyncedWithStatus:YES];
22622264
}
22632265
LP_END_TRY
22642266
if (block) {
@@ -2269,6 +2271,7 @@ + (void)forceContentUpdate:(LeanplumVariablesChangedBlock)block
22692271
if (block) {
22702272
block();
22712273
}
2274+
[[self inbox] triggerInboxSyncedWithStatus:NO];
22722275
}];
22732276
[req sendIfConnected];
22742277
LP_END_TRY

Leanplum-SDK/Classes/LeanplumInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ typedef void (^LeanplumInboxCacheUpdateBlock)(void);
164164
@property(strong, nonatomic) NSMutableDictionary *messages;
165165
@property(strong, nonatomic) NSMutableArray *inboxChangedBlocks;
166166
@property(strong, nonatomic) NSMutableSet *inboxChangedResponders;
167+
@property(strong, nonatomic) NSMutableArray *inboxSyncedBlocks;
167168
@property(strong, nonatomic) NSMutableSet *downloadedImageUrls;
168169

169170
#pragma mark - LPInbox method declaration
@@ -178,6 +179,7 @@ typedef void (^LeanplumInboxCacheUpdateBlock)(void);
178179
- (void)removeMessageForId:(NSString *)messageId;
179180
- (void)reset;
180181
- (void)triggerInboxChanged;
182+
- (void)triggerInboxSyncedWithStatus:(BOOL)success;
181183

182184
@end
183185

0 commit comments

Comments
 (0)