Call completionHandler in sendReply (#3926)

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
This commit is contained in:
Mattermost Build 2020-02-14 04:27:24 +01:00 committed by GitHub
parent 81292df787
commit 31e5e0426e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,13 +41,13 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
});
}
- (void)sendReply:(UNNotificationResponse *)response {
- (void)sendReply:(UNNotificationResponse *)response completionHandler:(void (^)(void))notificationCompletionHandler {
StoreManager *store = [StoreManager shared];
[store getEntities:true];
NSString *serverUrl = [store getServerUrl];
NSString *sessionToken = [store getToken];
if (serverUrl == nil || sessionToken == nil) {
[self handleReplyFailure:@""];
[self handleReplyFailure:@"" completionHandler:notificationCompletionHandler];
return;
}
@ -68,7 +68,7 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
if (!postData) {
[self handleReplyFailure:channelId];
[self handleReplyFailure:channelId completionHandler:notificationCompletionHandler];
return;
}
@ -85,24 +85,26 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode == 201) {
[self handleReplySuccess:completionKey];
[self handleReplySuccess:completionKey completionHandler:notificationCompletionHandler];
} else {
[self handleReplyFailure:channelId];
[self handleReplyFailure:channelId completionHandler:notificationCompletionHandler];
}
}];
[task resume];
}
- (void) handleReplySuccess:(NSString *)completionKey {
- (void) handleReplySuccess:(NSString *)completionKey completionHandler:(void (^)(void))completionHandler {
[[RNNotificationsStore sharedInstance] completeAction:completionKey];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[notifications count]];
}];
completionHandler();
}
- (void) handleReplyFailure:(NSString *)channelId {
- (void) handleReplyFailure:(NSString *)channelId completionHandler:(void (^)(void))completionHandler {
RNNotificationCenter *notificationCenter = [self notificationCenter];
if (!notificationCenter) {
notificationCenter = [RNNotificationCenter new];
@ -120,14 +122,15 @@ NSString *const ReplyActionID = @"REPLY_ACTION";
}
};
[notificationCenter sendLocalNotification:notification withId:id];
completionHandler();
}
#pragma mark - Method Swizzling
- (void)handleReplyAction_didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(void))completionHandler {
if ([response.actionIdentifier isEqualToString:ReplyActionID]) {
[self sendReply:response];
completionHandler();
[self sendReply:response completionHandler:completionHandler];
} else {
[self handleReplyAction_didReceiveNotificationResponse:response completionHandler:completionHandler];
}