Skip to content

Instantly share code, notes, and snippets.

@frehulfd
Last active April 16, 2020 02:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frehulfd/443e94454736af9763b1 to your computer and use it in GitHub Desktop.
Save frehulfd/443e94454736af9763b1 to your computer and use it in GitHub Desktop.
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
@AquaGeek
Copy link

AquaGeek commented Aug 7, 2017

License?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment