Nav apraksta

UIProgressView+AFNetworking.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if (task.state == NSURLSessionTaskStateCompleted) {
  46. return;
  47. }
  48. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  49. [task addObserver:self forKeyPath:@"countOfBytesSent" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesSentContext];
  50. [self af_setUploadProgressAnimated:animated];
  51. }
  52. - (void)setProgressWithDownloadProgressOfTask:(NSURLSessionDownloadTask *)task
  53. animated:(BOOL)animated
  54. {
  55. if (task.state == NSURLSessionTaskStateCompleted) {
  56. return;
  57. }
  58. [task addObserver:self forKeyPath:@"state" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  59. [task addObserver:self forKeyPath:@"countOfBytesReceived" options:(NSKeyValueObservingOptions)0 context:AFTaskCountOfBytesReceivedContext];
  60. [self af_setDownloadProgressAnimated:animated];
  61. }
  62. #pragma mark - NSKeyValueObserving
  63. - (void)observeValueForKeyPath:(NSString *)keyPath
  64. ofObject:(id)object
  65. change:(__unused NSDictionary *)change
  66. context:(void *)context
  67. {
  68. if (context == AFTaskCountOfBytesSentContext || context == AFTaskCountOfBytesReceivedContext) {
  69. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesSent))]) {
  70. if ([object countOfBytesExpectedToSend] > 0) {
  71. dispatch_async(dispatch_get_main_queue(), ^{
  72. [self setProgress:[object countOfBytesSent] / ([object countOfBytesExpectedToSend] * 1.0f) animated:self.af_uploadProgressAnimated];
  73. });
  74. }
  75. }
  76. if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) {
  77. if ([object countOfBytesExpectedToReceive] > 0) {
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. [self setProgress:[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f) animated:self.af_downloadProgressAnimated];
  80. });
  81. }
  82. }
  83. if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) {
  84. if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) {
  85. @try {
  86. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))];
  87. if (context == AFTaskCountOfBytesSentContext) {
  88. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesSent))];
  89. }
  90. if (context == AFTaskCountOfBytesReceivedContext) {
  91. [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))];
  92. }
  93. }
  94. @catch (NSException * __unused exception) {}
  95. }
  96. }
  97. }
  98. }
  99. @end
  100. #endif