Image not loading on swipe (#114)
* Adds prefetching to images * Adds image prefetch * bugfix for images not showing on swipe * Fixes prefetch bug * Update src/view/com/util/PostEmbeds.tsx --------- Co-authored-by: Paul Frazee <pfrazee@gmail.com>
This commit is contained in:
@@ -108,9 +108,6 @@ function ImageViewing({
|
|||||||
data={images}
|
data={images}
|
||||||
horizontal
|
horizontal
|
||||||
pagingEnabled
|
pagingEnabled
|
||||||
windowSize={2}
|
|
||||||
initialNumToRender={1}
|
|
||||||
maxToRenderPerBatch={1}
|
|
||||||
showsHorizontalScrollIndicator={false}
|
showsHorizontalScrollIndicator={false}
|
||||||
showsVerticalScrollIndicator={false}
|
showsVerticalScrollIndicator={false}
|
||||||
initialScrollIndex={imageIndex}
|
initialScrollIndex={imageIndex}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {StyleSheet, StyleProp, View, ViewStyle} from 'react-native'
|
import {StyleSheet, StyleProp, View, ViewStyle, Image} from 'react-native'
|
||||||
import {AppBskyEmbedImages, AppBskyEmbedExternal} from '@atproto/api'
|
import {AppBskyEmbedImages, AppBskyEmbedExternal} from '@atproto/api'
|
||||||
import LinearGradient from 'react-native-linear-gradient'
|
import LinearGradient from 'react-native-linear-gradient'
|
||||||
import {Link} from '../util/Link'
|
import {Link} from '../util/Link'
|
||||||
@@ -35,6 +35,16 @@ export function PostEmbeds({
|
|||||||
const onLongPress = (index: number) => {
|
const onLongPress = (index: number) => {
|
||||||
saveImageModal({uri: uris[index]})
|
saveImageModal({uri: uris[index]})
|
||||||
}
|
}
|
||||||
|
const onPressIn = (index: number) => {
|
||||||
|
const firstImageToShow = uris[index]
|
||||||
|
Image.prefetch(firstImageToShow)
|
||||||
|
uris.forEach(uri => {
|
||||||
|
if (firstImageToShow !== uri) {
|
||||||
|
// First image already prefeched above
|
||||||
|
Image.prefetch(uri)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (embed.images.length === 4) {
|
if (embed.images.length === 4) {
|
||||||
return (
|
return (
|
||||||
@@ -44,6 +54,7 @@ export function PostEmbeds({
|
|||||||
uris={embed.images.map(img => img.thumb)}
|
uris={embed.images.map(img => img.thumb)}
|
||||||
onPress={openLightbox}
|
onPress={openLightbox}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
|
onPressIn={onPressIn}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
@@ -55,6 +66,7 @@ export function PostEmbeds({
|
|||||||
uris={embed.images.map(img => img.thumb)}
|
uris={embed.images.map(img => img.thumb)}
|
||||||
onPress={openLightbox}
|
onPress={openLightbox}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
|
onPressIn={onPressIn}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
@@ -66,6 +78,7 @@ export function PostEmbeds({
|
|||||||
uris={embed.images.map(img => img.thumb)}
|
uris={embed.images.map(img => img.thumb)}
|
||||||
onPress={openLightbox}
|
onPress={openLightbox}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
|
onPressIn={onPressIn}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
@@ -76,6 +89,7 @@ export function PostEmbeds({
|
|||||||
uri={embed.images[0].thumb}
|
uri={embed.images[0].thumb}
|
||||||
onPress={() => openLightbox(0)}
|
onPress={() => openLightbox(0)}
|
||||||
onLongPress={() => onLongPress(0)}
|
onLongPress={() => onLongPress(0)}
|
||||||
|
onPressIn={() => onPressIn(0)}
|
||||||
containerStyle={styles.singleImage}
|
containerStyle={styles.singleImage}
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ export function AutoSizedImage({
|
|||||||
uri,
|
uri,
|
||||||
onPress,
|
onPress,
|
||||||
onLongPress,
|
onLongPress,
|
||||||
|
onPressIn,
|
||||||
style,
|
style,
|
||||||
containerStyle,
|
containerStyle,
|
||||||
}: {
|
}: {
|
||||||
@@ -85,6 +86,7 @@ export function AutoSizedImage({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
onPress={onPress}
|
onPress={onPress}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
|
onPressIn={onPressIn}
|
||||||
delayPressIn={DELAY_PRESS_IN}>
|
delayPressIn={DELAY_PRESS_IN}>
|
||||||
{error ? (
|
{error ? (
|
||||||
<View style={[styles.errorContainer, errPal.view, containerStyle]}>
|
<View style={[styles.errorContainer, errPal.view, containerStyle]}>
|
||||||
|
|||||||
@@ -23,11 +23,14 @@ export function ImageLayoutGrid({
|
|||||||
uris,
|
uris,
|
||||||
onPress,
|
onPress,
|
||||||
onLongPress,
|
onLongPress,
|
||||||
|
onPressIn,
|
||||||
style,
|
style,
|
||||||
}: {
|
}: {
|
||||||
type: ImageLayoutGridType
|
type: ImageLayoutGridType
|
||||||
uris: string[]
|
uris: string[]
|
||||||
onPress?: (index: number) => void
|
onPress?: (index: number) => void
|
||||||
|
onLongPress?: (index: number) => void
|
||||||
|
onPressIn?: (index: number) => void
|
||||||
style?: StyleProp<ViewStyle>
|
style?: StyleProp<ViewStyle>
|
||||||
}) {
|
}) {
|
||||||
const [containerInfo, setContainerInfo] = React.useState<Dim | undefined>()
|
const [containerInfo, setContainerInfo] = React.useState<Dim | undefined>()
|
||||||
@@ -46,6 +49,7 @@ export function ImageLayoutGrid({
|
|||||||
type={type}
|
type={type}
|
||||||
uris={uris}
|
uris={uris}
|
||||||
onPress={onPress}
|
onPress={onPress}
|
||||||
|
onPressIn={onPressIn}
|
||||||
onLongPress={onLongPress}
|
onLongPress={onLongPress}
|
||||||
containerInfo={containerInfo}
|
containerInfo={containerInfo}
|
||||||
/>
|
/>
|
||||||
@@ -59,11 +63,14 @@ function ImageLayoutGridInner({
|
|||||||
uris,
|
uris,
|
||||||
onPress,
|
onPress,
|
||||||
onLongPress,
|
onLongPress,
|
||||||
|
onPressIn,
|
||||||
containerInfo,
|
containerInfo,
|
||||||
}: {
|
}: {
|
||||||
type: ImageLayoutGridType
|
type: ImageLayoutGridType
|
||||||
uris: string[]
|
uris: string[]
|
||||||
onPress?: (index: number) => void
|
onPress?: (index: number) => void
|
||||||
|
onLongPress?: (index: number) => void
|
||||||
|
onPressIn?: (index: number) => void
|
||||||
containerInfo: Dim
|
containerInfo: Dim
|
||||||
}) {
|
}) {
|
||||||
const size1 = React.useMemo<ImageStyle>(() => {
|
const size1 = React.useMemo<ImageStyle>(() => {
|
||||||
@@ -91,6 +98,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(0)}
|
onPress={() => onPress?.(0)}
|
||||||
|
onPressIn={() => onPressIn?.(0)}
|
||||||
onLongPress={() => onLongPress(0)}>
|
onLongPress={() => onLongPress(0)}>
|
||||||
<Image source={{uri: uris[0]}} style={size1} />
|
<Image source={{uri: uris[0]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -98,6 +106,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(1)}
|
onPress={() => onPress?.(1)}
|
||||||
|
onPressIn={() => onPressIn?.(1)}
|
||||||
onLongPress={() => onLongPress(1)}>
|
onLongPress={() => onLongPress(1)}>
|
||||||
<Image source={{uri: uris[1]}} style={size1} />
|
<Image source={{uri: uris[1]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -110,6 +119,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(0)}
|
onPress={() => onPress?.(0)}
|
||||||
|
onPressIn={() => onPressIn?.(0)}
|
||||||
onLongPress={() => onLongPress(0)}>
|
onLongPress={() => onLongPress(0)}>
|
||||||
<Image source={{uri: uris[0]}} style={size2} />
|
<Image source={{uri: uris[0]}} style={size2} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -118,6 +128,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(1)}
|
onPress={() => onPress?.(1)}
|
||||||
|
onPressIn={() => onPressIn?.(1)}
|
||||||
onLongPress={() => onLongPress(1)}>
|
onLongPress={() => onLongPress(1)}>
|
||||||
<Image source={{uri: uris[1]}} style={size1} />
|
<Image source={{uri: uris[1]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -125,6 +136,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(2)}
|
onPress={() => onPress?.(2)}
|
||||||
|
onPressIn={() => onPressIn?.(2)}
|
||||||
onLongPress={() => onLongPress(2)}>
|
onLongPress={() => onLongPress(2)}>
|
||||||
<Image source={{uri: uris[2]}} style={size1} />
|
<Image source={{uri: uris[2]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -139,6 +151,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(0)}
|
onPress={() => onPress?.(0)}
|
||||||
|
onPressIn={() => onPressIn?.(0)}
|
||||||
onLongPress={() => onLongPress(0)}>
|
onLongPress={() => onLongPress(0)}>
|
||||||
<Image source={{uri: uris[0]}} style={size1} />
|
<Image source={{uri: uris[0]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -146,6 +159,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(1)}
|
onPress={() => onPress?.(1)}
|
||||||
|
onPressIn={() => onPressIn?.(1)}
|
||||||
onLongPress={() => onLongPress(1)}>
|
onLongPress={() => onLongPress(1)}>
|
||||||
<Image source={{uri: uris[1]}} style={size1} />
|
<Image source={{uri: uris[1]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -155,6 +169,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(2)}
|
onPress={() => onPress?.(2)}
|
||||||
|
onPressIn={() => onPressIn?.(2)}
|
||||||
onLongPress={() => onLongPress(2)}>
|
onLongPress={() => onLongPress(2)}>
|
||||||
<Image source={{uri: uris[2]}} style={size1} />
|
<Image source={{uri: uris[2]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
@@ -162,6 +177,7 @@ function ImageLayoutGridInner({
|
|||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
delayPressIn={DELAY_PRESS_IN}
|
delayPressIn={DELAY_PRESS_IN}
|
||||||
onPress={() => onPress?.(3)}
|
onPress={() => onPress?.(3)}
|
||||||
|
onPressIn={() => onPressIn?.(3)}
|
||||||
onLongPress={() => onLongPress(3)}>
|
onLongPress={() => onLongPress(3)}>
|
||||||
<Image source={{uri: uris[3]}} style={size1} />
|
<Image source={{uri: uris[3]}} style={size1} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
|||||||
Reference in New Issue
Block a user