説明なし

UIActivityIndicatorView+AFNetworking.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #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.activityIndicatorView startAnimating];
  64. } else {
  65. [self.activityIndicatorView stopAnimating];
  66. }
  67. #pragma clang diagnostic pop
  68. [notificationCenter addObserver:self selector:@selector(af_startAnimating) name:AFNetworkingTaskDidResumeNotification object:task];
  69. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidCompleteNotification object:task];
  70. [notificationCenter addObserver:self selector:@selector(af_stopAnimating) name:AFNetworkingTaskDidSuspendNotification object:task];
  71. }
  72. }
  73. }
  74. #pragma mark -
  75. - (void)af_startAnimating {
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. #pragma clang diagnostic push
  78. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  79. [self.activityIndicatorView startAnimating];
  80. #pragma clang diagnostic pop
  81. });
  82. }
  83. - (void)af_stopAnimating {
  84. dispatch_async(dispatch_get_main_queue(), ^{
  85. #pragma clang diagnostic push
  86. #pragma clang diagnostic ignored "-Wreceiver-is-weak"
  87. [self.activityIndicatorView stopAnimating];
  88. #pragma clang diagnostic pop
  89. });
  90. }
  91. #pragma mark -
  92. - (void)dealloc {
  93. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  94. [notificationCenter removeObserver:self name:AFNetworkingTaskDidCompleteNotification object:nil];
  95. [notificationCenter removeObserver:self name:AFNetworkingTaskDidResumeNotification object:nil];
  96. [notificationCenter removeObserver:self name:AFNetworkingTaskDidSuspendNotification object:nil];
  97. }
  98. @end
  99. #endif