Create a gist now

Instantly share code, notes, and snippets.

WKInterfaceController+Reactive
@import WatchKit;
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface WKInterfaceController (Reactive)
@property (nonatomic, readonly) RACSignal *activatedSignal;
@property (nonatomic, readonly) RACSignal *deactivatedSignal;
@property (nonatomic, readonly) RACSignal *didPresentControllerSignal;
@property (nonatomic, readonly) RACSignal *didPushControllerSignal;
/// Returns a signal that sends YES until the next time this interface is activated, then sends NO and completes.
/// One potential use for this would be to enable/disable a command for a sub interface that pops that interface
/// off the stack ONLY if the parent hasn't been shown again (like for Due Date changes, Comments, etc).
- (RACSignal *)yesUntilNextActivationSignal;
@end
#import "WKInterfaceController+Reactive.h"
#import <objc/runtime.h>
@interface WKInterfaceController (Reactive_Private)
@property (nonatomic, strong, readonly) RACSignal *activationSignal;
@end
@implementation WKInterfaceController (Reactive)
- (RACSignal *)activationSignal {
void* activationSignalKey = &activationSignalKey;
RACSignal *signal = objc_getAssociatedObject(self, activationSignalKey);
if (!signal) {
signal = [RACSignal merge:@[[[self rac_signalForSelector:@selector(willActivate)] mapReplace:@YES],
[[self rac_signalForSelector:@selector(didDeactivate)] mapReplace:@NO]]];
objc_setAssociatedObject(self, activationSignalKey, signal, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return signal;
}
- (RACSignal *)activatedSignal {
return [self.activationSignal ignore:@NO];
}
- (RACSignal *)deactivatedSignal {
return [self.activationSignal ignore:@YES];
}
- (RACSignal *)didPresentControllerSignal {
return [self rac_signalForSelector:@selector(presentControllerWithName:context:)];
}
- (RACSignal *)didPushControllerSignal {
return [self rac_signalForSelector:@selector(pushControllerWithName:context:)];
}
- (RACSignal *)yesUntilNextActivationSignal {
return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@YES];
return nil;
}] takeUntil:self.activatedSignal] concat:[RACSignal return:@NO]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment