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-05-06 06:11:55 -06:00
|
|
|
import Video, { LoadError, OnLoadData } 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';
|
2021-05-11 04:59:05 -06:00
|
|
|
import { useIsScreenFocussed } from './hooks/useIsScreenFocused';
|
|
|
|
import { PressableOpacity } from 'react-native-pressable-opacity';
|
2021-02-19 11:29:29 -07:00
|
|
|
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-26 09:34:28 -07:00
|
|
|
import type { NativeSyntheticEvent } from 'react-native';
|
|
|
|
import type { ImageLoadEventData } from 'react-native';
|
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;
|
2021-02-20 09:12:21 -07:00
|
|
|
if (permission == null) return false;
|
2021-02-19 11:29:29 -07:00
|
|
|
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-26 09:34:28 -07:00
|
|
|
const isVideoOnLoadEvent = (event: OnLoadData | NativeSyntheticEvent<ImageLoadEventData>): event is OnLoadData =>
|
|
|
|
'duration' in event && 'naturalSize' in event;
|
|
|
|
|
2021-05-11 04:59:05 -06:00
|
|
|
export const MediaPage: 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();
|
2021-05-11 04:59:05 -06:00
|
|
|
const isScreenFocused = useIsScreenFocussed(componentId);
|
2021-02-19 11:08:38 -07:00
|
|
|
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-26 09:34:28 -07:00
|
|
|
const onMediaLoad = useCallback((event: OnLoadData | NativeSyntheticEvent<ImageLoadEventData>) => {
|
|
|
|
if (isVideoOnLoadEvent(event)) {
|
|
|
|
console.log(
|
|
|
|
`Video loaded. Size: ${event.naturalSize.width}x${event.naturalSize.height} (${event.naturalSize.orientation}, ${event.duration} seconds)`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(`Image loaded. Size: ${event.nativeEvent.source.width}x${event.nativeEvent.source.height}`);
|
|
|
|
}
|
|
|
|
}, []);
|
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);
|
|
|
|
}, []);
|
2021-05-06 06:11:55 -06:00
|
|
|
const onMediaLoadError = useCallback((error: LoadError) => {
|
|
|
|
console.log(`failed to load media: ${JSON.stringify(error)}`);
|
|
|
|
}, []);
|
2021-02-19 11:29:29 -07:00
|
|
|
|
|
|
|
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-09-22 05:58:59 -06:00
|
|
|
const message = e instanceof Error ? e.message : JSON.stringify(e);
|
2021-02-20 09:07:10 -07:00
|
|
|
setSavingState('none');
|
2021-09-22 05:58:59 -06:00
|
|
|
Alert.alert('Failed to save!', `An unexpected error occured while trying to save your ${type}. ${message}`);
|
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-26 09:34:28 -07:00
|
|
|
{type === 'photo' && (
|
|
|
|
<Image source={source} style={StyleSheet.absoluteFill} resizeMode="cover" onLoadEnd={onMediaLoadEnd} onLoad={onMediaLoad} />
|
|
|
|
)}
|
2021-02-20 09:07:10 -07:00
|
|
|
{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-26 09:34:28 -07:00
|
|
|
onLoad={onMediaLoad}
|
2021-05-06 06:11:55 -06:00
|
|
|
onError={onMediaLoadError}
|
2021-02-20 09:07:10 -07:00
|
|
|
/>
|
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-05-11 04:59:05 -06:00
|
|
|
MediaPage.options = {
|
2021-02-19 11:29:29 -07:00
|
|
|
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
|
|
|
});
|