2011-07-27 15:14:15 -05:00
|
|
|
// AFNetworkActivityIndicatorManager.m
|
2011-05-31 16:27:34 -05:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-05-31 16:27:34 -05:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-05-31 16:27:34 -05:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2013-01-27 12:15:07 -05:00
|
|
|
//
|
2011-05-31 16:27:34 -05:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
#import "AFNetworkActivityIndicatorManager.h"
|
2011-05-31 16:27:34 -05:00
|
|
|
|
2011-09-23 15:01:19 -05:00
|
|
|
#import "AFHTTPRequestOperation.h"
|
|
|
|
|
|
2012-11-30 17:47:14 +11:00
|
|
|
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
|
2012-07-23 12:05:21 -07:00
|
|
|
static NSTimeInterval const kAFNetworkActivityIndicatorInvisibilityDelay = 0.17;
|
2011-10-06 10:43:48 -05:00
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
@interface AFNetworkActivityIndicatorManager ()
|
2012-08-22 10:09:24 -07:00
|
|
|
@property (readwrite, assign) NSInteger activityCount;
|
2012-10-09 09:26:50 -07:00
|
|
|
@property (readwrite, nonatomic, strong) NSTimer *activityIndicatorVisibilityTimer;
|
2011-10-06 10:43:48 -05:00
|
|
|
@property (readonly, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
|
|
|
|
|
|
|
|
|
|
- (void)updateNetworkActivityIndicatorVisibility;
|
2012-07-23 12:05:21 -07:00
|
|
|
- (void)updateNetworkActivityIndicatorVisibilityDelayed;
|
2011-05-31 16:27:34 -05:00
|
|
|
@end
|
|
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
@implementation AFNetworkActivityIndicatorManager
|
|
|
|
|
@synthesize activityCount = _activityCount;
|
2011-10-06 10:43:48 -05:00
|
|
|
@synthesize activityIndicatorVisibilityTimer = _activityIndicatorVisibilityTimer;
|
2011-09-23 15:01:19 -05:00
|
|
|
@synthesize enabled = _enabled;
|
2011-10-06 10:43:48 -05:00
|
|
|
@dynamic networkActivityIndicatorVisible;
|
2011-05-31 16:27:34 -05:00
|
|
|
|
2012-12-23 10:59:56 +02:00
|
|
|
+ (instancetype)sharedManager {
|
2011-07-27 15:14:15 -05:00
|
|
|
static AFNetworkActivityIndicatorManager *_sharedManager = nil;
|
2011-08-30 17:16:23 -07:00
|
|
|
static dispatch_once_t oncePredicate;
|
|
|
|
|
dispatch_once(&oncePredicate, ^{
|
2011-09-23 15:01:19 -05:00
|
|
|
_sharedManager = [[self alloc] init];
|
2011-08-30 17:16:23 -07:00
|
|
|
});
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
return _sharedManager;
|
2011-05-31 16:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
2012-08-22 10:09:24 -07:00
|
|
|
+ (NSSet *)keyPathsForValuesAffectingIsNetworkActivityIndicatorVisible {
|
|
|
|
|
return [NSSet setWithObject:@"activityCount"];
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-23 15:01:19 -05:00
|
|
|
- (id)init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (!self) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-10-04 00:13:12 -05:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incrementActivityCount) name:AFNetworkingOperationDidStartNotification object:nil];
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(decrementActivityCount) name:AFNetworkingOperationDidFinishNotification object:nil];
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-09-23 15:01:19 -05:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-10-06 10:43:48 -05:00
|
|
|
[_activityIndicatorVisibilityTimer invalidate];
|
2013-01-27 12:15:07 -05:00
|
|
|
|
2011-09-23 15:01:19 -05:00
|
|
|
}
|
|
|
|
|
|
2012-04-05 17:29:18 -07:00
|
|
|
- (void)updateNetworkActivityIndicatorVisibilityDelayed {
|
2011-09-23 15:01:19 -05:00
|
|
|
if (self.enabled) {
|
2011-10-06 10:43:48 -05:00
|
|
|
// Delay hiding of activity indicator for a short interval, to avoid flickering
|
|
|
|
|
if (![self isNetworkActivityIndicatorVisible]) {
|
|
|
|
|
[self.activityIndicatorVisibilityTimer invalidate];
|
|
|
|
|
self.activityIndicatorVisibilityTimer = [NSTimer timerWithTimeInterval:kAFNetworkActivityIndicatorInvisibilityDelay target:self selector:@selector(updateNetworkActivityIndicatorVisibility) userInfo:nil repeats:NO];
|
2012-07-10 21:12:54 -04:00
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.activityIndicatorVisibilityTimer forMode:NSRunLoopCommonModes];
|
2011-10-06 10:43:48 -05:00
|
|
|
} else {
|
2012-07-23 12:05:21 -07:00
|
|
|
[self performSelectorOnMainThread:@selector(updateNetworkActivityIndicatorVisibility) withObject:nil waitUntilDone:NO modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
|
2011-10-06 10:43:48 -05:00
|
|
|
}
|
2011-09-23 15:01:19 -05:00
|
|
|
}
|
2011-05-31 16:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
2011-10-06 10:43:48 -05:00
|
|
|
- (BOOL)isNetworkActivityIndicatorVisible {
|
2012-04-05 17:29:18 -07:00
|
|
|
return _activityCount > 0;
|
2011-10-06 10:43:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)updateNetworkActivityIndicatorVisibility {
|
2012-07-10 21:12:54 -04:00
|
|
|
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]];
|
2011-10-06 10:43:48 -05:00
|
|
|
}
|
|
|
|
|
|
2012-04-08 12:02:19 -07:00
|
|
|
// Not exposed, but used if activityCount is set via KVC.
|
2012-06-14 08:50:49 -07:00
|
|
|
- (NSInteger)activityCount {
|
|
|
|
|
return _activityCount;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 17:29:18 -07:00
|
|
|
- (void)setActivityCount:(NSInteger)activityCount {
|
2012-06-14 08:50:49 -07:00
|
|
|
@synchronized(self) {
|
|
|
|
|
_activityCount = activityCount;
|
|
|
|
|
}
|
2012-04-05 17:29:18 -07:00
|
|
|
[self updateNetworkActivityIndicatorVisibilityDelayed];
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-22 10:25:55 -05:00
|
|
|
- (void)incrementActivityCount {
|
2012-04-05 17:29:18 -07:00
|
|
|
[self willChangeValueForKey:@"activityCount"];
|
2012-06-14 08:50:49 -07:00
|
|
|
@synchronized(self) {
|
|
|
|
|
_activityCount++;
|
|
|
|
|
}
|
2012-04-05 17:29:18 -07:00
|
|
|
[self didChangeValueForKey:@"activityCount"];
|
|
|
|
|
[self updateNetworkActivityIndicatorVisibilityDelayed];
|
2011-05-31 16:27:34 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-22 10:25:55 -05:00
|
|
|
- (void)decrementActivityCount {
|
2012-04-05 17:29:18 -07:00
|
|
|
[self willChangeValueForKey:@"activityCount"];
|
2012-06-14 08:50:49 -07:00
|
|
|
@synchronized(self) {
|
2012-09-25 07:15:02 -07:00
|
|
|
_activityCount = MAX(_activityCount - 1, 0);
|
2012-06-14 08:50:49 -07:00
|
|
|
}
|
2012-04-05 17:29:18 -07:00
|
|
|
[self didChangeValueForKey:@"activityCount"];
|
|
|
|
|
[self updateNetworkActivityIndicatorVisibilityDelayed];
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-27 15:14:15 -05:00
|
|
|
@end
|
2011-11-11 11:28:05 -06:00
|
|
|
|
2011-10-11 10:05:07 -05:00
|
|
|
#endif
|