猎豆优选

UIProgressView+AFNetworking.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // UIProgressView+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 "UIProgressView+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if TARGET_OS_IOS || TARGET_OS_TV
  24. #import "AFURLSessionManager.h"
  25. static void * AFTaskCountOfBytesSentContext = &AFTaskCountOfBytesSentContext;
  26. static void * AFTaskCountOfBytesReceivedContext = &AFTaskCountOfBytesReceivedContext;
  27. #pragma mark -
  28. @implementation UIProgressView (AFNetworking)
  29. - (BOOL)af_uploadProgressAnimated {
  30. return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_uploadProgressAnimated)) boolValue];
  31. }
  32. - (void)af_setUploadProgressAnimated:(BOOL)animated {
  33. objc_setAssociatedObject(self, @selector(af_uploadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  34. }
  35. - (BOOL)af_downloadProgressAnimated {
  36. return [(NSNumber *)objc_getAssociatedObject(self, @selector(af_downloadProgressAnimated)) boolValue];
  37. }
  38. - (void)af_setDownloadProgressAnimated:(BOOL)animated {
  39. objc_setAssociatedObject(self, @selector(af_downloadProgressAnimated), @(animated), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  40. }
  41. #pragma mark -
  42. - (void)setProgressWithUploadProgressOfTask:(NSURLSessionUploadTask *)task
  43. animated:(BOOL)animated
  44. {
  45. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  46. [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  47. [self af_setUploadProgressAnimated:animated];
  48. }
  49. - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
  50. animated:(BOOL)animated
  51. {
  52. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  53. [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  54. [self af_setDownloadProgressAnimated:animated];
  55. }
  56. #pragma mark - NSKeyValueObserving
  57. - (void)observeValueForKeyPath:(NSString *)keyPath
  58. ofObject:(id)object
  59. change:(__unused NSDictionary *)change
  60. context:(void *)context
  61. {
  62. if (context == AFTaskCountOfBytesSentContext || context == AFTaskCountOfBytesReceivedContext) {
  63. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) {
  64. if ([object countOfBytesExpectedToSend] > 0) {
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. [self setProgress:[object countOfBytesSent] / ([object countOfBytesExpectedToSend] * 1.0f) animated:self.af_uploadProgressAnimated];
  67. });
  68. }
  69. }
  70. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
  71. if ([object countOfBytesExpectedToReceive] > 0) {
  72. dispatch_async(dispatch_get_main_queue(), ^{
  73. [self setProgress:[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f) animated:self.af_downloadProgressAnimated];
  74. });
  75. }
  76. }
  77. if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) {
  78. if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) {
  79. @try {
  80. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))];
  81. if (context == AFTaskCountOfBytesSentContext) {
  82. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))];
  83. }
  84. if (context == AFTaskCountOfBytesReceivedContext) {
  85. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
  86. }
  87. }
  88. @catch (NSException * __unused exception) {}
  89. }
  90. }
  91. }
  92. }
  93. @end
  94. #endif