-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathUIViewController+React.m
More file actions
89 lines (70 loc) · 2.63 KB
/
Copy pathUIViewController+React.m
File metadata and controls
89 lines (70 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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:)]) {
[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