Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
296 lines (266 loc) · 7.94 KB
/
Copy pathApp.tsx
File metadata and controls
296 lines (266 loc) · 7.94 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
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
import { useEvent } from 'expo';
import * as AppleAuthentication from 'expo-apple-authentication';
import { BlurView } from 'expo-blur';
import { CameraView, useCameraPermissions, CameraType } from 'expo-camera';
import { Image } from 'expo-image';
import { LinearGradient } from 'expo-linear-gradient';
import { useVideoPlayer, VideoView } from 'expo-video';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import {
Button,
SafeAreaView,
StatusBar,
StyleSheet,
Text,
View,
ScrollView,
TouchableOpacity,
Platform,
Alert,
Appearance,
PlatformColor,
Image as RNImage,
} from 'react-native';
function randomColor() {
return '#' + ((Math.random() * 0xffffff) << 0).toString(16).padStart(6, '0');
}
function randomGradientColors() {
return Array(3).fill(0).map(randomColor) as unknown as readonly [string, string, string];
}
export default function App() {
const [colorScheme, setColorScheme] = useState(Appearance.getColorScheme());
const isFabricEnabled = global.nativeFabricUIManager != null;
useEffect(() => {
const listener = Appearance.addChangeListener((preferences) => {
setColorScheme(preferences.colorScheme);
});
return listener.remove;
}, []);
return (
<SafeAreaView
style={[styles.container, { backgroundColor: colorScheme === 'light' ? '#fff' : '#161b22' }]}>
<ScrollView>
<Text style={[styles.text, { marginVertical: 10 }]}>
isFabricEnabled: {isFabricEnabled + ''}
</Text>
<ImageExample />
<LinearGradientExample />
{Platform.OS === 'ios' && <BlurExample />}
<VideoExample />
<CameraExample />
<AppleAuthenticationExample />
</ScrollView>
</SafeAreaView>
);
}
export function ImageExample() {
const [seed] = useState(100 + Math.round(Math.random() * 100));
const uri = `https://picsum.photos/id/${seed}/1000/1000`;
return (
<View style={styles.exampleContainer}>
<Image style={styles.image} source={{ uri }} />
<Text>
Image from RN core to test TurboModuleDelegate on iOS. If you see the first image but not
the second, that's a bug.
</Text>
<RNImage style={styles.image} source={{ uri }} />
</View>
);
}
export function LinearGradientExample() {
const [mounted, setMounted] = useState(true);
const [colors, setColors] = useState(randomGradientColors());
const toggleMounted = useCallback(() => setMounted(!mounted), [mounted]);
const randomizeColors = useCallback(() => setColors(randomGradientColors()), [colors]);
return (
<View style={styles.exampleContainer}>
<View style={styles.gradient}>
{mounted && <LinearGradient style={{ flex: 1 }} colors={colors} end={{ x: 0.5, y: 1.0 }} />}
</View>
<View style={styles.buttons}>
<Button title={mounted ? 'Unmount view' : 'Mount view'} onPress={toggleMounted} />
<Button title="Randomize colors" onPress={randomizeColors} disabled={!mounted} />
</View>
</View>
);
}
export function BlurExample() {
const uri = 'https://source.unsplash.com/random';
const text = "Hello, I'm blurring contents underneath!";
return (
<View style={[styles.exampleContainer, styles.blurExample]}>
<Image style={styles.blurImage} source={{ uri }} />
<BlurView intensity={100} style={styles.blurContainer}>
<Text style={styles.text}>{text}</Text>
</BlurView>
<BlurView intensity={80} tint="light" style={styles.blurContainer}>
<Text style={styles.text}>{text}</Text>
</BlurView>
<BlurView intensity={20} tint="dark" style={styles.blurContainer}>
<Text style={[styles.text, { color: '#fff' }]}>{text}</Text>
</BlurView>
</View>
);
}
export function VideoExample() {
const videoSource =
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4';
const player = useVideoPlayer(videoSource, (player) => {
player.loop = true;
});
const status = useEvent(player, 'playingChange', { isPlaying: player.playing });
const togglePlaying = useCallback(() => {
if (status.isPlaying) {
player.pause();
} else {
player.play();
}
}, [status.isPlaying]);
return (
<View style={[styles.exampleContainer, styles.videoExample]}>
<VideoView style={styles.video} player={player} allowsPictureInPicture />
<View style={styles.buttons}>
<Button title={status.isPlaying ? 'Pause' : 'Play'} onPress={togglePlaying} />
</View>
</View>
);
}
export function CameraExample() {
const [cameraPermissionStatus, requestCameraPermission] = useCameraPermissions();
const camera = useRef<CameraView>(null);
const [cameraType, setCameraType] = useState<CameraType>('back');
const takePicture = useCallback(async () => {
const result = await camera.current.takePictureAsync({
quality: 0.7,
});
alert(JSON.stringify(result, null, 2));
}, []);
const reverse = useCallback(() => {
setCameraType(cameraType === 'back' ? 'front' : 'back');
}, [cameraType]);
const onCameraReady = useCallback(() => {
console.log('Camera is ready!');
}, []);
if (!cameraPermissionStatus) {
requestCameraPermission();
return null;
}
return (
<View style={styles.exampleContainer}>
<CameraView
ref={camera}
style={styles.camera}
facing={cameraType}
onCameraReady={onCameraReady}>
<View style={styles.cameraShutterButtonContainer}>
<TouchableOpacity style={styles.cameraShutterButton} onPress={takePicture} />
</View>
</CameraView>
<View style={styles.buttons}>
<Button title="Take picture" onPress={takePicture} />
<Button
title={cameraType === 'back' ? 'Switch to front' : 'Switch to back'}
onPress={reverse}
/>
</View>
</View>
);
}
export function AppleAuthenticationExample() {
const signIn = useCallback(async () => {
try {
await AppleAuthentication.signInAsync({
requestedScopes: [
AppleAuthentication.AppleAuthenticationScope.FULL_NAME,
AppleAuthentication.AppleAuthenticationScope.EMAIL,
],
state: 'this-is-a-test',
});
} catch (error) {
Alert.alert(error.code, error.message);
}
}, []);
return (
<View style={styles.exampleContainer}>
<View style={{ flex: 1, alignItems: 'center' }}>
<AppleAuthentication.AppleAuthenticationButton
buttonStyle={AppleAuthentication.AppleAuthenticationButtonStyle.BLACK}
buttonType={AppleAuthentication.AppleAuthenticationButtonType.CONTINUE}
cornerRadius={10}
onPress={signIn}
style={{ width: 250, height: 44, margin: 15 }}
/>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: StatusBar.currentHeight,
},
exampleContainer: {
padding: 20,
borderTopWidth: StyleSheet.hairlineWidth,
borderStyle: 'solid',
borderColor: '#242c39',
},
image: {
flex: 1,
height: 200,
},
gradient: {
height: 200,
},
blurExample: {
height: 200,
},
blurImage: {
...StyleSheet.absoluteFill,
},
blurContainer: {
flex: 1,
justifyContent: 'center',
},
text: {
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
...Platform.select({
ios: { color: PlatformColor('labelColor') },
}),
},
videoExample: {
justifyContent: 'center',
},
video: {
alignSelf: 'center',
width: '100%',
height: 200,
},
buttons: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
camera: {
height: 500,
backgroundColor: 'red',
justifyContent: 'flex-end',
alignItems: 'center',
},
cameraShutterButtonContainer: {
width: 70,
height: 70,
margin: 20,
padding: 3,
borderRadius: 35,
borderWidth: 4,
borderColor: '#fff9',
},
cameraShutterButton: {
flex: 1,
borderRadius: 35,
backgroundColor: '#fff',
},
});