From dbb416451f4cee380bedd3d2d2572b72d0625291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Rekk=C3=A9?= Date: Fri, 7 Feb 2014 12:50:17 +0100 Subject: [PATCH 1/2] Add priority functionality - Messages are presented in order of their priority - This happens by sorting the messages list just before "fading in" a new message - If there are no differences in priority, the order stays the same --- TSMessages/Classes/TSMessage.h | 31 +++++++++++++++++++ TSMessages/Classes/TSMessage.m | 53 +++++++++++++++++++++++++++++++- TSMessages/Views/TSMessageView.h | 6 +++- TSMessages/Views/TSMessageView.m | 24 +++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/TSMessages/Classes/TSMessage.h b/TSMessages/Classes/TSMessage.h index b1525698..d0cc776b 100755 --- a/TSMessages/Classes/TSMessage.h +++ b/TSMessages/Classes/TSMessage.h @@ -48,6 +48,11 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) { TSMessageNotificationDurationEndless = -1 // The notification is displayed until the user dismissed it or it is dismissed by calling dismissActiveNotification }; +typedef NS_ENUM(NSInteger, TSMessageNotificationPriority) { + TSMessageNotificationPriorityNormal = 0, + TSMessageNotificationPriorityHighest = NSIntegerMax, + TSMessageNotificationPriorityLowest = NSIntegerMin +}; @interface TSMessage : NSObject @@ -108,6 +113,32 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) { atPosition:(TSMessageNotificationPosition)messagePosition canBeDismisedByUser:(BOOL)dismissingEnabled; +/** Shows a notification message in a specific view controller + @param viewController The view controller to show the notification in. + @param title The title of the notification view + @param subtitle The message that is displayed underneath the title (optional) + @param image A custom icon image (optional) + @param type The notification type (Message, Warning, Error, Success) + @param duration The duration of the notification being displayed + @param priority The priority of the notification being displayed (higher is more) + @param callback The block that should be executed, when the user tapped on the message + @param buttonTitle The title for button (optional) + @param buttonCallback The block that should be executed, when the user tapped on the button + @param messagePosition The position of the message on the screen + @param dismissingEnabled Should the message be dismissed when the user taps/swipes it + */ ++ (void)showNotificationInViewController:(UIViewController *)viewController + title:(NSString *)title + subtitle:(NSString *)subtitle + image:(UIImage *)image + type:(TSMessageNotificationType)type + duration:(NSTimeInterval)duration + priority:(NSInteger)priority + callback:(void (^)())callback + buttonTitle:(NSString *)buttonTitle + buttonCallback:(void (^)())buttonCallback + atPosition:(TSMessageNotificationPosition)messagePosition + canBeDismisedByUser:(BOOL)dismissingEnabled; /** Fades out the currently displayed notification. If another notification is in the queue, the next one will be displayed automatically diff --git a/TSMessages/Classes/TSMessage.m b/TSMessages/Classes/TSMessage.m index c64fb15d..1b9684ce 100755 --- a/TSMessages/Classes/TSMessage.m +++ b/TSMessages/Classes/TSMessage.m @@ -102,6 +102,7 @@ + (void)showNotificationInViewController:(UIViewController *)viewController image:image type:type duration:duration + priority:TSMessageNotificationPriorityNormal inViewController:viewController callback:callback buttonTitle:buttonTitle @@ -112,6 +113,35 @@ + (void)showNotificationInViewController:(UIViewController *)viewController } ++ (void)showNotificationInViewController:(UIViewController *)viewController + title:(NSString *)title + subtitle:(NSString *)subtitle + image:(UIImage *)image + type:(TSMessageNotificationType)type + duration:(NSTimeInterval)duration + priority:(NSInteger)priority + callback:(void (^)())callback + buttonTitle:(NSString *)buttonTitle + buttonCallback:(void (^)())buttonCallback + atPosition:(TSMessageNotificationPosition)messagePosition + canBeDismisedByUser:(BOOL)dismissingEnabled +{ + // Create the TSMessageView + TSMessageView *v = [[TSMessageView alloc] initWithTitle:title + subtitle:subtitle + image:image + type:type + duration:duration + priority:priority + inViewController:viewController + callback:callback + buttonTitle:buttonTitle + buttonCallback:buttonCallback + atPosition:messagePosition + shouldBeDismissed:dismissingEnabled]; + [self prepareNotificationToBeShown:v]; +} + + (void)prepareNotificationToBeShown:(TSMessageView *)messageView { NSString *title = messageView.title; @@ -121,6 +151,10 @@ + (void)prepareNotificationToBeShown:(TSMessageView *)messageView { if (([n.title isEqualToString:title] || (!n.title && !title)) && ([n.subtitle isEqualToString:subtitle] || (!n.subtitle && !subtitle))) { + if (n.priority < messageView.priority) + { + n.priority = messageView.priority; //new priority is higher, so raise the priority. + } return; // avoid showing the same messages twice in a row } } @@ -133,6 +167,21 @@ + (void)prepareNotificationToBeShown:(TSMessageView *)messageView } } ++(void)sortMessagesOnPriority: (NSMutableArray*) messages +{ + [[TSMessage sharedMessage].messages sortUsingComparator:^NSComparisonResult(TSMessageView* obj1, TSMessageView* obj2) { + if ( obj1.priority < obj2.priority ) + { + return NSOrderedDescending; + } + else if ( obj1.priority > obj2.priority ) + { + return NSOrderedAscending; + } + return NSOrderedSame; + }]; +} + #pragma mark Fading in/out the message view @@ -149,6 +198,8 @@ - (void)fadeInCurrentNotification { if ([self.messages count] == 0) return; + [TSMessage sortMessagesOnPriority:[TSMessage sharedMessage].messages]; + notificationActive = YES; TSMessageView *currentView = [self.messages objectAtIndex:0]; @@ -294,7 +345,7 @@ - (void)fadeOutNotification:(TSMessageView *)currentView if ([self.messages count] > 0) { - [self.messages removeObjectAtIndex:0]; + [self.messages removeObject:currentView]; } notificationActive = NO; diff --git a/TSMessages/Views/TSMessageView.h b/TSMessages/Views/TSMessageView.h index 3d1b889c..31029bc2 100755 --- a/TSMessages/Views/TSMessageView.h +++ b/TSMessages/Views/TSMessageView.h @@ -22,7 +22,7 @@ -@interface TSMessageView : UIView +@interface TSMessageView : UIView /** The displayed title of this message */ @property (nonatomic, readonly) NSString *title; @@ -36,6 +36,9 @@ /** The duration of the displayed message. If it is 0.0, it will automatically be calculated */ @property (nonatomic, assign) CGFloat duration; +/** The priority of the displayed message. Higher is more priority. */ +@property (nonatomic, assign) NSInteger priority; + /** The position of the message (top or bottom) */ @property (nonatomic, assign) TSMessageNotificationPosition messagePosition; @@ -63,6 +66,7 @@ image:(UIImage *)image type:(TSMessageNotificationType)notificationType duration:(CGFloat)duration + priority:(NSInteger)priority inViewController:(UIViewController *)viewController callback:(void (^)())callback buttonTitle:(NSString *)buttonTitle diff --git a/TSMessages/Views/TSMessageView.m b/TSMessages/Views/TSMessageView.m index 2da9ae87..328073bb 100755 --- a/TSMessages/Views/TSMessageView.m +++ b/TSMessages/Views/TSMessageView.m @@ -37,6 +37,8 @@ @interface TSMessageView () /** The view controller this message is displayed in */ @property (nonatomic, strong) UIViewController *viewController; +@property (nonatomic, assign) TSMessageNotificationType notificationType; +@property (nonatomic, assign) BOOL dismissAble; /** Internal properties needed to resize the view on device rotation properly */ @property (nonatomic, strong) UILabel *titleLabel; @@ -90,6 +92,7 @@ - (id)initWithTitle:(NSString *)title image:(UIImage *)image type:(TSMessageNotificationType)notificationType duration:(CGFloat)duration + priority:(NSInteger) priority inViewController:(UIViewController *)viewController callback:(void (^)())callback buttonTitle:(NSString *)buttonTitle @@ -105,8 +108,11 @@ - (id)initWithTitle:(NSString *)title _subtitle = subtitle; _buttonTitle = buttonTitle; _duration = duration; + _priority = priority; _viewController = viewController; _messagePosition = position; + _notificationType = notificationType; + _dismissAble = dismissAble; self.callback = callback; self.buttonCallback = buttonCallback; @@ -490,4 +496,22 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive return ! ([touch.view isKindOfClass:[UIControl class]]); } +#pragma mark - NSCopying + +- (id)copyWithZone:(NSZone *)zone +{ + return [[TSMessageView alloc] initWithTitle:self.title + subtitle:self.subtitle + image:self.iconImageView.image + type:self.notificationType + duration:self.duration + priority:self.priority + inViewController:self.viewController + callback:self.callback + buttonTitle:self.buttonTitle + buttonCallback:self.buttonCallback + atPosition:self.messagePosition + shouldBeDismissed:self.dismissAble ]; +} + @end From c0748ec312922e210693e2d2f1a1d177c6a54650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Rekk=C3=A9?= Date: Fri, 7 Feb 2014 14:05:58 +0100 Subject: [PATCH 2/2] Add priority usage examples --- ExampleProject/Example/TSDemoViewController.h | 3 ++ ExampleProject/Example/TSDemoViewController.m | 48 +++++++++++++++++++ .../en.lproj/MainStoryboard.storyboard | 40 +++++++++++++++- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/ExampleProject/Example/TSDemoViewController.h b/ExampleProject/Example/TSDemoViewController.h index dd353171..f3182d31 100644 --- a/ExampleProject/Example/TSDemoViewController.h +++ b/ExampleProject/Example/TSDemoViewController.h @@ -23,5 +23,8 @@ - (IBAction)didTapBottom:(id)sender; - (IBAction)didTapText:(id)sender; - (IBAction)didTapCustomDesign:(id)sender; +- (IBAction)didTapHighPriority:(id)sender; +- (IBAction)didTapNormalPriority:(id)sender; +- (IBAction)didTapLowPriority:(id)sender; @end diff --git a/ExampleProject/Example/TSDemoViewController.m b/ExampleProject/Example/TSDemoViewController.m index 3053cc7e..408bcefa 100644 --- a/ExampleProject/Example/TSDemoViewController.m +++ b/ExampleProject/Example/TSDemoViewController.m @@ -115,6 +115,54 @@ - (IBAction)didTapEndless:(id)sender canBeDismisedByUser:NO]; } +- (IBAction)didTapLowPriority:(id)sender +{ + [TSMessage showNotificationInViewController:self + title:NSLocalizedString(@"Low priority", nil) + subtitle:NSLocalizedString(@"This message has a low priority.", nil) + image:nil + type:TSMessageNotificationTypeSuccess + duration:TSMessageNotificationDurationAutomatic + priority:TSMessageNotificationPriorityLowest + callback:nil + buttonTitle:nil + buttonCallback:nil + atPosition:TSMessageNotificationPositionTop + canBeDismisedByUser:NO]; +} + +- (IBAction)didTapNormalPriority:(id)sender +{ + [TSMessage showNotificationInViewController:self + title:NSLocalizedString(@"Normal priority", nil) + subtitle:NSLocalizedString(@"This message has a normal priority.", nil) + image:nil + type:TSMessageNotificationTypeMessage + duration:TSMessageNotificationDurationAutomatic + priority:TSMessageNotificationPriorityNormal + callback:nil + buttonTitle:nil + buttonCallback:nil + atPosition:TSMessageNotificationPositionTop + canBeDismisedByUser:NO]; +} + +- (IBAction)didTapHighPriority:(id)sender +{ + [TSMessage showNotificationInViewController:self + title:NSLocalizedString(@"High priority", nil) + subtitle:NSLocalizedString(@"This message has a high priority!", nil) + image:nil + type:TSMessageNotificationTypeError + duration:TSMessageNotificationDurationAutomatic + priority:TSMessageNotificationPriorityHighest + callback:nil + buttonTitle:nil + buttonCallback:nil + atPosition:TSMessageNotificationPositionTop + canBeDismisedByUser:NO]; +} + - (IBAction)didTapLong:(id)sender { [TSMessage showNotificationInViewController:self diff --git a/ExampleProject/Example/en.lproj/MainStoryboard.storyboard b/ExampleProject/Example/en.lproj/MainStoryboard.storyboard index 4744e022..1ba22766 100644 --- a/ExampleProject/Example/en.lproj/MainStoryboard.storyboard +++ b/ExampleProject/Example/en.lproj/MainStoryboard.storyboard @@ -1,8 +1,8 @@ - + - + @@ -173,6 +173,33 @@ + + + @@ -182,7 +209,10 @@ + + + @@ -190,21 +220,27 @@ + + + + + +