Last active
April 16, 2020 02:00
-
-
Save frehulfd/443e94454736af9763b1 to your computer and use it in GitHub Desktop.
WKInterfaceController+Reactive
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
@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 |
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
#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
License?