Skip to content

Commit 3aac194

Browse files
authored
Download inbox messages (#416)
* exposing download inbox messages method * unit tests added
1 parent 3cf1f62 commit 3aac194

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

Example/Tests/Classes/LPInboxTest.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,63 @@ -(void)loadInbox:(void (^)(void))finishBlock
484484
[[Leanplum inbox] downloadMessages];
485485
}
486486

487+
- (void)testDownloadMessages
488+
{
489+
// Do not need to fetch images
490+
// Prefetching will trigger downloadFile requests
491+
[[Leanplum inbox] disableImagePrefetching];
492+
// Ensure errors are thrown
493+
[LeanplumHelper mockThrowErrorToThrow];
494+
495+
id<HTTPStubsDescriptor> getNewsfeedMessagesStub = [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
496+
return [request.URL.host isEqualToString:API_HOST];
497+
} withStubResponse:^HTTPStubsResponse * _Nonnull(NSURLRequest *request) {
498+
NSString *response_file = OHPathForFile(@"newsfeed_response.json", self.class);
499+
return [HTTPStubsResponse responseWithFileAtPath:response_file
500+
statusCode:200
501+
headers:@{@"Content-Type":@"application/json"}];
502+
}];
503+
504+
dispatch_semaphore_t semaphor = dispatch_semaphore_create(0);
505+
[[Leanplum inbox] onChanged:^{
506+
[HTTPStubs removeStub:getNewsfeedMessagesStub];
507+
dispatch_semaphore_signal(semaphor);
508+
}];
509+
510+
[LeanplumHelper setup_development_test];
511+
[[Leanplum inbox] downloadMessages];
512+
513+
long timedOut = dispatch_semaphore_wait(semaphor, [LeanplumHelper default_dispatch_time]);
514+
XCTAssertTrue(timedOut == 0);
515+
}
516+
517+
- (void)testDownloadMessagesWithCompletionHandler
518+
{
519+
// Do not need to fetch images
520+
// Prefetching will trigger downloadFile requests
521+
[[Leanplum inbox] disableImagePrefetching];
522+
// Ensure errors are thrown
523+
[LeanplumHelper mockThrowErrorToThrow];
524+
525+
id<HTTPStubsDescriptor> getNewsfeedMessagesStub = [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
526+
return [request.URL.host isEqualToString:API_HOST];
527+
} withStubResponse:^HTTPStubsResponse * _Nonnull(NSURLRequest *request) {
528+
NSString *response_file = OHPathForFile(@"newsfeed_response.json", self.class);
529+
return [HTTPStubsResponse responseWithFileAtPath:response_file
530+
statusCode:200
531+
headers:@{@"Content-Type":@"application/json"}];
532+
}];
533+
534+
dispatch_semaphore_t semaphor = dispatch_semaphore_create(0);
535+
536+
[LeanplumHelper setup_development_test];
537+
[[Leanplum inbox] downloadMessages:^(BOOL success) {
538+
[HTTPStubs removeStub:getNewsfeedMessagesStub];
539+
dispatch_semaphore_signal(semaphor);
540+
}];
541+
542+
long timedOut = dispatch_semaphore_wait(semaphor, [LeanplumHelper default_dispatch_time]);
543+
XCTAssertTrue(timedOut == 0);
544+
}
545+
487546
@end

Leanplum-SDK/Classes/Features/Inbox/LPInbox.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,28 @@ - (void)triggerInboxChanged
404404
LP_END_USER_CODE
405405
}
406406

