forked from ReactiveCocoa/ReactiveCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ReactiveCocoa#1251 from TimurBK/master
Added RAC support for UIImagePickerController.
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ReactiveCocoaFramework/ReactiveCocoa/UIImagePickerController+RACSignalSupport.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// UIImagePickerController+RACSignalSupport.h | ||
// ReactiveCocoa | ||
// | ||
// Created by Timur Kuchkarov on 28.03.14. | ||
// Copyright (c) 2014 GitHub. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@class RACDelegateProxy; | ||
@class RACSignal; | ||
|
||
@interface UIImagePickerController (RACSignalSupport) | ||
|
||
/// A delegate proxy which will be set as the receiver's delegate when any of the | ||
/// methods in this category are used. | ||
@property (nonatomic, strong, readonly) RACDelegateProxy *rac_delegateProxy; | ||
|
||
/// Creates a signal for every new selected image. | ||
/// | ||
/// When this method is invoked, the `rac_delegateProxy` will become the | ||
/// receiver's delegate. Any previous delegate will become the -[RACDelegateProxy | ||
/// rac_proxiedDelegate], so that it receives any messages that the proxy doesn't | ||
/// know how to handle. Setting the receiver's `delegate` afterward is considered | ||
/// undefined behavior. | ||
/// | ||
/// Returns a signal which will send the dictionary with info for the selected image. | ||
/// Caller is responsible for picker controller dismissal. The signal will complete | ||
/// itself when the receiver is deallocated or when user cancels selection. | ||
- (RACSignal *)rac_imageSelectedSignal; | ||
|
||
@end |
53 changes: 53 additions & 0 deletions
53
ReactiveCocoaFramework/ReactiveCocoa/UIImagePickerController+RACSignalSupport.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// UIImagePickerController+RACSignalSupport.m | ||
// ReactiveCocoa | ||
// | ||
// Created by Timur Kuchkarov on 28.03.14. | ||
// Copyright (c) 2014 GitHub. All rights reserved. | ||
// | ||
|
||
#import "UIImagePickerController+RACSignalSupport.h" | ||
#import "RACDelegateProxy.h" | ||
#import "RACSignal+Operations.h" | ||
#import "NSObject+RACDeallocating.h" | ||
#import "NSObject+RACDescription.h" | ||
#import <objc/runtime.h> | ||
|
||
@implementation UIImagePickerController (RACSignalSupport) | ||
|
||
static void RACUseDelegateProxy(UIImagePickerController *self) { | ||
if (self.delegate == self.rac_delegateProxy) return; | ||
|
||
self.rac_delegateProxy.rac_proxiedDelegate = self.delegate; | ||
self.delegate = (id)self.rac_delegateProxy; | ||
} | ||
|
||
- (RACDelegateProxy *)rac_delegateProxy { | ||
RACDelegateProxy *proxy = objc_getAssociatedObject(self, _cmd); | ||
if (proxy == nil) { | ||
proxy = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UIImagePickerControllerDelegate)]; | ||
objc_setAssociatedObject(self, _cmd, proxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
|
||
return proxy; | ||
} | ||
|
||
- (RACSignal *)rac_imageSelectedSignal { | ||
RACSignal *pickerCancelledSignal = [[self.rac_delegateProxy | ||
signalForSelector:@selector(imagePickerControllerDidCancel:)] | ||
merge:self.rac_willDeallocSignal]; | ||
|
||
RACSignal *imagePickerSignal = [[[[self.rac_delegateProxy | ||
signalForSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)] | ||
reduceEach:^(UIImagePickerController *pickerController, NSDictionary *userInfo) { | ||
return userInfo; | ||
}] | ||
takeUntil:pickerCancelledSignal] | ||
setNameWithFormat:@"%@ -rac_imageSelectedSignal", [self rac_description]]; | ||
|
||
RACUseDelegateProxy(self); | ||
|
||
return imagePickerSignal; | ||
} | ||
|
||
@end |
51 changes: 51 additions & 0 deletions
51
ReactiveCocoaFramework/ReactiveCocoaTests/UIImagePickerControllerRACSupportSpec.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// UIImagePickerControllerRACSupportSpec.m | ||
// ReactiveCocoa | ||
// | ||
// Created by Timur Kuchkarov on 17.04.14. | ||
// Copyright (c) 2014 GitHub, Inc. All rights reserved. | ||
// | ||
|
||
#import "UIImagePickerController+RACSignalSupport.h" | ||
#import "RACSignal.h" | ||
|
||
SpecBegin(UIImagePickerControllerRACSupport) | ||
|
||
describe(@"UIImagePickerController", ^{ | ||
__block UIImagePickerController *imagePicker; | ||
|
||
beforeEach(^{ | ||
imagePicker = [[UIImagePickerController alloc] init]; | ||
expect(imagePicker).notTo.beNil(); | ||
}); | ||
|
||
it(@"sends the user info dictionary after confirmation", ^{ | ||
__block NSDictionary *selectedImageUserInfo = nil; | ||
[imagePicker.rac_imageSelectedSignal subscribeNext:^(NSDictionary *userInfo) { | ||
selectedImageUserInfo = userInfo; | ||
}]; | ||
|
||
NSDictionary *info = @{ | ||
UIImagePickerControllerMediaType: @"public.image", | ||
UIImagePickerControllerMediaMetadata: @{} | ||
}; | ||
[imagePicker.delegate imagePickerController:imagePicker didFinishPickingMediaWithInfo:info]; | ||
expect(selectedImageUserInfo).to.equal(info); | ||
}); | ||
|
||
it(@"cancels image picking process", ^{ | ||
__block BOOL didSend = NO; | ||
__block BOOL didComplete = NO; | ||
[imagePicker.rac_imageSelectedSignal subscribeNext:^(NSDictionary *userInfo) { | ||
didSend = YES; | ||
} completed:^{ | ||
didComplete = YES; | ||
}]; | ||
|
||
[imagePicker.delegate imagePickerControllerDidCancel:imagePicker]; | ||
expect(didSend).to.beFalsy(); | ||
expect(didComplete).to.beTruthy(); | ||
}); | ||
}); | ||
|
||
SpecEnd |