AFNetworkActivityIndicatorManager.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // AFNetworkActivityIndicatorManager.m
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "AFNetworkActivityIndicatorManager.h"
  22. #if TARGET_OS_IOS
  23. #import "AFURLSessionManager.h"
  24. typedef NS_ENUM(NSInteger, AFNetworkActivityManagerState) {
  25. AFNetworkActivityManagerStateNotActive,
  26. AFNetworkActivityManagerStateDelayingStart,
  27. AFNetworkActivityManagerStateActive,
  28. AFNetworkActivityManagerStateDelayingEnd
  29. };
  30. static NSTimeInterval const kDefaultAFNetworkActivityManagerActivationDelay = 1.0;
  31. static NSTimeInterval const kDefaultAFNetworkActivityManagerCompletionDelay = 0.17;
  32. static NSURLRequest * AFNetworkRequestFromNotification(NSNotification *notification) {
  33. if ([[notification object] respondsToSelector:@selector(originalRequest)]) {
  34. return [(NSURLSessionTask *)[notification object] originalRequest];
  35. } else {
  36. return nil;
  37. }
  38. }
  39. typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisible);
  40. @interface AFNetworkActivityIndicatorManager ()
  41. @property (readwrite, nonatomic, assign) NSInteger activityCount;
  42. @property (readwrite, nonatomic, strong) NSTimer *activationDelayTimer;
  43. @property (readwrite, nonatomic, strong) NSTimer *completionDelayTimer;
  44. @property (readonly, nonatomic, getter = isNetworkActivityOccurring) BOOL networkActivityOccurring;
  45. @property (nonatomic, copy) AFNetworkActivityActionBlock networkActivityActionBlock;
  46. @property (nonatomic, assign) AFNetworkActivityManagerState currentState;
  47. @property (nonatomic, assign, getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
  48. - (void)updateCurrentStateForNetworkActivityChange;
  49. @end
  50. @implementation AFNetworkActivityIndicatorManager
  51. + (instancetype)sharedManager {
  52. static AFNetworkActivityIndicatorManager *_sharedManager = nil;
  53. static dispatch_once_t oncePredicate;
  54. dispatch_once(&oncePredicate, ^{
  55. _sharedManager = [[self alloc] init];
  56. });
  57. return _sharedManager;
  58. }
  59. - (instancetype)init {
  60. self = [super init];
  61. if (!self) {
  62. return nil;
  63. }
  64. self.currentState = AFNetworkActivityManagerStateNotActive;
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidStart:) name:AFNetworkingTaskDidResumeNotification object:nil];
  66. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidFinish:) name:AFNetworkingTaskDidSuspendNotification object:nil];
  67. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkRequestDidFinish:) name:AFNetworkingTaskDidCompleteNotification object:nil];
  68. self.activationDelay = kDefaultAFNetworkActivityManagerActivationDelay;
  69. self.completionDelay = kDefaultAFNetworkActivityManagerCompletionDelay;
  70. return self;
  71. }
  72. - (void)dealloc {
  73. [[NSNotificationCenter defaultCenter] removeObserver:self];
  74. [_activationDelayTimer invalidate];
  75. [_completionDelayTimer invalidate];
  76. }
  77. - (void)setEnabled:(BOOL)enabled {
  78. _enabled = enabled;
  79. if (enabled == NO) {
  80. [self setCurrentState:AFNetworkActivityManagerStateNotActive];
  81. }
  82. }
  83. - (void)setNetworkingActivityActionWithBlock:(void (^)(BOOL networkActivityIndicatorVisible))block {
  84. self.networkActivityActionBlock = block;
  85. }
  86. - (BOOL)isNetworkActivityOccurring {
  87. @synchronized(self) {
  88. return self.activityCount > 0;
  89. }
  90. }
  91. - (void)setNetworkActivityIndicatorVisible:(BOOL)networkActivityIndicatorVisible {
  92. if (_networkActivityIndicatorVisible != networkActivityIndicatorVisible) {
  93. @synchronized(self) {
  94. _networkActivityIndicatorVisible = networkActivityIndicatorVisible;
  95. }
  96. if (self.networkActivityActionBlock) {
  97. self.networkActivityActionBlock(networkActivityIndicatorVisible);
  98. } else {
  99. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:networkActivityIndicatorVisible];
  100. }
  101. }
  102. }
  103. - (void)incrementActivityCount {
  104. @synchronized(self) {
  105. self.activityCount++;
  106. }
  107. dispatch_async(dispatch_get_main_queue(), ^{
  108. [self updateCurrentStateForNetworkActivityChange];
  109. });
  110. }
  111. - (void)decrementActivityCount {
  112. @synchronized(self) {
  113. self.activityCount = MAX(_activityCount - 1, 0);
  114. }
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. [self updateCurrentStateForNetworkActivityChange];
  117. });
  118. }
  119. - (void)networkRequestDidStart:(NSNotification *)notification {
  120. if ([AFNetworkRequestFromNotification(notification) URL]) {
  121. [self incrementActivityCount];
  122. }
  123. }
  124. - (void)networkRequestDidFinish:(NSNotification *)notification {
  125. if ([AFNetworkRequestFromNotification(notification) URL]) {
  126. [self decrementActivityCount];
  127. }
  128. }
  129. #pragma mark - Internal State Management
  130. - (void)setCurrentState:(AFNetworkActivityManagerState)currentState {
  131. @synchronized(self) {
  132. if (_currentState != currentState) {
  133. _currentState = currentState;
  134. switch (currentState) {
  135. case AFNetworkActivityManagerStateNotActive:
  136. [self cancelActivationDelayTimer];
  137. [self cancelCompletionDelayTimer];
  138. [self setNetworkActivityIndicatorVisible:NO];
  139. break;
  140. case AFNetworkActivityManagerStateDelayingStart:
  141. [self startActivationDelayTimer];
  142. break;
  143. case AFNetworkActivityManagerStateActive:
  144. [self cancelCompletionDelayTimer];
  145. [self setNetworkActivityIndicatorVisible:YES];
  146. break;
  147. case AFNetworkActivityManagerStateDelayingEnd:
  148. [self startCompletionDelayTimer];
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. - (void)updateCurrentStateForNetworkActivityChange {
  155. if (self.enabled) {
  156. switch (self.currentState) {
  157. case AFNetworkActivityManagerStateNotActive:
  158. if (self.isNetworkActivityOccurring) {
  159. [self setCurrentState:AFNetworkActivityManagerStateDelayingStart];
  160. }
  161. break;
  162. case AFNetworkActivityManagerStateDelayingStart:
  163. //No op. Let the delay timer finish out.
  164. break;
  165. case AFNetworkActivityManagerStateActive:
  166. if (!self.isNetworkActivityOccurring) {
  167. [self setCurrentState:AFNetworkActivityManagerStateDelayingEnd];
  168. }
  169. break;
  170. case AFNetworkActivityManagerStateDelayingEnd:
  171. if (self.isNetworkActivityOccurring) {
  172. [self setCurrentState:AFNetworkActivityManagerStateActive];
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. - (void)startActivationDelayTimer {
  179. self.activationDelayTimer = [NSTimer
  180. timerWithTimeInterval:self.activationDelay target:self selector:@selector(activationDelayTimerFired) userInfo:nil repeats:NO];
  181. [[NSRunLoop mainRunLoop] addTimer:self.activationDelayTimer forMode:NSRunLoopCommonModes];
  182. }
  183. - (void)activationDelayTimerFired {
  184. if (self.networkActivityOccurring) {
  185. [self setCurrentState:AFNetworkActivityManagerStateActive];
  186. } else {
  187. [self setCurrentState:AFNetworkActivityManagerStateNotActive];
  188. }
  189. }
  190. - (void)startCompletionDelayTimer {
  191. [self.completionDelayTimer invalidate];
  192. self.completionDelayTimer = [NSTimer timerWithTimeInterval:self.completionDelay target:self selector:@selector(completionDelayTimerFired) userInfo:nil repeats:NO];
  193. [[NSRunLoop mainRunLoop] addTimer:self.completionDelayTimer forMode:NSRunLoopCommonModes];
  194. }
  195. - (void)completionDelayTimerFired {
  196. [self setCurrentState:AFNetworkActivityManagerStateNotActive];
  197. }
  198. - (void)cancelActivationDelayTimer {
  199. [self.activationDelayTimer invalidate];
  200. }
  201. - (void)cancelCompletionDelayTimer {
  202. [self.completionDelayTimer invalidate];
  203. }
  204. @end
  205. #endif