From eff13de77199722a86fb0041a627761472dc0823 Mon Sep 17 00:00:00 2001 From: gia Date: Fri, 20 Jun 2014 15:25:57 +0700 Subject: [PATCH] dismissActiveNotificationWithCompletion will now call completion even when nothing is display along with a BOOL to show if it has dismissed a notification or not --- TSMessages/Classes/TSMessage.h | 10 ++++++--- TSMessages/Classes/TSMessage.m | 39 +++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/TSMessages/Classes/TSMessage.h b/TSMessages/Classes/TSMessage.h index dc1cd506..6551946c 100755 --- a/TSMessages/Classes/TSMessage.h +++ b/TSMessages/Classes/TSMessage.h @@ -140,6 +140,10 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) { atPosition:(TSMessageNotificationPosition)messagePosition canBeDismissedByUser:(BOOL)dismissingEnabled; +/** Return YES if there is a notification, and that notification is fully display (check by messageIsFullyDisplayed method) + */ ++ (BOOL)hasFullyDisplayedNotification; + /** Fades out the currently displayed notification. If another notification is in the queue, the next one will be displayed automatically @return YES if the currently displayed notification was successfully dismissed. NO if no notification @@ -148,11 +152,11 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) { + (BOOL)dismissActiveNotification; /** Fades out the currently displayed notification with a completion block after the animation has finished. If another notification is in the queue, - the next one will be displayed automatically + the next one will be displayed automatically. The completion block will be call even when there is nothing to dismiss (didDismissNotification will be NO in this case) @return YES if the currently displayed notification was successfully dismissed. NO if no notification - was currently displayed. + was currently fully displayed. */ -+ (BOOL)dismissActiveNotificationWithCompletion:(void (^)())completion; ++ (BOOL)dismissActiveNotificationWithCompletion:(void (^)(BOOL didDismissNotification))completion; /** Use this method to set a default view controller to display the messages in */ + (void)setDefaultViewController:(UIViewController *)defaultViewController; diff --git a/TSMessages/Classes/TSMessage.m b/TSMessages/Classes/TSMessage.m index 59e6bfd6..702ce9db 100755 --- a/TSMessages/Classes/TSMessage.m +++ b/TSMessages/Classes/TSMessage.m @@ -362,25 +362,48 @@ - (void)fadeOutNotification:(TSMessageView *)currentView animationFinishedBlock: }]; } ++ (BOOL)hasFullyDisplayedNotification { + if ([[TSMessage sharedMessage].messages count] > 0) { + TSMessageView *currentMessage = [[TSMessage sharedMessage].messages objectAtIndex:0]; + if (currentMessage.messageIsFullyDisplayed) + { + return YES; + } + } + return NO; +} + + (BOOL)dismissActiveNotification { return [self dismissActiveNotificationWithCompletion:nil]; } -+ (BOOL)dismissActiveNotificationWithCompletion:(void (^)())completion ++ (BOOL)dismissActiveNotificationWithCompletion:(void (^)(BOOL didDismissNotification))completion { - if ([[TSMessage sharedMessage].messages count] == 0) return NO; - dispatch_async(dispatch_get_main_queue(), ^ { - if ([[TSMessage sharedMessage].messages count] == 0) return; - TSMessageView *currentMessage = [[TSMessage sharedMessage].messages objectAtIndex:0]; - if (currentMessage.messageIsFullyDisplayed) + if ([[TSMessage sharedMessage].messages count] > 0) { - [[TSMessage sharedMessage] fadeOutNotification:currentMessage animationFinishedBlock:completion]; + TSMessageView *currentMessage = [[TSMessage sharedMessage].messages objectAtIndex:0]; + if (currentMessage.messageIsFullyDisplayed) + { + [[TSMessage sharedMessage] fadeOutNotification:currentMessage animationFinishedBlock:^{ + if (completion) { + completion(YES); + } + }]; + }else { + if (completion) { + completion(NO); + } + } + }else { + if (completion) { + completion(NO); + } } }); - return YES; + return [self hasFullyDisplayedNotification]; } #pragma mark Customizing TSMessages