UIRefreshControl+AFNetworking.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // UIRefreshControl+AFNetworking.m
  2. //
  3. // Copyright (c) 2014 AFNetworking (http://afnetworking.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "UIRefreshControl+AFNetworking.h"
  23. #import <objc/runtime.h>
  24. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  25. #import "AFHTTPRequestOperation.h"
  26. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  27. #import "AFURLSessionManager.h"
  28. #endif
  29. @interface AFRefreshControlNotificationObserver : NSObject
  30. @property (readonly, nonatomic, weak) UIRefreshControl *refreshControl;
  31. - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl;
  32. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  33. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;
  34. #endif
  35. - (void)setRefreshingWithStateOfOperation:(AFURLConnectionOperation *)operation;
  36. @end
  37. @implementation UIRefreshControl (AFNetworking)
  38. - (AFRefreshControlNotificationObserver *)af_notificationObserver {
  39. AFRefreshControlNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
  40. if (notificationObserver == nil) {
  41. notificationObserver = [[AFRefreshControlNotificationObserver alloc] initWithActivityRefreshControl:self];
  42. objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  43. }
  44. return notificationObserver;
  45. }
  46. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  47. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
  48. [[self af_notificationObserver] setRefreshingWithStateOfTask:task];
  49. }
  50. #endif
  51. - (void)setRefreshingWithStateOfOperation:(AFURLConnectionOperation *)operation {
  52. [[self af_notificationObserver] setRefreshingWithStateOfOperation:operation];
  53. }
  54. @end
  55. @implementation AFRefreshControlNotificationObserver
  56. - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl
  57. {
  58. self = [super init];
  59. if (self) {
  60. _refreshControl = refreshControl;
  61. }
  62. return self;
  63. }
  64. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  65. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
  66. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  67. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  68. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  69. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  70. if (task) {
  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.refreshControl beginRefreshing];
  76. [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task];
  77. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task];
  78. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task];
  79. } else {
  80. [self.refreshControl endRefreshing];
  81. }
  82. #pragma clang diagnostic pop
  83. }
  84. }
  85. #endif
  86. - (void)setRefreshingWithStateOfOperation:(AFURLConnectionOperation *)operation {
  87. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  88. [notificationCenter removeObserver:self name:AFNetworkingOperationDidStartNotification object:nil];
  89. [notificationCenter removeObserver:self name:AFNetworkingOperationDidFinishNotification object:nil];
  90. if (operation) {
  91. #pragma clang diagnostic push
  92. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  93. #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
  94. if (![operation isFinished]) {
  95. if ([operation isExecuting]) {
  96. [self.refreshControl beginRefreshing];
  97. } else {
  98. [self.refreshControl endRefreshing];
  99. }
  100. [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingOperationDidStartNotification object:operation];
  101. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingOperationDidFinishNotification object:operation];
  102. }
  103. #pragma clang diagnostic pop
  104. }
  105. }
  106. #pragma mark -
  107. - (void)af_beginRefreshing {
  108. dispatch_async(dispatch_get_main_queue(), ^{
  109. #pragma clang diagnostic push
  110. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  111. [self.refreshControl beginRefreshing];
  112. #pragma clang diagnostic pop
  113. });
  114. }
  115. - (void)af_endRefreshing {
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. #pragma clang diagnostic push
  118. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  119. [self.refreshControl endRefreshing];
  120. #pragma clang diagnostic pop
  121. });
  122. }
  123. #pragma mark -
  124. - (void)dealloc {
  125. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  126. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  127. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  128. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  129. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  130. #endif
  131. [notificationCenter removeObserver:self name:AFNetworkingOperationDidStartNotification object:nil];
  132. [notificationCenter removeObserver:self name:AFNetworkingOperationDidFinishNotification object:nil];
  133. }
  134. @end
  135. #endif