|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import "RCTViewController.h" |
| 9 | + |
| 10 | +#import <objc/runtime.h> |
| 11 | + |
| 12 | +@interface RCTViewControllerAppearanceState : NSObject |
| 13 | + |
| 14 | +@property (nonatomic, strong, readonly) NSHashTable<id<RCTViewControllerAppearanceListener>> *listeners; |
| 15 | +@property (nonatomic, assign) BOOL visible; |
| 16 | + |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation RCTViewControllerAppearanceState |
| 20 | + |
| 21 | +- (instancetype)init |
| 22 | +{ |
| 23 | + if (self = [super init]) { |
| 24 | + _listeners = [NSHashTable weakObjectsHashTable]; |
| 25 | + } |
| 26 | + return self; |
| 27 | +} |
| 28 | + |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation UIViewController (RCTViewControllerAppearance) |
| 32 | + |
| 33 | +- (RCTViewControllerAppearanceState *)reactViewControllerAppearanceState |
| 34 | +{ |
| 35 | + RCTViewControllerAppearanceState *state = |
| 36 | + objc_getAssociatedObject(self, @selector(reactViewControllerAppearanceState)); |
| 37 | + if (!state) { |
| 38 | + state = [RCTViewControllerAppearanceState new]; |
| 39 | + objc_setAssociatedObject( |
| 40 | + self, @selector(reactViewControllerAppearanceState), state, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 41 | + } |
| 42 | + return state; |
| 43 | +} |
| 44 | + |
| 45 | +- (BOOL)reactViewControllerIsVisible |
| 46 | +{ |
| 47 | + return [self reactViewControllerAppearanceState].visible; |
| 48 | +} |
| 49 | + |
| 50 | +- (void)reactAddViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener |
| 51 | +{ |
| 52 | + RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; |
| 53 | + [state.listeners addObject:listener]; |
| 54 | + |
| 55 | + if (state.visible && [listener respondsToSelector:@selector(reactViewControllerDidAppear:animated:)]) { |
| 56 | + [listener reactViewControllerDidAppear:self animated:NO]; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +- (void)reactRemoveViewControllerAppearanceListener:(id<RCTViewControllerAppearanceListener>)listener |
| 61 | +{ |
| 62 | + [[self reactViewControllerAppearanceState].listeners removeObject:listener]; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)reactNotifyViewControllerDidAppear:(BOOL)animated |
| 66 | +{ |
| 67 | + RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; |
| 68 | + state.visible = YES; |
| 69 | + |
| 70 | + for (id<RCTViewControllerAppearanceListener> listener in state.listeners.allObjects) { |
| 71 | + if ([listener respondsToSelector:@selector(reactViewControllerDidAppear:animated:)]) { |
| 72 | + [listener reactViewControllerDidAppear:self animated:animated]; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +- (void)reactNotifyViewControllerDidDisappear:(BOOL)animated |
| 78 | +{ |
| 79 | + RCTViewControllerAppearanceState *state = [self reactViewControllerAppearanceState]; |
| 80 | + state.visible = NO; |
| 81 | + |
| 82 | + for (id<RCTViewControllerAppearanceListener> listener in state.listeners.allObjects) { |
| 83 | + if ([listener respondsToSelector:@selector(reactViewControllerDidDisappear:animated:)]) { |
| 84 | + [listener reactViewControllerDidDisappear:self animated:animated]; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +@end |
| 90 | + |
| 91 | +@implementation RCTViewController |
| 92 | + |
| 93 | +- (void)viewDidAppear:(BOOL)animated |
| 94 | +{ |
| 95 | + [super viewDidAppear:animated]; |
| 96 | + [self reactNotifyViewControllerDidAppear:animated]; |
| 97 | +} |
| 98 | + |
| 99 | +- (void)viewDidDisappear:(BOOL)animated |
| 100 | +{ |
| 101 | + [super viewDidDisappear:animated]; |
| 102 | + [self reactNotifyViewControllerDidDisappear:animated]; |
| 103 | +} |
| 104 | + |
| 105 | +@end |
0 commit comments