UIRefreshControl+AFNetworking.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. UIRefreshControl *refreshControl = self.refreshControl;
  60. if (task.state == NSURLSessionTaskStateRunning) {
  61. [refreshControl beginRefreshing];
  62. [notificationCenter addObserver:self selector:@selector(af_beginRefreshing) name:AFNetworkingTaskDidResumeNotification object:task];
  63. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidCompleteNotification object:task];
  64. [notificationCenter addObserver:self selector:@selector(af_endRefreshing) name:AFNetworkingTaskDidSuspendNotification object:task];
  65. } else {
  66. [refreshControl endRefreshing];
  67. }
  68. }
  69. }
  70. #pragma mark -
  71. - (void)af_beginRefreshing {
  72. dispatch_async(dispatch_get_main_queue(), ^{
  73. [self.refreshControl beginRefreshing];
  74. });
  75. }
  76. - (void)af_endRefreshing {
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. [self.refreshControl endRefreshing];
  79. });
  80. }
  81. #pragma mark -
  82. - (void)dealloc {
  83. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  84. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  85. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  86. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  87. }
  88. @end
  89. #endif