UIActivityIndicatorView+AFNetworking.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // UIActivityIndicatorView+AFNetworking.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 "UIActivityIndicatorView+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if TARGET_OS_IOS || TARGET_OS_TV
  24. #import "AFURLSessionManager.h"
  25. @interface AFActivityIndicatorViewNotificationObserver : NSObject
  26. @property (readonly, nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
  27. - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView;
  28. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task;
  29. @end
  30. @implementation UIActivityIndicatorView (AFNetworking)
  31. - (AFActivityIndicatorViewNotificationObserver *)af_notificationObserver {
  32. AFActivityIndicatorViewNotificationObserver *notificationObserver = objc_getAssociatedObject(self, @selector(af_notificationObserver));
  33. if (notificationObserver == nil) {
  34. notificationObserver = [[AFActivityIndicatorViewNotificationObserver alloc] initWithActivityIndicatorView:self];
  35. objc_setAssociatedObject(self, @selector(af_notificationObserver), notificationObserver, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  36. }
  37. return notificationObserver;
  38. }
  39. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
  40. [[self af_notificationObserver] setAnimatingWithStateOfTask:task];
  41. }
  42. @end
  43. @implementation AFActivityIndicatorViewNotificationObserver
  44. - (instancetype)initWithActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView
  45. {
  46. self = [super init];
  47. if (self) {
  48. _activityIndicatorView = activityIndicatorView;
  49. }
  50. return self;
  51. }
  52. - (void)setAnimatingWithStateOfTask:(NSURLSessionTask *)task {
  53. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  54. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  55. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  56. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  57. if (task) {
  58. if (task.state != NSURLSessionTaskStateCompleted) {
  59. UIActivityIndicatorView *activityIndicatorView = self.activityIndicatorView;
  60. if (task.state == NSURLSessionTaskStateRunning) {
  61. [activityIndicatorView startAnimating];
  62. } else {
  63. [activityIndicatorView stopAnimating];
  64. }
  65. [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingTaskDidResumeNotification object:task];
  66. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidCompleteNotification object:task];
  67. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidSuspendNotification object:task];
  68. }
  69. }
  70. }
  71. #pragma mark -
  72. - (void)af_startAnimating {
  73. dispatch_async(dispatch_get_main_queue(), ^{
  74. [self.activityIndicatorView startAnimating];
  75. });
  76. }
  77. - (void)af_stopAnimating {
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [self.activityIndicatorView stopAnimating];
  80. });
  81. }
  82. #pragma mark -
  83. - (void)dealloc {
  84. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  85. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  86. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  87. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  88. }
  89. @end
  90. #endif