Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags and endless notifications helper #197

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions Example/TSMessages/TSDemoViewController.m
Expand Up @@ -44,6 +44,17 @@ - (IBAction)didTapMessage:(id)sender
type:TSMessageNotificationTypeMessage];
}


- (IBAction)didTapEndlessMessage:(id)sender
{
[TSMessage showEndlessNotificationWithTag:NSLocalizedString(@"Tell the user something", nil)
subtitle:NSLocalizedString(@"This is some neutral notification it wont disappear unless you tell it to!", nil)
type:TSMessageNotificationTypeMessage
tag:10];

}


- (IBAction)didTapSuccess:(id)sender
{
[TSMessage showNotificationWithTitle:NSLocalizedString(@"Success", nil)
Expand Down
66 changes: 64 additions & 2 deletions Pod/Classes/TSMessage.h
Expand Up @@ -76,8 +76,10 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) {
@param message The title of the notification view
@param type The notification type (Message, Warning, Error, Success)
*/
+ (void)showNotificationWithTitle:(NSString *)message
type:(TSMessageNotificationType)type;
+ (void)showNotificationWithTitle:(NSString *)message type:(TSMessageNotificationType)type;


+ (void)showEndlessNotificationWithTitle:(NSString *)message type:(TSMessageNotificationType)type;

/** Shows a notification message
@param title The title of the notification view
Expand All @@ -88,6 +90,24 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) {
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type;

+ (void)showEndlessNotification:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type;

/** Shows an endless notification message
@param title The title of the notification view
@param subtitle The text that is displayed underneath the title
@param type The notification type (Message, Warning, Error, Success)
*/
+(void)showEndlessNotification:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type;

+(void)showEndlessNotificationWithTag:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
tag:(NSInteger)tag;

/** Shows a notification message in a specific view controller
@param viewController The view controller to show the notification in.
You can use +setDefaultViewController: to set the the default one instead
Expand All @@ -100,6 +120,18 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) {
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type;

/** Shows an endless notification message in a specific view controller
@param viewController The view controller to show the notification in.
You can use +setDefaultViewController: to set the the default one instead
@param title The title of the notification view
@param subtitle The text that is displayed underneath the title
@param type The notification type (Message, Warning, Error, Success)
*/
+ (TSMessageView*)showEndlessNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type;

/** Shows a notification message in a specific view controller with a specific duration
@param viewController The view controller to show the notification in.
You can use +setDefaultViewController: to set the the default one instead
Expand All @@ -114,6 +146,7 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) {
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration;


/** Shows a notification message in a specific view controller with a specific duration
@param viewController The view controller to show the notification in.
You can use +setDefaultViewController: to set the the default one instead
Expand Down Expand Up @@ -157,13 +190,42 @@ typedef NS_ENUM(NSInteger,TSMessageNotificationDuration) {
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled;

/** Shows a notification message with a tag 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 tag 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 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
*/
+ (TSMessageView*)showNotificationWithTagInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
tag:(NSInteger)tag
image:(UIImage *)image
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled;

/** 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
was currently displayed.
*/
+ (BOOL)dismissActiveNotification;

+ (void)dismissNotificationsWithTag:(NSInteger)tag;

/** 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
@return YES if the currently displayed notification was successfully dismissed. NO if no notification
Expand Down
159 changes: 133 additions & 26 deletions Pod/Classes/TSMessage.m
Expand Up @@ -51,6 +51,16 @@ + (void)showNotificationWithTitle:(NSString *)title
type:type];
}

+ (void)showEndlessNotificationWithTitle:(NSString *)title
type:(TSMessageNotificationType)type
{
[self showEndlessNotificationInViewController:[self defaultViewController]
title:title
subtitle:nil
tag:-1
type:type];
}

+ (void)showNotificationWithTitle:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
Expand All @@ -61,6 +71,29 @@ + (void)showNotificationWithTitle:(NSString *)title
type:type];
}

+(void)showEndlessNotification:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
{
[self showEndlessNotificationInViewController:[self defaultViewController]
title:title
subtitle:subtitle
tag:-1
type:type];
}

+ (void)showEndlessNotificationWithTag:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
tag:(NSInteger)tag
{
[self showEndlessNotificationInViewController:[self defaultViewController]
title:title
subtitle:subtitle
tag:tag
type:type];
}

+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
Expand Down Expand Up @@ -119,49 +152,110 @@ + (void)showNotificationInViewController:(UIViewController *)viewController
}


+ (TSMessageView*)showEndlessNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
{
return [TSMessage showEndlessNotificationInViewController:viewController title:title subtitle:subtitle tag:-1 type:type];
}


+ (TSMessageView*)showEndlessNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
tag:(NSInteger)tag
type:(TSMessageNotificationType)type
{
return [TSMessage showNotificationWithTagInViewController:viewController
title:title
subtitle:subtitle
tag:tag
image:nil
type:type
duration:TSMessageNotificationDurationEndless
callback:nil
buttonTitle:nil
buttonCallback:nil
atPosition:TSMessageNotificationPositionTop
canBeDismissedByUser:YES];

}

+ (TSMessageView*)showNotificationWithTagInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
tag:(NSInteger)tag
image:(UIImage *)image
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled
{
// Create the TSMessageView
TSMessageView *v = [[TSMessageView alloc] initWithTag:title
subtitle:subtitle
tag:tag
image:image
type:type
duration:duration
inViewController:viewController
callback:callback
buttonTitle:buttonTitle
buttonCallback:buttonCallback
atPosition:messagePosition
canBeDismissedByUser:dismissingEnabled];
[self prepareNotificationToBeShown:v];

return v;
}

+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled
title:(NSString *)title
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled
{
// Create the TSMessageView
TSMessageView *v = [[TSMessageView alloc] initWithTitle:title
subtitle:subtitle
image:image
type:type
duration:duration
inViewController:viewController
callback:callback
buttonTitle:buttonTitle
buttonCallback:buttonCallback
atPosition:messagePosition
canBeDismissedByUser:dismissingEnabled];
[self prepareNotificationToBeShown:v];
// Create the TSMessageView
TSMessageView *v = [[TSMessageView alloc] initWithTitle:title
subtitle:subtitle
image:image
type:type
duration:duration
inViewController:viewController
callback:callback
buttonTitle:buttonTitle
buttonCallback:buttonCallback
atPosition:messagePosition
canBeDismissedByUser:dismissingEnabled];
[self prepareNotificationToBeShown:v];
}


+ (void)prepareNotificationToBeShown:(TSMessageView *)messageView
{
NSString *title = messageView.title;
NSString *subtitle = messageView.subtitle;
for (TSMessageView *n in [TSMessage sharedMessage].messages)
{
if (([n.title isEqualToString:title] || (!n.title && !title)) && ([n.subtitle isEqualToString:subtitle] || (!n.subtitle && !subtitle)))
{
return; // avoid showing the same messages twice in a row
}
}
[[TSMessage sharedMessage].messages addObject:messageView];
if (!notificationActive)
{
[[TSMessage sharedMessage] fadeInCurrentNotification];
Expand Down Expand Up @@ -374,6 +468,19 @@ - (void)fadeOutNotification:(TSMessageView *)currentView animationFinishedBlock:
}];
}

+ (void)dismissNotificationsWithTag:(NSInteger)tag
{
NSArray *messages = [TSMessage queuedMessages];

for(TSMessageView *messageView in messages)
{
if(messageView.tag == tag)
{
[[TSMessage sharedMessage] fadeOutNotification:messageView];
}
}
}

+ (BOOL)dismissActiveNotification
{
return [self dismissActiveNotificationWithCompletion:nil];
Expand Down
44 changes: 36 additions & 8 deletions Pod/Classes/TSMessageView.h
Expand Up @@ -34,6 +34,8 @@
/** Is the message currenlty fully displayed? Is set as soon as the message is really fully visible */
@property (nonatomic, assign) BOOL messageIsFullyDisplayed;

@property (nonatomic, assign) BOOL centerIconImage;

/** Customize title font using Apperance */
@property (nonatomic,strong) UIFont *titleFont UI_APPEARANCE_SELECTOR;
@property (nonatomic,strong) UIColor *titleTextColor UI_APPEARANCE_SELECTOR;
Expand All @@ -60,15 +62,41 @@
@param dismissingEnabled Should this message be dismissed when the user taps/swipes it?
*/
- (id)initWithTitle:(NSString *)title
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)notificationType
duration:(CGFloat)duration
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)notificationType
duration:(CGFloat)duration
inViewController:(UIViewController *)viewController
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)position
canBeDismissedByUser:(BOOL)dismissingEnabled;

/** Inits the notification view. Do not call this from outside this library.
@param title The title of the notification view
@param subtitle The subtitle of the notification view (optional)
@param image A custom icon image (optional)
@param notificationType The type (color) of the notification view
@param duration The duration this notification should be displayed (optional)
@param viewController The view controller this message should be displayed in
@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 position The position of the message on the screen
@param dismissingEnabled Should this message be dismissed when the user taps/swipes it?
*/
- (id)initWithTag:(NSString *)title
subtitle:(NSString *)subtitle
tag:(NSInteger)tag
image:(UIImage *)image
type:(TSMessageNotificationType)notificationType
duration:(CGFloat)duration
inViewController:(UIViewController *)viewController
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)position
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)position
canBeDismissedByUser:(BOOL)dismissingEnabled;

/** Fades out this notification view */
Expand Down