Last active
December 16, 2015 04:29
-
-
Save robbdimitrov/5377745 to your computer and use it in GitHub Desktop.
Simple NetworkActivityIndicator manager
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
// RDVNetworkActivityIndicatorManager.h | |
// | |
// Copyright (c) 2013 Robert Dimitrov | |
// | |
// 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: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// 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. | |
#import <Foundation/Foundation.h> | |
@interface RDVNetworkActivityIndicatorManager : NSObject | |
+ (RDVNetworkActivityIndicatorManager *)sharedManager; | |
/* | |
* Increase tasks count. | |
* If tasks == 0, show the network activity indicator. | |
*/ | |
- (void)pushTask; | |
/* | |
* Decrease tasks count. | |
* If tasks == 0, hide the network activity indicator. | |
*/ | |
- (void)popTask; | |
/* | |
* Returs number of currently pushed tasks | |
*/ | |
- (NSInteger)tasksCount; | |
@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
// RDVNetworkActivityIndicatorManager.m | |
// | |
// Copyright (c) 2013 Robert Dimitrov | |
// | |
// 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: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// 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. | |
#import "RDVNetworkActivityIndicatorManager.h" | |
@interface RDVNetworkActivityIndicatorManager () { | |
NSInteger tasks; | |
} | |
@end | |
@implementation RDVNetworkActivityIndicatorManager | |
+ (RDVNetworkActivityIndicatorManager *)sharedManager { | |
static RDVNetworkActivityIndicatorManager *_sharedManager = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_sharedManager = [[RDVNetworkActivityIndicatorManager alloc] init]; | |
}); | |
return _sharedManager; | |
} | |
- (void)pushTask { | |
void (^block)(void) = ^{ | |
tasks++; | |
if (tasks == 1) { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; | |
} | |
}; | |
if ([NSThread isMainThread]) { | |
block(); | |
} else { | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
block(); | |
}); | |
} | |
} | |
- (void)popTask { | |
if (tasks <= 0) { | |
return; | |
} | |
void (^block)(void) = ^{ | |
tasks--; | |
if (tasks == 0) { | |
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; | |
} | |
}; | |
if ([NSThread isMainThread]) { | |
block(); | |
} else { | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
block(); | |
}); | |
} | |
} | |
- (NSInteger)tasksCount { | |
return tasks; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment