import { useColors } from './Provider'; import React, { ComponentProps } from 'react'; import { StyleSheet, View } from 'react-native'; import { TouchableRipple } from 'react-native-paper'; import { MaterialCommunityIcons } from '@expo/vector-icons'; import * as ImagePicker from 'expo-image-picker'; import ImagePreview from './ImagePreview'; import type { ImageInfo } from 'expo-image-picker/build/ImagePicker.types'; function getIconOffset( iconPosition: 'bottomRight' | 'bottomLeft' | 'topRight' | 'topLeft', rounded: boolean, width: number ) { const horizontalOffset = rounded ? width / 4 - 30 : -10; const verticalOffset = rounded ? 0 : -10; switch (iconPosition) { case 'topRight': return { right: horizontalOffset, top: verticalOffset }; case 'topLeft': return { left: horizontalOffset, top: verticalOffset }; case 'bottomRight': return { right: horizontalOffset, bottom: verticalOffset }; case 'bottomLeft': return { left: horizontalOffset, bottom: verticalOffset }; } } interface Props { value: string; onChange: (imageResult: ImageInfo) => void; rounded?: boolean; iconPosition?: 'bottomRight' | 'bottomLeft' | 'topRight' | 'topLeft'; icon?: ComponentProps['name']; iconBackgroundColor?: string; iconColor?: string; isExpandable?: boolean; size: { width: number; height: number }; source?: 'camera' | 'gallery'; } const EditablePhoto = (props: Props) => { const { getColor } = useColors(); const { value, onChange, rounded = false, iconPosition = 'topRight', icon = 'pencil', iconBackgroundColor = getColor('primary500'), iconColor = 'white', isExpandable = true, size, source = 'camera', } = props; async function takePhoto() { const getImage = source === 'camera' ? ImagePicker.launchCameraAsync : ImagePicker.launchImageLibraryAsync; const result = await getImage({ mediaTypes: ImagePicker.MediaTypeOptions.Images, }); if (!result.cancelled) { onChange(result); } } const iconOffset = getIconOffset(iconPosition, rounded, size.width); return ( {value ? ( ) : ( )} ); }; export default EditablePhoto; const styles = StyleSheet.create({ pencil: { position: 'absolute', borderRadius: 100, zIndex: 2, padding: 10, }, });