[Issue #65] Adding slight delay when hiding network activity indicator, to avoid flickering
This commit is contained in:
parent
9b1c1f285c
commit
c1dbf407be
2 changed files with 29 additions and 2 deletions
|
|
@ -29,6 +29,7 @@
|
||||||
@private
|
@private
|
||||||
NSInteger _activityCount;
|
NSInteger _activityCount;
|
||||||
BOOL _enabled;
|
BOOL _enabled;
|
||||||
|
NSTimer *_activityIndicatorVisibilityTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,21 @@
|
||||||
|
|
||||||
#import "AFHTTPRequestOperation.h"
|
#import "AFHTTPRequestOperation.h"
|
||||||
|
|
||||||
|
static NSTimeInterval const kAFNetworkActivityIndicatorInvisibilityDelay = 0.25;
|
||||||
|
|
||||||
@interface AFNetworkActivityIndicatorManager ()
|
@interface AFNetworkActivityIndicatorManager ()
|
||||||
@property (readwrite, nonatomic, assign) NSInteger activityCount;
|
@property (readwrite, nonatomic, assign) NSInteger activityCount;
|
||||||
|
@property (readwrite, nonatomic, retain) NSTimer *activityIndicatorVisibilityTimer;
|
||||||
|
@property (readonly, getter = isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
|
||||||
|
|
||||||
|
- (void)updateNetworkActivityIndicatorVisibility;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation AFNetworkActivityIndicatorManager
|
@implementation AFNetworkActivityIndicatorManager
|
||||||
@synthesize activityCount = _activityCount;
|
@synthesize activityCount = _activityCount;
|
||||||
|
@synthesize activityIndicatorVisibilityTimer = _activityIndicatorVisibilityTimer;
|
||||||
@synthesize enabled = _enabled;
|
@synthesize enabled = _enabled;
|
||||||
|
@dynamic networkActivityIndicatorVisible;
|
||||||
|
|
||||||
+ (AFNetworkActivityIndicatorManager *)sharedManager {
|
+ (AFNetworkActivityIndicatorManager *)sharedManager {
|
||||||
static AFNetworkActivityIndicatorManager *_sharedManager = nil;
|
static AFNetworkActivityIndicatorManager *_sharedManager = nil;
|
||||||
|
|
@ -57,6 +65,9 @@
|
||||||
- (void)dealloc {
|
- (void)dealloc {
|
||||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||||
|
|
||||||
|
[_activityIndicatorVisibilityTimer invalidate];
|
||||||
|
[_activityIndicatorVisibilityTimer release]; _activityIndicatorVisibilityTimer = nil;
|
||||||
|
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,10 +77,25 @@
|
||||||
[self didChangeValueForKey:@"activityCount"];
|
[self didChangeValueForKey:@"activityCount"];
|
||||||
|
|
||||||
if (self.enabled) {
|
if (self.enabled) {
|
||||||
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:self.activityCount > 0];
|
// 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];
|
||||||
|
[[NSRunLoop currentRunLoop] addTimer:self.activityIndicatorVisibilityTimer forMode:NSRunLoopCommonModes];
|
||||||
|
} else {
|
||||||
|
[self updateNetworkActivityIndicatorVisibility];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)isNetworkActivityIndicatorVisible {
|
||||||
|
return self.activityCount > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateNetworkActivityIndicatorVisibility {
|
||||||
|
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)incrementActivityCount {
|
- (void)incrementActivityCount {
|
||||||
@synchronized(self) {
|
@synchronized(self) {
|
||||||
self.activityCount += 1;
|
self.activityCount += 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue