RACSubscriber+AFProgressCallbacks.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // RACSubscriber+AFProgressCallbacks.m
  3. // Reactive AFNetworking Example
  4. //
  5. // Created by Robert Widmann on 3/28/13.
  6. // Copyright (c) 2013 CodaFi. All rights reserved.
  7. //
  8. #ifdef RAFN_EXPERIMENTAL_PROGRESS_SUPPORT
  9. #import "RACSubscriber+AFProgressCallbacks.h"
  10. #import <objc/runtime.h>
  11. static char RAFNProgress_Block_Key;
  12. @interface RACSubscriber (AFInternalProgressCallbacks)
  13. @property (nonatomic, copy) void (^_progress)(float progress);
  14. @end
  15. @implementation RACSubscriber (AFProgressCallbacks)
  16. + (instancetype)subscriberWithNext:(void (^)(id x))next progress:(void (^)(float progress))progress error:(void (^)(NSError *error))error completed:(void (^)(void))completed {
  17. RACSubscriber *subscriber = [self subscriberWithNext:next error:error completed:completed];
  18. subscriber._progress = progress;
  19. return subscriber;
  20. }
  21. - (void)set_progress:(void (^)(float))_progress {
  22. objc_setAssociatedObject(self, &RAFNProgress_Block_Key, _progress, OBJC_ASSOCIATION_COPY);
  23. }
  24. - (void (^)(float))_progress {
  25. return objc_getAssociatedObject(self, &RAFNProgress_Block_Key);
  26. }
  27. - (void)sendProgress:(float)p {
  28. [[self performSelector:@selector(disposable)] dispose];
  29. if (self._progress != NULL) self._progress(p);
  30. }
  31. @end
  32. @implementation RACSignal (RAFNProgressSubscriptions)
  33. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress next:(void (^)(id x))nextBlock {
  34. NSParameterAssert(progress != NULL);
  35. NSParameterAssert(nextBlock != NULL);
  36. RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock progress:progress error:NULL completed:NULL];
  37. return [self subscribe:o];
  38. }
  39. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress next:(void (^)(id x))nextBlock completed:(void (^)(void))completedBlock {
  40. NSParameterAssert(progress != NULL);
  41. NSParameterAssert(nextBlock != NULL);
  42. NSParameterAssert(completedBlock != NULL);
  43. RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock progress:progress error:NULL completed:completedBlock];
  44. return [self subscribe:o];
  45. }
  46. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress next:(void (^)(id x))nextBlock error:(void (^)(NSError *error))errorBlock completed:(void (^)(void))completedBlock {
  47. NSParameterAssert(progress != NULL);
  48. NSParameterAssert(nextBlock != NULL);
  49. NSParameterAssert(errorBlock != NULL);
  50. NSParameterAssert(completedBlock != NULL);
  51. RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock progress:progress error:errorBlock completed:completedBlock];
  52. return [self subscribe:o];
  53. }
  54. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress completed:(void (^)(void))completedBlock {
  55. NSParameterAssert(progress != NULL);
  56. NSParameterAssert(completedBlock != NULL);
  57. RACSubscriber *o = [RACSubscriber subscriberWithNext:NULL progress:progress error:NULL completed:completedBlock];
  58. return [self subscribe:o];
  59. }
  60. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress next:(void (^)(id x))nextBlock error:(void (^)(NSError *error))errorBlock {
  61. NSParameterAssert(progress != NULL);
  62. NSParameterAssert(nextBlock != NULL);
  63. NSParameterAssert(errorBlock != NULL);
  64. RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock progress:progress error:errorBlock completed:NULL];
  65. return [self subscribe:o];
  66. }
  67. - (RACDisposable *)subscribeProgress:(void (^)(float progress))progress error:(void (^)(NSError *error))errorBlock completed:(void (^)(void))completedBlock {
  68. NSParameterAssert(progress != NULL);
  69. NSParameterAssert(errorBlock != NULL);
  70. NSParameterAssert(completedBlock != NULL);
  71. RACSubscriber *o = [RACSubscriber subscriberWithNext:NULL progress:progress error:errorBlock completed:completedBlock];
  72. return [self subscribe:o];
  73. }
  74. @end
  75. @implementation RACSubject (RAFNProgressSending)
  76. - (void)sendProgress:(float)value {
  77. void (^subscriberBlock)(id<RACSubscriber> subscriber) = ^(id<RACSubscriber> subscriber){
  78. [(RACSubscriber*)subscriber sendProgress:value];
  79. };
  80. [self performSelector:@selector(performBlockOnEachSubscriber:) withObject:subscriberBlock];
  81. }
  82. @end
  83. #endif