Adding enabled @property to AFNetworkActivityIndicatorManager
Decoupling AFNetworkActivityIndicatorManager from AFHTTPRequestOperation, by using notifications instead of directly incrementing and decrementing the activity count Initializing the network activity indicator in App Delegate
This commit is contained in:
parent
1748a62b8d
commit
e76262c275
@ -21,7 +21,6 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import "AFHTTPRequestOperation.h"
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
|
||||
static NSUInteger const kAFHTTPMinimumInitialDataCapacity = 1024;
|
||||
static NSUInteger const kAFHTTPMaximumInitialDataCapacity = 1024 * 1024 * 8;
|
||||
@ -227,11 +226,9 @@ static NSThread *_networkRequestThread = nil;
|
||||
|
||||
switch (state) {
|
||||
case AFHTTPOperationExecutingState:
|
||||
[[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidStartNotification object:self];
|
||||
break;
|
||||
case AFHTTPOperationFinishedState:
|
||||
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidFinishNotification object:self];
|
||||
break;
|
||||
default:
|
||||
|
||||
@ -23,13 +23,21 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
`AFNetworkActivityIndicatorManager` manages the state of the network activity indicator in the status bar. When network operations start, they can call `-incrementActivityCount`, and once they're finished, call `-decrementActivityCount`. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero.
|
||||
`AFNetworkActivityIndicatorManager` manages the state of the network activity indicator in the status bar. When enabled, it will listen for notifications indicating that a network request operation has started or finished, and start or stop animating the indicator accordingly. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero.
|
||||
*/
|
||||
@interface AFNetworkActivityIndicatorManager : NSObject {
|
||||
@private
|
||||
NSInteger _activityCount;
|
||||
BOOL _enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
A Boolean value indicating whether the manager is enabled.
|
||||
|
||||
@discussion If YES, the manager will change status bar network activity indicator according to network operation notifications it receives. The default value is NO.
|
||||
*/
|
||||
@property (nonatomic, assign, getter = isEnabled) BOOL enabled;
|
||||
|
||||
/**
|
||||
Returns the shared network activity indicator manager object for the system.
|
||||
|
||||
|
||||
@ -22,29 +22,52 @@
|
||||
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
|
||||
#import "AFHTTPRequestOperation.h"
|
||||
|
||||
@interface AFNetworkActivityIndicatorManager ()
|
||||
@property (readwrite, nonatomic, assign) NSInteger activityCount;
|
||||
@end
|
||||
|
||||
@implementation AFNetworkActivityIndicatorManager
|
||||
@synthesize activityCount = _activityCount;
|
||||
@synthesize enabled = _enabled;
|
||||
|
||||
+ (AFNetworkActivityIndicatorManager *)sharedManager {
|
||||
static AFNetworkActivityIndicatorManager *_sharedManager = nil;
|
||||
static dispatch_once_t oncePredicate;
|
||||
dispatch_once(&oncePredicate, ^{
|
||||
_sharedManager = [[AFNetworkActivityIndicatorManager alloc] init];
|
||||
_sharedManager = [[self alloc] init];
|
||||
});
|
||||
|
||||
return _sharedManager;
|
||||
}
|
||||
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(incrementActivityCount) name:AFHTTPOperationDidStartNotification object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(decrementActivityCount) name:AFHTTPOperationDidFinishNotification object:nil];
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (void)setActivityCount:(NSInteger)activityCount {
|
||||
[self willChangeValueForKey:@"activityCount"];
|
||||
_activityCount = MAX(activityCount, 0);
|
||||
[self didChangeValueForKey:@"activityCount"];
|
||||
|
||||
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:self.activityCount > 0];
|
||||
if (self.enabled) {
|
||||
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:self.activityCount > 0];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)incrementActivityCount {
|
||||
|
||||
@ -23,6 +23,8 @@
|
||||
#import "AppDelegate.h"
|
||||
#import "NearbySpotsViewController.h"
|
||||
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
|
||||
@implementation AppDelegate
|
||||
@synthesize window = _window;
|
||||
@synthesize navigationController = _navigationController;
|
||||
@ -31,6 +33,8 @@
|
||||
NSURLCache *URLCache = [[[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil] autorelease];
|
||||
[NSURLCache setSharedURLCache:URLCache];
|
||||
|
||||
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
|
||||
|
||||
UITableViewController *viewController = [[[NearbySpotsViewController alloc] init] autorelease];
|
||||
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
|
||||
|
||||
@ -38,6 +42,7 @@
|
||||
self.window.backgroundColor = [UIColor whiteColor];
|
||||
self.window.rootViewController = self.navigationController;
|
||||
[self.window makeKeyAndVisible];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user