Skip to content

Commit

Permalink
add completion block to hide loading view with success
Browse files Browse the repository at this point in the history
  • Loading branch information
grantjk committed Jul 4, 2012
1 parent 26e8258 commit 6d7db24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions JGALoadingView/JGALoadingView.h
Expand Up @@ -8,6 +8,8 @@

#import <UIKit/UIKit.h>

typedef void(^JGALoadingViewCompletionBlock)(void);

@interface JGALoadingView : UIView

// Create a new view with loading text
Expand All @@ -28,5 +30,7 @@
// Remove loading view based on key
+ (void)hideLoadingViewForKey:(NSString *)key;

// Removes the loading view with a success and takes a block to execute after success delay
+ (void)hideLoadingViewWithSuccess:(NSString *)message delay:(int)delay completion:(void(^)(void))completion;

@end
26 changes: 22 additions & 4 deletions JGALoadingView/JGALoadingView.m
Expand Up @@ -50,6 +50,8 @@ @implementation JGALoadingView
#define kNotifSuccess @"successNotification"
#define kNotifError @"errorNotification"

#define kCompletionBlock @"completion_block"

static NSString *animationScaleUpKey = @"scaleUp";
static NSString *animationScaleNormKey = @"scaleNorm";
static NSString *animationScaleOutKey = @"scaleOut";
Expand Down Expand Up @@ -108,19 +110,29 @@ - (void)showNotificationImage:(UIImage *)image opts:(NSDictionary *)opts
target:self
selector:@selector(hide:)
userInfo:nil repeats:0];

if ([opts objectForKey:kCompletionBlock]) {
[NSTimer scheduledTimerWithTimeInterval:delay
target:self
selector:@selector(executeCompletionBlock:)
userInfo:opts repeats:0];
}
}

- (void)executeCompletionBlock:(NSTimer *)timer
{
((JGALoadingViewCompletionBlock)[[timer userInfo] objectForKey:kCompletionBlock])();
}

- (void)showSuccessNotification:(NSNotification *)notification
{
UIImage *checkmark = [UIImage imageNamed:@"WhiteCheck"];
DLog(@"showing checkmark");
[self showNotificationImage:checkmark opts:notification.userInfo];
}

- (void)showFailNotification:(NSNotification *)notification
{
UIImage *failImage = [UIImage imageNamed:@"WhiteX"];
DLog(@"showing error");
[self showNotificationImage:failImage opts:notification.userInfo];
}

Expand Down Expand Up @@ -211,14 +223,21 @@ + (void)hideLoadingViewForKey:(NSString *)key
}

+ (void)hideLoadingViewWithSuccess:(NSString *)message delay:(int)delay
{

[JGALoadingView hideLoadingViewWithSuccess:message delay:delay completion:nil];
}

+ (void)hideLoadingViewWithSuccess:(NSString *)message delay:(int)delay completion:(void(^)(void))completion
{
NSMutableDictionary *opts = [NSMutableDictionary dictionaryWithCapacity:2];
[opts setObject:message forKey:kOptsKeyMessage];
[opts setObject:[NSNumber numberWithInt:delay] forKey:kOptsKeyDelay];
if (completion) [opts setObject:completion forKey:kCompletionBlock];

[[NSNotificationCenter defaultCenter] postNotificationName:kNotifSuccess
object:nil
userInfo:opts];
userInfo:opts];
}

+ (void)hideLoadingViewWithError:(NSString *)message delay:(int)delay
Expand All @@ -232,7 +251,6 @@ + (void)hideLoadingViewWithError:(NSString *)message delay:(int)delay
userInfo:opts];
}


#pragma mark - Animation
-(void)scaleLayerTo:(float)scaleValue duration:(float)duration withKey:(NSString *)key fadeOpts:(NSDictionary *)fadeOpts{
// Bounce In
Expand Down

0 comments on commit 6d7db24

Please sign in to comment.