Skip to content

Commit

Permalink
[Lightbox] Always rely on Expo Image cache (#6189)
Browse files Browse the repository at this point in the history
* Inline useImageAspectRatio

* Switch AutoSizedImage to read dimensions from Expo Image cache

* Include thumbnail dimensions in image data

* Use dims from Expo Image cache in lightbox

* Fix wiring so all thumbnails get dimensions

* Fix type

* Oops
  • Loading branch information
gaearon authored Nov 9, 2024
1 parent 2d73c5a commit 42abd97
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 146 deletions.
93 changes: 0 additions & 93 deletions src/lib/media/image-sizes.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/screens/Profile/Header/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ let ProfileHeaderShell = ({
height: 1000,
width: 1000,
},
thumbDimensions: null,
type: 'circle-avi',
},
],
Expand Down
3 changes: 2 additions & 1 deletion src/view/com/lightbox/ImageViewing/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export type Position = {

export type ImageSource = {
uri: string
dimensions: Dimensions | null
thumbUri: string
thumbDimensions: Dimensions | null
thumbRect: MeasuredDimensions | null
alt?: string
dimensions: Dimensions | null
type: 'image' | 'circle-avi' | 'rect-avi'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Props = {
onRequestClose: () => void
onTap: () => void
onZoom: (isZoomed: boolean) => void
onLoad: (dims: ImageDimensions) => void
isScrollViewBeingDragged: boolean
showControls: boolean
measureSafeArea: () => {
Expand All @@ -66,6 +67,7 @@ const ImageItem = ({
imageSrc,
onTap,
onZoom,
onLoad,
isScrollViewBeingDragged,
measureSafeArea,
imageAspect,
Expand Down Expand Up @@ -330,8 +332,8 @@ const ImageItem = ({
transform: scaleAndMoveTransform.concat(manipulationTransform),
width: screenSize.width,
maxHeight: screenSize.height,
aspectRatio: imageAspect,
alignSelf: 'center',
aspectRatio: imageAspect ?? 1 /* force onLoad */,
}
})

Expand All @@ -349,6 +351,7 @@ const ImageItem = ({
return {
flex: 1,
transform: cropContentTransform,
opacity: imageAspect === undefined ? 0 : 1,
}
})

Expand Down Expand Up @@ -393,7 +396,10 @@ const ImageItem = ({
placeholderContentFit="cover"
placeholder={{uri: imageSrc.thumbUri}}
accessibilityLabel={imageSrc.alt}
onLoad={() => setHasLoaded(false)}
onLoad={e => {
setHasLoaded(true)
onLoad({width: e.source.width, height: e.source.height})
}}
style={{flex: 1, borderRadius}}
accessibilityHint=""
accessibilityIgnoresInvertColors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Props = {
onRequestClose: () => void
onTap: () => void
onZoom: (scaled: boolean) => void
onLoad: (dims: ImageDimensions) => void
isScrollViewBeingDragged: boolean
showControls: boolean
measureSafeArea: () => {
Expand All @@ -64,6 +65,7 @@ const ImageItem = ({
imageSrc,
onTap,
onZoom,
onLoad,
showControls,
measureSafeArea,
imageAspect,
Expand Down Expand Up @@ -162,8 +164,9 @@ const ImageItem = ({
transform: cropFrameTransform,
width: screenSize.width,
maxHeight: screenSize.height,
aspectRatio: imageAspect,
alignSelf: 'center',
aspectRatio: imageAspect ?? 1 /* force onLoad */,
opacity: imageAspect === undefined ? 0 : 1,
}
})

Expand All @@ -172,7 +175,8 @@ const ImageItem = ({
return {
transform: cropContentTransform,
width: '100%',
aspectRatio: imageAspect,
aspectRatio: imageAspect ?? 1 /* force onLoad */,
opacity: imageAspect === undefined ? 0 : 1,
}
})

Expand Down Expand Up @@ -224,7 +228,10 @@ const ImageItem = ({
accessibilityHint=""
enableLiveTextInteraction={showControls && !scaled}
accessibilityIgnoresInvertColors
onLoad={() => setHasLoaded(true)}
onLoad={e => {
setHasLoaded(true)
onLoad({width: e.source.width, height: e.source.height})
}}
/>
</Animated.View>
</Animated.View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {View} from 'react-native'
import {PanGesture} from 'react-native-gesture-handler'
import {SharedValue} from 'react-native-reanimated'

import {Dimensions} from '#/lib/media/types'
import {
Dimensions as ImageDimensions,
ImageSource,
Expand All @@ -16,6 +17,7 @@ type Props = {
onRequestClose: () => void
onTap: () => void
onZoom: (scaled: boolean) => void
onLoad: (dims: Dimensions) => void
isScrollViewBeingDragged: boolean
showControls: boolean
measureSafeArea: () => {
Expand Down
22 changes: 15 additions & 7 deletions src/view/com/lightbox/ImageViewing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {Trans} from '@lingui/macro'

import {useImageDimensions} from '#/lib/media/image-sizes'
import {Dimensions} from '#/lib/media/types'
import {colors, s} from '#/lib/styles'
import {isIOS} from '#/platform/detection'
import {Lightbox} from '#/state/lightbox'
Expand Down Expand Up @@ -92,7 +92,9 @@ export default function ImageViewRoot({

const canAnimate =
!PlatformInfo.getIsReducedMotionEnabled() &&
nextLightbox.images.every(img => img.dimensions && img.thumbRect)
nextLightbox.images.every(
img => img.thumbRect && (img.dimensions || img.thumbDimensions),
)

// https://github.com/software-mansion/react-native-reanimated/issues/6677
requestAnimationFrame(() => {
Expand Down Expand Up @@ -345,10 +347,15 @@ function LightboxImage({
openProgress: SharedValue<number>
dismissSwipeTranslateY: SharedValue<number>
}) {
const [imageAspect, imageDimensions] = useImageDimensions({
src: imageSrc.uri,
knownDimensions: imageSrc.dimensions,
})
const [fetchedDims, setFetchedDims] = React.useState<Dimensions | null>(null)
const dims = fetchedDims ?? imageSrc.dimensions ?? imageSrc.thumbDimensions
let imageAspect: number | undefined
if (dims) {
imageAspect = dims.width / dims.height
if (Number.isNaN(imageAspect)) {
imageAspect = undefined
}
}

const safeFrameDelayedForJSThreadOnly = useSafeAreaFrame()
const safeInsetsDelayedForJSThreadOnly = useSafeAreaInsets()
Expand Down Expand Up @@ -452,11 +459,12 @@ function LightboxImage({
onTap={onTap}
onZoom={onZoom}
onRequestClose={onRequestClose}
onLoad={setFetchedDims}
isScrollViewBeingDragged={isScrollViewBeingDragged}
showControls={showControls}
measureSafeArea={measureSafeArea}
imageAspect={imageAspect}
imageDimensions={imageDimensions}
imageDimensions={dims ?? undefined}
dismissSwipePan={dismissSwipePan}
transforms={transforms}
/>
Expand Down
1 change: 1 addition & 0 deletions src/view/com/profile/ProfileSubpageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function ProfileSubpageHeader({
height: 1000,
width: 1000,
},
thumbDimensions: null,
type: 'rect-avi',
},
],
Expand Down
Loading

0 comments on commit 42abd97

Please sign in to comment.