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 {
|
|
|
|
path: string,
|
|
|
|
type: 'video' | 'photo'
|
|
|
|
}
|
|
|
|
|
2021-02-19 11:29:29 -07:00
|
|
|
const requestSavePermission = async (): Promise<boolean> => {
|
|
|
|
if (Platform.OS !== "android") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const permission = PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE;
|
|
|
|
let hasPermission = await PermissionsAndroid.check(permission);
|
|
|
|
if (!hasPermission) {
|
|
|
|
const permissionRequestResult = await PermissionsAndroid.request(permission);
|
|
|
|
hasPermission = permissionRequestResult === "granted";
|
|
|
|
}
|
|
|
|
return hasPermission;
|
|
|
|
}
|
|
|
|
|
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-19 11:29:29 -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(() => {
|
|
|
|
console.log(`media has loaded.`);
|
|
|
|
setHasMediaLoaded(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onSavePressed = useCallback(async () => {
|
|
|
|
try {
|
|
|
|
setSavingState("saving");
|
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
setSavingState("saved");
|
|
|
|
} catch (e) {
|
|
|
|
setSavingState("none");
|
|
|
|
Alert.alert(
|
|
|
|
"Failed to save!",
|
|
|
|
`An unexpected error occured while trying to save your ${type}. ${e?.message ?? JSON.stringify(e)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [path, type]);
|
|
|
|
|
|
|
|
const source = useMemo(() => ({ uri: `file://${path}` }), [path]);
|
|
|
|
|
|
|
|
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-19 11:08:38 -07:00
|
|
|
{type === "photo" && (
|
|
|
|
<Image
|
|
|
|
source={source}
|
|
|
|
style={StyleSheet.absoluteFill}
|
2021-02-19 11:29:29 -07:00
|
|
|
resizeMode="cover"
|
|
|
|
onLoadEnd={onMediaLoadEnd} />
|
2021-02-19 11:08:38 -07:00
|
|
|
)}
|
|
|
|
{type === "video" && (
|
|
|
|
<Video 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"
|
|
|
|
onReadyForDisplay={onMediaLoadEnd} />
|
2021-02-19 11:08:38 -07:00
|
|
|
)}
|
2021-02-19 10:44:05 -07:00
|
|
|
|
2021-02-19 11:29:29 -07:00
|
|
|
<PressableOpacity
|
|
|
|
style={styles.closeButton}
|
|
|
|
onPress={onClosePressed}>
|
|
|
|
<IonIcon
|
|
|
|
name="close"
|
|
|
|
size={35}
|
|
|
|
color="white"
|
|
|
|
style={styles.icon} />
|
|
|
|
</PressableOpacity>
|
|
|
|
|
|
|
|
<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" />
|
|
|
|
)}
|
|
|
|
</PressableOpacity>
|
2021-02-19 11:54:30 -07:00
|
|
|
|
|
|
|
<StatusBarBlurBackground />
|
2021-02-19 10:44:05 -07:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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: {
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
componentBackgroundColor: "transparent",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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: {
|
|
|
|
textShadowColor: "black",
|
|
|
|
textShadowOffset: {
|
|
|
|
height: 0,
|
|
|
|
width: 0,
|
|
|
|
},
|
|
|
|
textShadowRadius: 1,
|
|
|
|
},
|
2021-02-19 10:44:05 -07:00
|
|
|
});
|