Last active
August 29, 2015 14:16
-
-
Save pronebird/552a547e1e9a0ad6814f to your computer and use it in GitHub Desktop.
JDStatusBarNotification+Queue category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create statusbar activity per upload | |
JDStatusBarActivity* upload1 = [JDStatusBarActivity new]; | |
upload1.styleName = JDStatusBarStyleDark; | |
upload1.message = @"Uploading file #1"; | |
JDStatusBarActivity* upload2 = [JDStatusBarActivity new]; | |
upload2.styleName = JDStatusBarStyleDark; | |
upload2.message = @"Uploading file #2"; | |
// Add activities to status bar | |
// FIFO principle, last added is displayed in statusbar | |
[JDStatusBarNotification addActivity:upload1]; | |
[JDStatusBarNotification addActivity:upload2]; | |
// KVO observers for NSProgress | |
NSProgress* progress1 = /* ... */; | |
NSProgress *progress2 = /* ... */; | |
[self.KVOController observe:progress1 keyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew block:^(id observer, NSProgress* progress, NSDictionary *change) { | |
upload1.progress = progress.fractionCompleted; | |
if(progress.completedUnitCount == progress.totalUnitCount) { | |
[upload1 complete]; // pop statusbar activity when finished | |
} | |
}]; | |
[self.KVOController observe:progress2 keyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew block:^(id observer, NSProgress* progress, NSDictionary *change) { | |
upload2.progress = progress.fractionCompleted; | |
if(progress.completedUnitCount == progress.totalUnitCount) { | |
[upload2 complete]; // pop statusbar activity when finished | |
} | |
}]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// JDStatusBarNotification+Queue.h | |
// | |
// Created by pronebird on 3/3/15. | |
// Copyright (c) 2015 pronebird. All rights reserved. | |
// | |
#import <JDStatusBarNotification/JDStatusBarNotification.h> | |
@interface JDStatusBarActivity : NSObject | |
@property (nonatomic) NSString* message; | |
@property (nonatomic) CGFloat progress; | |
@property (nonatomic) NSString* styleName; | |
@property (nonatomic, readonly, getter=isCurrent) BOOL current; | |
- (void)complete; | |
@end | |
@interface JDStatusBarNotification (Queue) | |
+ (void)addActivity:(JDStatusBarActivity*)activity; | |
+ (void)removeActivity:(JDStatusBarActivity*)activity; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// JDStatusBarNotification+Queue.m | |
// | |
// Created by pronebird on 3/3/15. | |
// Copyright (c) 2015 pronebird. All rights reserved. | |
// | |
#import "JDStatusBarNotification+Queue.h" | |
static NSMutableArray* _queue; | |
@interface JDStatusBarActivity() | |
@property (readwrite) BOOL current; | |
@end | |
@implementation JDStatusBarActivity | |
- (void)setMessage:(NSString *)message { | |
if(![_message isEqualToString:message]) { | |
_message = message; | |
if([self isCurrent]) { | |
[JDStatusBarNotification showWithStatus:message styleName:self.styleName]; | |
} | |
} | |
} | |
- (void)setProgress:(CGFloat)progress { | |
if(_progress != progress) { | |
_progress = progress; | |
if([self isCurrent]) { | |
[JDStatusBarNotification showProgress:self.progress]; | |
} | |
} | |
} | |
- (void)setStyleName:(NSString *)styleName { | |
if(![_styleName isEqualToString:styleName]) { | |
_styleName = styleName; | |
if(self.message && [self isCurrent]) { | |
[JDStatusBarNotification showWithStatus:self.message styleName:styleName]; | |
} | |
} | |
} | |
- (void)fire { | |
if(![self isCurrent]) { | |
return; | |
} | |
[JDStatusBarNotification showWithStatus:self.message styleName:self.styleName]; | |
[JDStatusBarNotification showProgress:self.progress]; | |
} | |
- (void)complete { | |
[JDStatusBarNotification removeActivity:self]; | |
} | |
@end | |
@implementation JDStatusBarNotification (Queue) | |
+ (void)addActivity:(JDStatusBarActivity*)activity { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_queue = [NSMutableArray new]; | |
}); | |
JDStatusBarActivity* prevActivity = [_queue lastObject]; | |
prevActivity.current = NO; | |
activity.current = YES; | |
[_queue addObject:activity]; | |
[activity fire]; | |
} | |
+ (void)removeActivity:(JDStatusBarActivity*)activity { | |
if(!_queue) { | |
return; | |
} | |
NSUInteger index = [_queue indexOfObject:activity]; | |
if(index == NSNotFound) { | |
return; | |
} | |
[_queue removeObjectAtIndex:index]; | |
JDStatusBarActivity* prevActivity = [_queue lastObject]; | |
activity.current = NO; | |
if(index == _queue.count) { | |
prevActivity.current = YES; | |
[prevActivity fire]; | |
} | |
if(_queue.count == 0) { | |
[JDStatusBarNotification dismissAnimated:YES]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment