UIProgressView+AFNetworking.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // UIProgressView+AFNetworking.m
  2. // Copyright (c) 2011–2015 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 "UIProgressView+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  24. #import "AFURLConnectionOperation.h"
  25. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  26. #import "AFURLSessionManager.h"
  27. #endif
  28. static void * AFTaskCountOfBytesSentContext = &AFTaskCountOfBytesSentContext;
  29. static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedContext;
  30. @interface AFURLConnectionOperation (_UIProgressView)
  31. @property (readwrite, nonatomic, copy) void (^uploadProgress)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected);
  32. @property (readwrite, nonatomic, assign, setter = af_setUploadProgressAnimated:) BOOL af_uploadProgressAnimated;
  33. @property (readwrite, nonatomic, copy) void (^downloadProgress)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected);
  34. @property (readwrite, nonatomic, assign, setter = af_setDownloadProgressAnimated:) BOOL af_downloadProgressAnimated;
  35. @end
  36. @implementation AFURLConnectionOperation (_UIProgressView)
  37. @dynamic uploadProgress; // Implemented in AFURLConnectionOperation
  38. @dynamic af_uploadProgressAnimated;
  39. @dynamic downloadProgress; // Implemented in AFURLConnectionOperation
  40. @dynamic af_downloadProgressAnimated;
  41. @end
  42. #pragma mark -
  43. @implementation UIProgressView (AFNetworking)
  44. - (BOOL)af_uploadProgressAnimated {
  45. return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_uploadProgressAnimated)) boolValue];
  46. }
  47. - (void)af_setUploadProgressAnimated:(BOOL)animated {
  48. objc_setAssociatedObject(self, @selector(af_uploadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  49. }
  50. - (BOOL)af_downloadProgressAnimated {
  51. return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_downloadProgressAnimated)) boolValue];
  52. }
  53. - (void)af_setDownloadProgressAnimated:(BOOL)animated {
  54. objc_setAssociatedObject(self, @selector(af_downloadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  55. }
  56. #pragma mark -
  57. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  58. - (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task
  59. animated:(BOOL)animated
  60. {
  61. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  62. [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  63. [self af_setUploadProgressAnimated:animated];
  64. }
  65. - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
  66. animated:(BOOL)animated
  67. {
  68. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  69. [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  70. [self af_setDownloadProgressAnimated:animated];
  71. }
  72. #endif
  73. #pragma mark -
  74. - (void)setProgressWithUploadProgressOfOperation:(AFURLConnectionOperation *)operation
  75. animated:(BOOL)animated
  76. {
  77. __weak __typeof(self)weakSelf = self;
  78. void (^original)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) = [operation.uploadProgress copy];
  79. [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  80. if (original) {
  81. original(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  82. }
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. if (totalBytesExpectedToWrite > 0) {
  85. __strong __typeof(weakSelf)strongSelf = weakSelf;
  86. [strongSelf setProgress:(totalBytesWritten / (totalBytesExpectedToWrite * 1.0f)) animated:animated];
  87. }
  88. });
  89. }];
  90. }
  91. - (void)setProgressWithDownloadProgressOfOperation:(AFURLConnectionOperation *)operation
  92. animated:(BOOL)animated
  93. {
  94. __weak __typeof(self)weakSelf = self;
  95. void (^original)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) = [operation.downloadProgress copy];
  96. [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
  97. if (original) {
  98. original(bytesRead, totalBytesRead, totalBytesExpectedToRead);
  99. }
  100. dispatch_async(dispatch_get_main_queue(), ^{
  101. if (totalBytesExpectedToRead > 0) {
  102. __strong __typeof(weakSelf)strongSelf = weakSelf;
  103. [strongSelf setProgress:(totalBytesRead / (totalBytesExpectedToRead * 1.0f)) animated:animated];
  104. }
  105. });
  106. }];
  107. }
  108. #pragma mark - NSKeyValueObserving
  109. - (void)observeValueForKeyPath:(NSString *)keyPath
  110. ofObject:(id)object
  111. change:(__unused NSDictionary *)change
  112. context:(void *)context
  113. {
  114. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  115. if (context == AFTaskCountOfBytesSentContext || context == AFTaskCountOfBytesReceivedContext) {
  116. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) {
  117. if ([object countOfBytesExpectedToSend] > 0) {
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. [self setProgress:[object countOfBytesSent] / ([object countOfBytesExpectedToSend] * 1.0f) animated:self.af_uploadProgressAnimated];
  120. });
  121. }
  122. }
  123. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
  124. if ([object countOfBytesExpectedToReceive] > 0) {
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. [self setProgress:[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f) animated:self.af_downloadProgressAnimated];
  127. });
  128. }
  129. }
  130. if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) {
  131. if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) {
  132. @try {
  133. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))];
  134. if (context == AFTaskCountOfBytesSentContext) {
  135. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))];
  136. }
  137. if (context == AFTaskCountOfBytesReceivedContext) {
  138. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
  139. }
  140. }
  141. @catch (NSException * __unused exception) {}
  142. }
  143. }
  144. }
  145. #endif
  146. }
  147. @end
  148. #endif