UIActivityIndicatorView+AFNetworking.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // UIActivityIndicatorView+AFNetworking.m
  2. // Copyright (c) 2011–2015 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 "UIActivityIndicatorView+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  24. #import "AFHTTPRequestOperation.h"
  25. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  26. #import "AFURLSessionManager.h"
  27. #endif
  28. @interface AFActivityIndicatorViewNotificationObserver : NSObject
  29. @property (readonly, nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
  30. - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView;
  31. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  32. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task;
  33. #endif
  34. - (void)setAnimatingWithStateOfOperation:(AFURLConnectionOperation *)operation;
  35. @end
  36. @implementation UIActivityIndicatorView (AFNetworking)
  37. - (AFActivityIndicatorViewNotificationObserver *)af_notificationObserver {
  38. AFActivityIndicatorViewNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
  39. if (notificationObserver == nil) {
  40. notificationObserver = [[AFActivityIndicatorViewNotificationObserver alloc] initWithActivityIndicatorView:self];
  41. objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  42. }
  43. return notificationObserver;
  44. }
  45. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  46. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
  47. [[self af_notificationObserver] setAnimatingWithStateOfTask:task];
  48. }
  49. #endif
  50. - (void)setAnimatingWithStateOfOperation:(AFURLConnectionOperation *)operation {
  51. [[self af_notificationObserver] setAnimatingWithStateOfOperation:operation];
  52. }
  53. @end
  54. @implementation AFActivityIndicatorViewNotificationObserver
  55. - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView
  56. {
  57. self = [super init];
  58. if (self) {
  59. _activityIndicatorView = activityIndicatorView;
  60. }
  61. return self;
  62. }
  63. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  64. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
  65. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  66. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  67. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  68. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  69. if (task) {
  70. if (task.state != NSURLSessionTaskStateCompleted) {
  71. #pragma clang diagnostic push
  72. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  73. #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
  74. if (task.state == NSURLSessionTaskStateRunning) {
  75. [self.activityIndicatorView startAnimating];
  76. } else {
  77. [self.activityIndicatorView stopAnimating];
  78. }
  79. #pragma clang diagnostic pop
  80. [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingTaskDidResumeNotification object:task];
  81. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidCompleteNotification object:task];
  82. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidSuspendNotification object:task];
  83. }
  84. }
  85. }
  86. #endif
  87. #pragma mark -
  88. - (void)setAnimatingWithStateOfOperation:(AFURLConnectionOperation *)operation {
  89. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  90. [notificationCenter removeObserver:self name:AFNetworkingOperationDidStartNotification object:nil];
  91. [notificationCenter removeObserver:self name:AFNetworkingOperationDidFinishNotification object:nil];
  92. if (operation) {
  93. if (![operation isFinished]) {
  94. #pragma clang diagnostic push
  95. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  96. #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
  97. if ([operation isExecuting]) {
  98. [self.activityIndicatorView startAnimating];
  99. } else {
  100. [self.activityIndicatorView stopAnimating];
  101. }
  102. #pragma clang diagnostic pop
  103. [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingOperationDidStartNotification object:operation];
  104. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingOperationDidFinishNotification object:operation];
  105. }
  106. }
  107. }
  108. #pragma mark -
  109. - (void)af_startAnimating {
  110. dispatch_async(dispatch_get_main_queue(), ^{
  111. #pragma clang diagnostic push
  112. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  113. [self.activityIndicatorView startAnimating];
  114. #pragma clang diagnostic pop
  115. });
  116. }
  117. - (void)af_stopAnimating {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. #pragma clang diagnostic push
  120. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  121. [self.activityIndicatorView stopAnimating];
  122. #pragma clang diagnostic pop
  123. });
  124. }
  125. #pragma mark -
  126. - (void)dealloc {
  127. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  128. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  129. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  130. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  131. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  132. #endif
  133. [notificationCenter removeObserver:self name:AFNetworkingOperationDidStartNotification object:nil];
  134. [notificationCenter removeObserver:self name:AFNetworkingOperationDidFinishNotification object:nil];
  135. }
  136. @end
  137. #endif