-
Notifications
You must be signed in to change notification settings - Fork 25.2k
feat(ios): RCTViewControllerAppearanceListener #56598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @protocol RCTViewControllerAppearanceListener <NSObject> | ||
|
|
||
| @optional | ||
| - (void)reactViewControllerDidAppear:(UIViewController *)viewController animated:(BOOL)animated; | ||
| - (void)reactViewControllerDidDisappear:(UIViewController *)viewController animated:(BOOL)animated; | ||
|
|
||
| @end | ||
|
|
||
| @interface UIViewController (React) | ||
|
|
||
| @property (nonatomic, assign, readonly) BOOL reactViewControllerIsVisible; | ||
|
|
||
| - (void)reactAddViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener; | ||
| - (void)reactRemoveViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener; | ||
|
|
||
| /** | ||
| * Call from `viewDidAppear:` / `viewDidDisappear:` in UIViewController subclasses | ||
| * that want to notify registered React Native appearance listeners. | ||
| */ | ||
| - (void)reactNotifyViewControllerDidAppear:(BOOL)animated; | ||
| - (void)reactNotifyViewControllerDidDisappear:(BOOL)animated; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import "UIViewController+React.h" | ||
|
|
||
| #import <objc/runtime.h> | ||
|
|
||
| @interface RCTViewControllerAppearanceState : NSObject | ||
|
|
||
| @property (nonatomic, strong, readonly) NSHashTable<id<RCTViewControllerAppearanceListener>> *listeners; | ||
| @property (nonatomic, assign) BOOL visible; | ||
|
|
||
| @end | ||
|
|
||
| @implementation RCTViewControllerAppearanceState | ||
|
|
||
| - (instancetype)init | ||
| { | ||
| if (self = [super init]) { | ||
| _listeners = [NSHashTable weakObjectsHashTable]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| @implementation UIViewController (React) | ||
|
|
||
| - (RCTViewControllerAppearanceState *)reactViewControllerAppearanceState | ||
| { | ||
| RCTViewControllerAppearanceState *state = | ||
| objc_getAssociatedObject(self, @selector(reactViewControllerAppearanceState)); | ||
| if (!state) { | ||
| state = [RCTViewControllerAppearanceState new]; | ||
| objc_setAssociatedObject( | ||
| self, @selector(reactViewControllerAppearanceState), state, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
| } | ||
| return state; | ||
| } | ||
|
|
||
| - (BOOL)reactViewControllerIsVisible | ||
| { | ||
| return [self reactViewControllerAppearanceState].visible; | ||
| } | ||
|
|
||
| - (void)reactAddViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener | ||
| { | ||
| RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; | ||
| [state.listeners addObject:listener]; | ||
|
|
||
| if (state.visible && [listener respondsToSelector:@selector(reactViewControllerDidAppear:animated:)]) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check the selector? Guess that if it does not implement it then it will just fail silently right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why are we dispatching something in the registration method?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, or are we going to get
Are you worried that we might not catch all the events?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we need the check, otherwise we would get
I think there are two ways to handle this with listener patterns. The question is basically, what should happen if you attach a listener for something that has already happened. Should the listener callback be invoked immediately (the case here), or is it the callers responsibility to check for the condition first (and its for future events only).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // Edit: oops, only saw Riccardo's comment now |
||
| [listener reactViewControllerDidAppear:self animated:NO]; | ||
| } | ||
| } | ||
|
|
||
| - (void)reactRemoveViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener | ||
| { | ||
| [[self reactViewControllerAppearanceState].listeners removeObject:listener]; | ||
| } | ||
|
|
||
| - (void)reactNotifyViewControllerDidAppear:(BOOL)animated | ||
| { | ||
| RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; | ||
| state.visible = YES; | ||
|
|
||
| for (id<RCTViewControllerAppearanceListener> listener in state.listeners.allObjects) { | ||
| if ([listener respondsToSelector:@selector(reactViewControllerDidAppear:animated:)]) { | ||
| [listener reactViewControllerDidAppear:self animated:animated]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| - (void)reactNotifyViewControllerDidDisappear:(BOOL)animated | ||
| { | ||
| RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; | ||
| state.visible = NO; | ||
|
|
||
| for (id<RCTViewControllerAppearanceListener> listener in state.listeners.allObjects) { | ||
| if ([listener respondsToSelector:@selector(reactViewControllerDidDisappear:animated:)]) { | ||
| [listener reactViewControllerDidDisappear:self animated:animated]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're doing it in such weird way cause its category right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, unfortunately