-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
297 lines (278 loc) · 7.27 KB
/
App.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Demo app showing how react-navigation works with Apple TV.
*
* @flow
*/
import * as React from 'react';
import {
TouchableOpacity,
StyleSheet,
View,
Text,
TVMenuControl,
Platform,
} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
/**
* We use this Button component instead of the one from react-native,
* because we need to pass in `isTVSelectable` and have more control
* over styling. We also use magnification instead of opacity to highlight
* which button is focused.
*/
function Button({title, onPress, onFocus, isTVSelectable}) {
return (
<TouchableOpacity
tvParallaxProperties={{magnification: 1.2}}
activeOpacity={Platform.isTVOS ? 1.0 : 0.5}
onPress={() => onPress()}
onFocus={() => onFocus()}
isTVSelectable={isTVSelectable}>
<Text style={styles.button}>{title}</Text>
</TouchableOpacity>
);
}
Button.defaultProps = {
isTVSelectable: true,
};
/**
* Demo button for the upper right that doesn't do anything.
* It is styled to invisibly extend close to the center of the screen,
* so that it will be reachable by the tvOS focus engine without having
* to implement a TVFocusGuideView. We use tint instead of magnification
* or opacity to highlight this button when focused.
*/
function InfoButton() {
const [infoButtonFocused, setInfoButtonFocused] = React.useState(false);
return (
<TouchableOpacity
activeOpacity={1.0}
style={styles.infoButtonContainer}
onPress={() => alert('Info')} /* eslint-disable-line no-alert */
onFocus={() => {
console.log('Focus: Info button');
setInfoButtonFocused(true);
}}
onBlur={() => setInfoButtonFocused(false)}>
<View style={styles.spacer} />
<Text
style={
infoButtonFocused
? styles.headerBackTitleFocused
: styles.headerBackTitle
}>
Info
</Text>
</TouchableOpacity>
);
}
/**
* The home screen.
*/
function HomeScreen({navigation}) {
// Set up focus and blur listeners to set our state properly;
// isFocused === true when this screen is shown.
const [isFocused, setIsFocused] = React.useState(false);
React.useEffect(
() =>
navigation.addListener('focus', () => {
setIsFocused(true);
}),
[navigation],
);
React.useEffect(
() =>
navigation.addListener('blur', () => {
setIsFocused(false);
}),
[navigation],
);
return (
<View style={styles.container}>
<Text style={styles.title}>Home Screen</Text>
<Button
isTVSelectable={
isFocused
} /* button is removed from focus engine
when screen is not visible */
onPress={() => navigation.navigate('Screen1')}
onFocus={() => console.log('Focus: Home')}
title="Go to Screen 1"
/>
</View>
);
}
/**
* Component for other screens in the stack navigator.
*/
function Screen({route, navigation}) {
// Same code as above for detecting when screens are visible
const [isFocused, setIsFocused] = React.useState(false);
React.useEffect(
() =>
navigation.addListener('focus', () => {
setIsFocused(true);
}),
[navigation],
);
React.useEffect(
() =>
navigation.addListener('blur', () => {
setIsFocused(false);
}),
[navigation],
);
// Hacky code to get which screen this is
const index = parseInt(route.name.substring(6, 7), 10);
// Enable the menu key when this screen appears.
// In the cleanup method, disable the menu key again if we are
// navigating back to the home screen; this way the menu key
// will exit the app properly if pressed in the home screen.
React.useEffect(() => {
TVMenuControl.enableTVMenuKey();
return () => {
if (index === 1) {
TVMenuControl.disableTVMenuKey();
}
};
});
const nextIndex = index + 1;
const buttonTitle = `Go to Screen ${nextIndex}`;
return (
<View style={styles.container}>
<Text style={styles.title}>{`Screen ${index}`}</Text>
<View style={styles.buttonContainer}>
{/* Put some buttons on the screen just for fun */}
<Button
isTVSelectable={isFocused}
onPress={() => {}}
onFocus={() => console.log(`Focus: ${route.name}, Button 1`)}
title="Button 1"
/>
<Button
isTVSelectable={isFocused}
onPress={() => {}}
onFocus={() => console.log(`Focus: ${route.name}, Button 2`)}
title="Button 2"
/>
<Button
isTVSelectable={isFocused}
onPress={() => {}}
onFocus={() => console.log(`Focus: ${route.name}, Button 3`)}
title="Button 3"
/>
</View>
{index < 3 /* Don't push more than 3 screens on the stack */ ? (
<Button
isTVSelectable={isFocused}
onPress={() => navigation.navigate(`Screen${nextIndex}`)}
onFocus={() =>
console.log(`Focus: ${route.name}, ${index}, nav button`)
}
title={buttonTitle}
/>
) : null}
</View>
);
}
/**
* Construct the options for the navigation header.
*/
let infoButtonFocused = false;
const headerOptions = (title) => {
return {
title,
headerBackTitleStyle: styles.headerBackTitle,
headerStyle: styles.header,
headerTitleContainerStyle: styles.headerTitleContainer,
headerTitleStyle: styles.headerTitle,
headerRight: () => <InfoButton />,
};
};
const Stack = createStackNavigator();
/**
* Standard construction of a stack navigator.
*/
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
options={headerOptions('Home')}
/>
<Stack.Screen
name="Screen1"
component={Screen}
options={headerOptions('Screen 1')}
/>
<Stack.Screen
name="Screen2"
component={Screen}
options={headerOptions('Screen 2')}
/>
<Stack.Screen
name="Screen3"
component={Screen}
options={headerOptions('Screen 3')}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
const colors = {
blue: '#0070d2',
white: '#ffffff',
yellow: '#ffdd00',
};
const scale = Platform.isTVOS ? 1.0 : 0.5;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 50*scale,
},
header: {
height: 150*scale,
backgroundColor: colors.blue,
},
headerTitle: {
fontSize: 50*scale,
color: colors.white,
},
headerBackTitle: {
fontSize: 40*scale,
color: colors.white,
},
headerBackTitleFocused: {
fontSize: 40*scale,
color: colors.yellow,
},
headerTitleContainer: {
marginTop: 0,
},
button: {
fontSize: 50*scale,
color: colors.blue,
margin: 50*scale,
},
buttonContainer: {
flexDirection: 'row',
margin: 100*scale,
},
infoButtonContainer: {
backgroundColor: 'transparent',
width: 700*scale,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
spacer: {
flex: 1,
},
});