407-
- (void)triggerInboxSyncedWithStatus:(BOOL)success
407+
- (void)triggerInboxSyncedWithStatus:(BOOL)success withCompletionHandler:(nullable LeanplumInboxSyncedBlock)completionHandler
408408
{
409409
LP_BEGIN_USER_CODE
410-
for (LeanplumInboxSyncedBlock block in _inboxSyncedBlocks.copy) {
411-
block(success);
410+
if (completionHandler != nil)
411+
{
412+
completionHandler(success);
413+
} else {
414+
for (LeanplumInboxSyncedBlock block in _inboxSyncedBlocks.copy) {
415+
block(success);
416+
}
412417
}
413418
LP_END_USER_CODE
414419
}
415420

416421
#pragma mark - LPInbox methods
417422

418423
- (void)downloadMessages
424+
{
425+
[self downloadMessages:nil];
426+
}
427+
428+
- (void)downloadMessages:(LeanplumInboxSyncedBlock)completionHandler
419429
{
420430
RETURN_IF_NOOP;
421431
LP_TRY
@@ -460,17 +470,17 @@ - (void)downloadMessages
460470
[Leanplum onceVariablesChangedAndNoDownloadsPending:^{
461471
LP_END_USER_CODE
462472
[self updateMessages:messages unreadCount:unreadCount];
463-
[self triggerInboxSyncedWithStatus:YES];
473+
[self triggerInboxSyncedWithStatus:YES withCompletionHandler:completionHandler];
464474
LP_BEGIN_USER_CODE
465475
}];
466476
} else {
467477
[self updateMessages:messages unreadCount:unreadCount];
468-
[self triggerInboxSyncedWithStatus:YES];
478+
[self triggerInboxSyncedWithStatus:YES withCompletionHandler:completionHandler];
469479
}
470480
LP_END_TRY
471481
}];
472482
[request onError:^(NSError *error) {
473-
[self triggerInboxSyncedWithStatus:NO];
483+
[self triggerInboxSyncedWithStatus:NO withCompletionHandler:completionHandler];
474484
}];
475485
[[LPRequestSender sharedInstance] send:request];
476486
LP_END_TRY

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ + (void)forceContentUpdate:(LeanplumVariablesChangedBlock)block
24252425
if ([response[LP_KEY_SYNC_INBOX] boolValue]) {
24262426
[[self inbox] downloadMessages];
24272427
} else {
2428-
[[self inbox] triggerInboxSyncedWithStatus:YES];
2428+
[[self inbox] triggerInboxSyncedWithStatus:YES withCompletionHandler:nil];
24292429
}
24302430
LP_END_TRY
24312431
if (block) {
@@ -2436,7 +2436,7 @@ + (void)forceContentUpdate:(LeanplumVariablesChangedBlock)block
24362436
if (block) {
24372437
block();
24382438
}
2439-
[[self inbox] triggerInboxSyncedWithStatus:NO];
2439+
[[self inbox] triggerInboxSyncedWithStatus:NO withCompletionHandler:nil];
24402440
}];
24412441
[[LPRequestSender sharedInstance] send:request];
24422442
LP_END_TRY

Leanplum-SDK/Classes/Internal/LeanplumInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ typedef void (^LeanplumInboxCacheUpdateBlock)(void);
137137
- (void)removeMessageForId:(NSString *)messageId;
138138
- (void)reset;
139139
- (void)triggerInboxChanged;
140-
- (void)triggerInboxSyncedWithStatus:(BOOL)success;
140+
- (void)triggerInboxSyncedWithStatus:(BOOL)success withCompletionHandler:(nullable LeanplumInboxSyncedBlock)completionHandler;
141141

142142
@end
143143

Leanplum-SDK/Classes/LPInbox.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ NS_SWIFT_NAME(onInboxChanged(completion:));
173173
*/
174174
- (void)onForceContentUpdate:(LeanplumInboxSyncedBlock)block;
175175

176+
/**
177+
Forces downloading of inbox messages from the server. After messages are downloaded the appropriate callbacks will fire.
178+
*/
179+
- (void)downloadMessages;
180+
/**
181+
Forces downloading of inbox messages from the server. After messages are downloaded the appropriate callbacks will fire.
182+
@param completionHandler The callback to invoke when messages are downloaded.
183+
*/
184+
- (void)downloadMessages:(nullable LeanplumInboxSyncedBlock)completionHandler;
176185
/**
177186
@{
178187
* Adds a responder to be executed when an event happens.

0 commit comments

Comments
 (0)