import React, {useMemo, useState} from 'react' import { LayoutChangeEvent, StyleProp, StyleSheet, TouchableOpacity, View, ViewStyle, } from 'react-native' import {Image, ImageStyle} from 'expo-image' import {Dimensions} from 'lib/media/types' export const DELAY_PRESS_IN = 500 export type ImageLayoutGridType = number export function ImageLayoutGrid({ type, uris, onPress, onLongPress, onPressIn, style, }: { type: ImageLayoutGridType uris: string[] onPress?: (index: number) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void style?: StyleProp }) { const [containerInfo, setContainerInfo] = useState() const onLayout = (evt: LayoutChangeEvent) => { setContainerInfo({ width: evt.nativeEvent.layout.width, height: evt.nativeEvent.layout.height, }) } return ( {containerInfo ? ( ) : undefined} ) } function ImageLayoutGridInner({ type, uris, onPress, onLongPress, onPressIn, containerInfo, }: { type: ImageLayoutGridType uris: string[] onPress?: (index: number) => void onLongPress?: (index: number) => void onPressIn?: (index: number) => void containerInfo: Dimensions }) { const size1 = useMemo(() => { if (type === 3) { const size = (containerInfo.width - 10) / 3 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } else { const size = (containerInfo.width - 5) / 2 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } }, [type, containerInfo]) const size2 = React.useMemo(() => { if (type === 3) { const size = ((containerInfo.width - 10) / 3) * 2 + 5 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } else { const size = (containerInfo.width - 5) / 2 return {width: size, height: size, resizeMode: 'cover', borderRadius: 4} } }, [type, containerInfo]) if (type === 2) { return ( onPress?.(0)} onPressIn={() => onPressIn?.(0)} onLongPress={() => onLongPress?.(0)}> onPress?.(1)} onPressIn={() => onPressIn?.(1)} onLongPress={() => onLongPress?.(1)}> ) } if (type === 3) { return ( onPress?.(0)} onPressIn={() => onPressIn?.(0)} onLongPress={() => onLongPress?.(0)}> onPress?.(1)} onPressIn={() => onPressIn?.(1)} onLongPress={() => onLongPress?.(1)}> onPress?.(2)} onPressIn={() => onPressIn?.(2)} onLongPress={() => onLongPress?.(2)}> ) } if (type === 4) { return ( onPress?.(0)} onPressIn={() => onPressIn?.(0)} onLongPress={() => onLongPress?.(0)}> onPress?.(2)} onPressIn={() => onPressIn?.(2)} onLongPress={() => onLongPress?.(2)}> onPress?.(1)} onPressIn={() => onPressIn?.(1)} onLongPress={() => onLongPress?.(1)}> onPress?.(3)} onPressIn={() => onPressIn?.(3)} onLongPress={() => onLongPress?.(3)}> ) } return } const styles = StyleSheet.create({ flexRow: {flexDirection: 'row'}, wSpace: {width: 5}, hSpace: {height: 5}, })