react-native-vision-camera/example/src/Media.tsx

157 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-02-19 11:29:29 -07:00
import React, { useCallback, useMemo, useState } from 'react';
import { StyleSheet, View, Image, ActivityIndicator, PermissionsAndroid, Platform } from 'react-native';
import { Navigation, NavigationFunctionComponent, OptionsModalPresentationStyle } from 'react-native-navigation';
2021-02-19 10:44:05 -07:00
import Video from 'react-native-video';
2021-02-19 11:29:29 -07:00
import { SAFE_AREA_PADDING } from './Constants';
2021-02-19 11:08:38 -07:00
import { useIsForeground } from './hooks/useIsForeground';
import { useIsScreenFocused } from './hooks/useIsScreenFocused';
2021-02-19 11:29:29 -07:00
import { PressableOpacity } from './views/PressableOpacity';
import IonIcon from 'react-native-vector-icons/Ionicons';
import { Alert } from 'react-native';
import CameraRoll from '@react-native-community/cameraroll';
2021-02-19 11:54:30 -07:00
import { StatusBarBlurBackground } from './views/StatusBarBlurBackground';
2021-02-19 10:44:05 -07:00
interface MediaProps {
2021-02-20 09:07:10 -07:00
path: string;
type: 'video' | 'photo';
2021-02-19 10:44:05 -07:00
}
2021-02-19 11:29:29 -07:00
const requestSavePermission = async (): Promise<boolean> => {
2021-02-20 09:07:10 -07:00
if (Platform.OS !== 'android') return true;
2021-02-19 11:29:29 -07:00
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
let hasPermission = await PermissionsAndroid.check(permission);
if (!hasPermission) {
const permissionRequestResult = await PermissionsAndroid.request(permission);
2021-02-20 09:07:10 -07:00
hasPermission = permissionRequestResult === 'granted';
2021-02-19 11:29:29 -07:00
}
return hasPermission;
2021-02-20 09:07:10 -07:00
};
2021-02-19 11:29:29 -07:00
2021-02-19 10:44:05 -07:00
export const Media: NavigationFunctionComponent<MediaProps> = ({ componentId, type, path }) => {
2021-02-19 11:29:29 -07:00
const [hasMediaLoaded, setHasMediaLoaded] = useState(false);
2021-02-19 11:08:38 -07:00
const isForeground = useIsForeground();
const isScreenFocused = useIsScreenFocused(componentId);
const isVideoPaused = !isForeground || !isScreenFocused;
2021-02-20 09:07:10 -07:00
const [savingState, setSavingState] = useState<'none' | 'saving' | 'saved'>('none');
2021-02-19 11:08:38 -07:00
2021-02-19 10:44:05 -07:00
const onClosePressed = useCallback(() => {
Navigation.dismissModal(componentId);
}, [componentId]);
2021-02-19 11:29:29 -07:00
const onMediaLoadEnd = useCallback(() => {
2021-02-20 09:07:10 -07:00
console.log('media has loaded.');
2021-02-19 11:29:29 -07:00
setHasMediaLoaded(true);
}, []);
const onSavePressed = useCallback(async () => {
try {
2021-02-20 09:07:10 -07:00
setSavingState('saving');
2021-02-19 11:29:29 -07:00
const hasPermission = await requestSavePermission();
if (!hasPermission) {
Alert.alert('Permission denied!', 'Vision Camera does not have permission to save the media to your camera roll.');
return;
}
await CameraRoll.save(`file://${path}`, {
type: type,
});
2021-02-20 09:07:10 -07:00
setSavingState('saved');
2021-02-19 11:29:29 -07:00
} catch (e) {
2021-02-20 09:07:10 -07:00
setSavingState('none');
Alert.alert('Failed to save!', `An unexpected error occured while trying to save your ${type}. ${e?.message ?? JSON.stringify(e)}`);
2021-02-19 11:29:29 -07:00
}
}, [path, type]);
const source = useMemo(() => ({ uri: `file://${path}` }), [path]);
2021-02-20 09:07:10 -07:00
const screenStyle = useMemo(() => ({ opacity: hasMediaLoaded ? 1 : 0 }), [hasMediaLoaded]);
2021-02-19 10:44:05 -07:00
return (
2021-02-19 11:29:29 -07:00
<View style={[styles.container, screenStyle]}>
2021-02-20 09:07:10 -07:00
{type === 'photo' && <Image source={source} style={StyleSheet.absoluteFill} resizeMode="cover" onLoadEnd={onMediaLoadEnd} />}
{type === 'video' && (
<Video
2021-02-19 11:08:38 -07:00
source={source}
style={StyleSheet.absoluteFill}
paused={isVideoPaused}
resizeMode="cover"
posterResizeMode="cover"
allowsExternalPlayback={false}
automaticallyWaitsToMinimizeStalling={false}
disableFocus={true}
repeat={true}
useTextureView={false}
controls={false}
playWhenInactive={true}
2021-02-19 11:29:29 -07:00
ignoreSilentSwitch="ignore"
2021-02-20 09:07:10 -07:00
onReadyForDisplay={onMediaLoadEnd}
/>
2021-02-19 11:08:38 -07:00
)}
2021-02-19 10:44:05 -07:00
2021-02-20 09:07:10 -07:00
<PressableOpacity style={styles.closeButton} onPress={onClosePressed}>
<IonIcon name="close" size={35} color="white" style={styles.icon} />
2021-02-19 11:29:29 -07:00
</PressableOpacity>
2021-02-20 09:07:10 -07:00
<PressableOpacity style={styles.saveButton} onPress={onSavePressed} disabled={savingState !== 'none'}>
{savingState === 'none' && <IonIcon name="download" size={35} color="white" style={styles.icon} />}
{savingState === 'saved' && <IonIcon name="checkmark" size={35} color="white" style={styles.icon} />}
{savingState === 'saving' && <ActivityIndicator color="white" />}
2021-02-19 11:29:29 -07:00
</PressableOpacity>
2021-02-19 11:54:30 -07:00
<StatusBarBlurBackground />
2021-02-19 10:44:05 -07:00
</View>
);
2021-02-20 09:07:10 -07:00
};
2021-02-19 10:44:05 -07:00
2021-02-19 11:29:29 -07:00
Media.options = {
modal: {
swipeToDismiss: false,
},
modalPresentationStyle: OptionsModalPresentationStyle.overCurrentContext,
animations: {
showModal: {
waitForRender: true,
enabled: false,
},
dismissModal: {
enabled: false,
},
},
layout: {
2021-02-20 09:07:10 -07:00
backgroundColor: 'transparent',
componentBackgroundColor: 'transparent',
2021-02-19 11:29:29 -07:00
},
};
2021-02-19 10:44:05 -07:00
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
closeButton: {
position: 'absolute',
2021-02-19 11:29:29 -07:00
top: SAFE_AREA_PADDING.paddingTop,
left: SAFE_AREA_PADDING.paddingLeft,
width: 40,
height: 40,
},
saveButton: {
position: 'absolute',
bottom: SAFE_AREA_PADDING.paddingBottom,
left: SAFE_AREA_PADDING.paddingLeft,
2021-02-19 10:44:05 -07:00
width: 40,
height: 40,
},
2021-02-19 11:29:29 -07:00
icon: {
2021-02-20 09:07:10 -07:00
textShadowColor: 'black',
2021-02-19 11:29:29 -07:00
textShadowOffset: {
height: 0,
width: 0,
},
textShadowRadius: 1,
},
2021-02-19 10:44:05 -07:00
});