diff options
author | dan <dan.abramov@gmail.com> | 2024-11-04 21:28:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-04 21:28:27 +0000 |
commit | 174988bc5ab00774d200a882312985f55d903d81 (patch) | |
tree | af52d6f05093ceeea3e3293db0cbab3d5cf43156 /src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx | |
parent | ac9d910e1e77c559eff8b32cd8412335f41074f1 (diff) | |
download | voidsky-174988bc5ab00774d200a882312985f55d903d81.tar.zst |
Unify dimensions cache between lightbox and feed (#6047)
* Remove useless memo * Use explicit values when useImageAspectRatio doesn't know It's not very good that you can't distingiush when we haven't loaded vs when we're certain. This shifts the burden of dealing with missing values to the caller. * Check cache early * Handle src change * Rewrite image-sizes.fetch to avoid mixing async styles * Make image-sizes LRU Code is copy paste from useImageDimensions.ts * Rm unused fields * Derive aspect on the fly * Factor useImageDimensions out of useImageAspectRatio * Move useImageDimensions into image-sizes * Make lightbox use the same cache * Wire up known dimensions to the lightbox * Handle division by zero in the hook * Use safe aspect for lightbox calculations
Diffstat (limited to 'src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx')
-rw-r--r-- | src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx b/src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx index f21953498..a96a1c913 100644 --- a/src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx +++ b/src/view/com/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx @@ -19,8 +19,8 @@ import Animated, { import {Image} from 'expo-image' import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED' -import {Dimensions as ImageDimensions, ImageSource} from '../../@types' -import useImageDimensions from '../../hooks/useImageDimensions' +import {useImageDimensions} from '#/lib/media/image-sizes' +import {ImageSource} from '../../@types' const SWIPE_CLOSE_OFFSET = 75 const SWIPE_CLOSE_VELOCITY = 1 @@ -47,7 +47,10 @@ const ImageItem = ({ const scrollViewRef = useAnimatedRef<Animated.ScrollView>() const translationY = useSharedValue(0) const [scaled, setScaled] = useState(false) - const imageDimensions = useImageDimensions(imageSrc) + const [imageAspect, imageDimensions] = useImageDimensions({ + src: imageSrc.uri, + knownDimensions: imageSrc.dimensions, + }) const maxZoomScale = imageDimensions ? (imageDimensions.width / SCREEN.width) * MAX_ORIGINAL_IMAGE_ZOOM : 1 @@ -99,7 +102,7 @@ const ImageItem = ({ const willZoom = !scaled if (willZoom) { nextZoomRect = getZoomRectAfterDoubleTap( - imageDimensions, + imageAspect, absoluteX, absoluteY, ) @@ -179,7 +182,7 @@ const styles = StyleSheet.create({ }) const getZoomRectAfterDoubleTap = ( - imageDimensions: ImageDimensions | null, + imageAspect: number | undefined, touchX: number, touchY: number, ): { @@ -188,7 +191,7 @@ const getZoomRectAfterDoubleTap = ( width: number height: number } => { - if (!imageDimensions) { + if (!imageAspect) { return { x: 0, y: 0, @@ -199,7 +202,6 @@ const getZoomRectAfterDoubleTap = ( // First, let's figure out how much we want to zoom in. // We want to try to zoom in at least close enough to get rid of black bars. - const imageAspect = imageDimensions.width / imageDimensions.height const screenAspect = SCREEN.width / SCREEN.height const zoom = Math.max( imageAspect / screenAspect, |