No Description

UIRefreshControl+AFNetworking.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // UIRefreshControl+AFNetworking.m
  2. //
  3. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  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 TARGET_OS_IOS
  25. #import "AFURLSessionManager.h"
  26. @interface AFRefreshControlNotificationObserver : NSObject
  27. @property (readonly, nonatomic, weak) UIRefreshControl *refreshControl;
  28. - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl;
  29. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task;
  30. @end
  31. @implementation UIRefreshControl (AFNetworking)
  32. - (AFRefreshControlNotificationObserver *)af_notificationObserver {
  33. AFRefreshControlNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
  34. if (notificationObserver == nil) {
  35. notificationObserver = [[AFRefreshControlNotificationObserver alloc] initWithActivityRefreshControl:self];
  36. objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  37. }
  38. return notificationObserver;
  39. }
  40. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
  41. [[self af_notificationObserver] setRefreshingWithStateOfTask:task];
  42. }
  43. @end
  44. @implementation AFRefreshControlNotificationObserver
  45. - (instancetype)initWithActivityRefreshControl:(UIRefreshControl *)refreshControl
  46. {
  47. self = [super init];
  48. if (self) {
  49. _refreshControl = refreshControl;
  50. }
  51. return self;
  52. }
  53. - (void)setRefreshingWithStateOfTask:(NSURLSessionTask *)task {
  54. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  55. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  56. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  57. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  58. if (task) {
  59. #pragma clang diagnostic push
  60. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  61. #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
  62. if (task.state == NSURLSessionTaskStateRunning) {
  63. [self.refreshControl beginRefreshing];
  64. [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task];
  65. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task];
  66. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task];
  67. } else {
  68. [self.refreshControl endRefreshing];
  69. }
  70. #pragma clang diagnostic pop
  71. }
  72. }
  73. #pragma mark -
  74. - (void)af_beginRefreshing {
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. #pragma clang diagnostic push
  77. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  78. [self.refreshControl beginRefreshing];
  79. #pragma clang diagnostic pop
  80. });
  81. }
  82. - (void)af_endRefreshing {
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. #pragma clang diagnostic push
  85. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  86. [self.refreshControl endRefreshing];
  87. #pragma clang diagnostic pop
  88. });
  89. }
  90. #pragma mark -
  91. - (void)dealloc {
  92. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  93. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  94. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  95. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  96. }
  97. @end
  98. #endif