-
Notifications
You must be signed in to change notification settings - Fork 13
/
Scene.js
158 lines (130 loc) · 4.31 KB
/
Scene.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react';
import PropTypes from 'prop-types';
import { View, ViewPropTypes } from 'react-native';
import FBNavigator from './FBNavigator';
import { isIphoneX } from './utils';
export default class Scene extends React.Component {
componentDidMount() {
const { delegate } = this.props;
if (delegate) {
const navigationDelegate = delegate.constructor.navigationDelegate;
if (navigationDelegate && navigationDelegate.id) {
const navigationDelegateCopy = Object.assign({},
delegate.constructor.navigationDelegate);
const navigationEvents = ['onSceneWillFocus', 'onSceneDidFocus'];
navigationEvents.forEach((eventName) =>
this._addListener(eventName, delegate));
if (delegate.onSceneWillFocus) {
delegate.onSceneWillFocus();
}
setTimeout(() => {
const events = delegate.constructor.navigationDelegate._events;
navigationDelegateCopy._events = events;
if (events && events.length) {
this._events = events.slice();
this._events.forEach((eventName) => {
this._addListener(eventName, delegate);
});
}
}, 300);
const delegateUnmountHandler = delegate.componentWillUnmount;
Object.defineProperty(delegate, 'componentWillUnmount', {
writable: false,
configurable: true,
enumerable: false,
value: () => {
delegateUnmountHandler && delegateUnmountHandler.bind(delegate)();
navigationEvents.forEach((eventName) =>
this._removeListener(eventName, delegate));
const events = this._events;
if (events && events.length) {
events.forEach((eventName) => this._removeListener(eventName));
}
this._events = null;
// restore prev state here because we might change it
Object.keys(navigationDelegateCopy).forEach((key) => {
if (navigationDelegate[key]) {
navigationDelegate[key] = navigationDelegateCopy[key];
} else {
delete navigationDelegate[key];
}
});
},
});
}
}
}
componentDidUpdate() {
const { delegate } = this.props;
if (delegate) {
const navigationDelegate = delegate.constructor.navigationDelegate;
const events = navigationDelegate._events;
setTimeout(() => {
if (events && events.length) {
events.forEach((eventName) => {
if (!this[`_${eventName}Sub`]) {
this._addListener(eventName, delegate);
}
});
}
}, 300);
}
}
_addListener = (eventName, delegate) => {
const navigationContext = delegate.props.navigator.navigationContext;
const delegateId = delegate.constructor.navigationDelegate.id;
const formatEventName = (eventName) => {
if (eventName === 'onSceneWillFocus') {
return 'willfocus';
} else if (eventName === 'onSceneDidFocus') {
return 'didfocus';
}
return eventName;
};
this[`_${eventName}Sub`] = navigationContext.addListener(
formatEventName(eventName),
({ data: { route, e } }) => {
if (route.component.navigationDelegate &&
delegateId === route.component.navigationDelegate.id &&
delegate[eventName]) {
delegate[eventName](e);
}
},
);
}
_removeListener = (eventName) => {
this[`_${eventName}Sub`].remove();
delete this[`_${eventName}Sub`];
}
render() {
return (
<View
style={[
{
flex: 1,
paddingTop: this.props.paddingTop ?
Scene.navBarHeight :
0,
},
this.props.style,
]}
>
{this.props.children}
</View>
);
}
static propTypes = {
style: ViewPropTypes.style,
paddingTop: PropTypes.bool,
delegate: (props, propName) => {
if (props[propName] && !(props[propName] instanceof React.Component)) {
return new Error('Scene delegate should be instance of React.Component');
}
},
};
static defaultProps = {
paddingTop: true,
};
static navBarHeight = FBNavigator.NavigationBar.Styles.General.TotalNavHeight +
(isIphoneX() ? 24 : 0);
}