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

New Features #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions Pod/Classes/SSSnackbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
IB_DESIGNABLE
@interface SSSnackbar : UIView
/**
* The duration of time for which the snackbar should be shown on-screen. Changing this value only has effect before the snackbar is presented by sending it the show message.
* The duration of time for which the snackbar should be shown on-screen, else 0 if the snackbar should be displayed indefinitely. Changing this value only has effect before the snackbar is presented by sending it the show message.
*/
@property (assign, nonatomic) NSTimeInterval duration;
/**
* If this value is set to YES, then the action block is not executed on the main thread to avoid blocking the UI. Instead, it is executed asynchronously on a background queue.
*/
@property (assign, nonatomic) BOOL actionIsLongRunning;
/**
* If this value is set to YES, then the user can tap within the body of the Snackbar to dismiss it without executing it's associated action. Changing this value only has effect before the snackbar is presented by sending it the show message.
*/
@property (assign, nonatomic) BOOL canTapToDismiss;
/**
* A block which is called when the user presses the action button on the snackbar.
*
Expand All @@ -37,7 +41,7 @@ IB_DESIGNABLE
*
* @param message The message displayed on the snackbars's text label.
* @param actionText The text displayed on the snackbar's action button.
* @param duration The duration of time for which the snackbar should remain on the screen.
* @param duration The duration of time for which the snackbar should remain on the screen, else 0 if the snackbar should be displayed indefinitely.
* @param actionBlock A block to be called when the user presses the action button.
* @param dismissalBlock A block to be called when the snackbar is removed from the screen by any means other than the user having pressed the action button. Can be nil.
*
Expand All @@ -52,7 +56,7 @@ IB_DESIGNABLE
*
* @param message The message displayed on the snackbars's text label.
* @param actionText The text displayed on the snackbar's action button.
* @param duration The duration of time for which the snackbar should remain on the screen.
* @param duration The duration of time for which the snackbar should remain on the screen, else 0 if the snackbar should be displayed indefinitely.
* @param actionBlock A block to be called when the user presses the action button.
* @param dismissalBlock A block to be called when the snackbar is removed from the screen by any means other than the user having pressed the action button. Can be nil.
*/
Expand All @@ -65,6 +69,12 @@ IB_DESIGNABLE
* Presents the snackbar to the user for the configured duration of time.
*/
- (void)show;
/**
* Presents the snackbar to the user for the configured duration of time.
*
* @param superview The view that the snackbar should be displayed within.
*/
- (void) showInView:(UIView *)superview;
/**
* Removes the snackbar from the screen. Calls the snackbar's dismissal block if one exists, unless the snackbar has its isLongRunning property set to YES and it action button has already been pressed by the user. This message is shorthand for calling dismissAnimated with YES as the argument.
*/
Expand All @@ -75,4 +85,18 @@ IB_DESIGNABLE
* @param animated Determines whether the snackbars's removal from the screen is animated or not.
*/
- (void)dismissAnimated:(BOOL)animated;
/**
* Changes the color of the Snackbar's message text.
*
* @param color The new color that the message text should use.
*/
- (void) setMessageTextColor:(UIColor *)color;
/**
* Changes the color of the Snackbar's action button text.
*
* @param color The new color that the action button text should use.
* @param state The button state that this text color should be applied to.
*/
- (void) setActionTextColor:(UIColor *)color
forState:(UIControlState)state;
@end
81 changes: 64 additions & 17 deletions Pod/Classes/SSSnackbar.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ @interface SSSnackbar ()
@property (strong, nonatomic) NSArray *horizontalLayoutConstraints;

@property (assign, nonatomic) BOOL actionBlockDispatched;

@property (strong, nonatomic) UITapGestureRecognizer * tapToDismissRecognizer;
@end

@implementation SSSnackbar
Expand Down Expand Up @@ -72,8 +74,12 @@ - (instancetype)initWithMessage:(NSString *)message

_separator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
_separator.backgroundColor = [UIColor colorWithWhite:0.99 alpha:.1];
_separator.hidden = TRUE;
_separator.translatesAutoresizingMaskIntoConstraints = NO;

_tapToDismissRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismiss)];

[self addSubview:_messageLabel];
[self addSubview:_actionButton];
[self addSubview:_separator];
Expand Down Expand Up @@ -107,7 +113,10 @@ - (void)show {
}

UIView *superview = topController.view;

[self showInView:superview];
}

- (void) showInView:(UIView *)superview {
BOOL shouldReplaceExistingSnackbar = currentlyVisibleSnackbar != nil;

if (shouldReplaceExistingSnackbar) {
Expand All @@ -133,22 +142,29 @@ - (void)show {
}
completion:nil];
}
self.dismissalTimer = [NSTimer scheduledTimerWithTimeInterval:self.duration
target:self
selector:@selector(timeoutForDismissal:)
userInfo:nil
repeats:NO];

if (self.duration > 0) {
self.dismissalTimer = [NSTimer scheduledTimerWithTimeInterval:self.duration
target:self
selector:@selector(timeoutForDismissal:)
userInfo:nil
repeats:NO];
}

if (_canTapToDismiss) {
[self addGestureRecognizer:_tapToDismissRecognizer];
}

currentlyVisibleSnackbar = self;
}

- (void)replaceExistingSnackbar {
UIView *superview = [UIApplication sharedApplication].delegate.window.rootViewController.view;
[currentlyVisibleSnackbar invalidateTimer];
[currentlyVisibleSnackbar removeFromSuperview];
[superview addSubview:self];
[superview addConstraints:self.horizontalLayoutConstraints];
[superview addConstraints:self.visibleVerticalLayoutConstraints];
[superview layoutIfNeeded];
[self.superview addSubview:self];
[self.superview addConstraints:self.horizontalLayoutConstraints];
[self.superview addConstraints:self.visibleVerticalLayoutConstraints];
[self.superview layoutIfNeeded];
[self setupContentLayout];
}

Expand All @@ -162,6 +178,7 @@ - (void)dismiss {

- (void)dismissAnimated:(BOOL)animated {
[self invalidateTimer];
[self removeGestureRecognizer:_tapToDismissRecognizer];
[self.superview removeConstraints:self.visibleVerticalLayoutConstraints];
[self.superview addConstraints:self.hiddenVerticalLayoutConstraints];
currentlyVisibleSnackbar = nil;
Expand Down Expand Up @@ -189,6 +206,7 @@ - (void)dismissAnimated:(BOOL)animated {

- (IBAction)executeAction:(id)sender {
[self invalidateTimer];
[self removeGestureRecognizer:_tapToDismissRecognizer];
if (self.actionIsLongRunning) {
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
indicator.translatesAutoresizingMaskIntoConstraints = NO;
Expand Down Expand Up @@ -265,12 +283,32 @@ - (NSArray *)visibleVerticalLayoutConstraints {

- (NSArray *)horizontalLayoutConstraints {
if (!_horizontalLayoutConstraints) {

_horizontalLayoutConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[self]-(5)-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(self)];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
_horizontalLayoutConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[self]-(5)-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(self)];
}
else {
_horizontalLayoutConstraints = @[

[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.superview
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f],
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:400.0]
];
}
}

return _horizontalLayoutConstraints;
Expand Down Expand Up @@ -309,4 +347,13 @@ - (void)setupContentLayout {
[self layoutIfNeeded];
}

- (void) setMessageTextColor:(UIColor *)color {
[_messageLabel setTextColor:color];
}

- (void) setActionTextColor:(UIColor *)color
forState:(UIControlState)state {
[_actionButton setTitleColor:color forState:state];
}

@end