diff --git a/apps/mobile/src/components/lightbox/ImageViewing/@types/index.ts b/apps/mobile/src/components/lightbox/ImageViewing/@types/index.ts index a960cb0dc..7cf658c99 100644 --- a/apps/mobile/src/components/lightbox/ImageViewing/@types/index.ts +++ b/apps/mobile/src/components/lightbox/ImageViewing/@types/index.ts @@ -6,6 +6,7 @@ * */ +import type { ImageProps } from "expo-image" import type { TransformsStyle } from "react-native" import type { MeasuredDimensions } from "react-native-reanimated" @@ -19,10 +20,10 @@ export type Position = { y: number } -export type ImageSource = { +export type LightboxImageSource = { uri: string dimensions: Dimensions | null - thumbUri: string + thumbUri: ImageProps["placeholder"] thumbDimensions: Dimensions | null thumbRect: MeasuredDimensions | null alt?: string diff --git a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.android.tsx b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.android.tsx index 5f0e6c7f8..eedc98172 100644 --- a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.android.tsx +++ b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.android.tsx @@ -15,7 +15,7 @@ import Animated, { import { Image as ProxyImage } from "@/src/components/ui/image/Image" -import type { Dimensions as ImageDimensions, ImageSource, Transform } from "../../@types" +import type { Dimensions as ImageDimensions, LightboxImageSource, Transform } from "../../@types" import type { TransformMatrix } from "../../transforms" import { applyRounding, @@ -32,7 +32,7 @@ const MAX_ORIGINAL_IMAGE_ZOOM = 2 const initialTransform = createTransform() type Props = { - imageSrc: ImageSource + imageSrc: LightboxImageSource onRequestClose: () => void onTap: () => void onZoom: (isZoomed: boolean) => void @@ -350,7 +350,7 @@ const ImageItem = ({ contentFit="contain" source={{ uri: imageSrc.uri }} placeholderContentFit="contain" - placeholder={{ uri: imageSrc.thumbUri }} + placeholder={imageSrc.thumbUri} accessibilityLabel={imageSrc.alt} onLoad={ hasLoaded @@ -360,7 +360,7 @@ const ImageItem = ({ onLoad({ width: e.source.width, height: e.source.height }) } } - style={{ flex: 1, borderRadius, backgroundColor: "transparent" }} + style={{ flex: 1, borderRadius, backgroundColor: "rgba(255, 255, 255, 0.1)" }} accessibilityHint="" accessibilityIgnoresInvertColors cachePolicy="memory" diff --git a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx index 5f061f074..4f6327020 100644 --- a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx +++ b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.ios.tsx @@ -25,13 +25,13 @@ import { useSafeAreaFrame } from "react-native-safe-area-context" import { Image as ProxyImage } from "@/src/components/ui/image/Image" -import type { Dimensions as ImageDimensions, ImageSource, Transform } from "../../@types" +import type { Dimensions as ImageDimensions, LightboxImageSource, Transform } from "../../@types" const MAX_ORIGINAL_IMAGE_ZOOM = 2 const MIN_SCREEN_ZOOM = 2 type Props = { - imageSrc: ImageSource + imageSrc: LightboxImageSource onRequestClose: () => void onTap: () => void onZoom: (scaled: boolean) => void @@ -219,13 +219,17 @@ const ImageItem = ({ contentFit="contain" source={{ uri: imageSrc.uri }} placeholderContentFit="contain" - placeholder={{ uri: imageSrc.thumbUri }} - style={{ flex: 1, borderRadius, backgroundColor: "transparent" }} + placeholder={imageSrc.thumbUri} + style={{ flex: 1, borderRadius, backgroundColor: "rgba(255, 255, 255, 0.1)" }} accessibilityLabel={imageSrc.alt} accessibilityHint="" enableLiveTextInteraction={showControls && !scaled} accessibilityIgnoresInvertColors priority="high" + // Workaround for a bug where the image doesn't retrieve the cache on iOS + // Remove this when updated to Expo + // Learn more https://github.com/expo/expo/issues/37705 + blurRadius={0.001} onLoad={ hasLoaded ? undefined diff --git a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.tsx b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.tsx index 4d0e14818..f257615dd 100644 --- a/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.tsx +++ b/apps/mobile/src/components/lightbox/ImageViewing/components/ImageItem/ImageItem.tsx @@ -8,12 +8,12 @@ import type { SharedValue } from "react-native-reanimated" import type { Dimensions, Dimensions as ImageDimensions, - ImageSource, + LightboxImageSource, Transform, } from "../../@types" type Props = { - imageSrc: ImageSource + imageSrc: LightboxImageSource onRequestClose: () => void onTap: () => void onZoom: (scaled: boolean) => void diff --git a/apps/mobile/src/components/lightbox/ImageViewing/index.tsx b/apps/mobile/src/components/lightbox/ImageViewing/index.tsx index 4ddc6e65a..fa9254e6b 100644 --- a/apps/mobile/src/components/lightbox/ImageViewing/index.tsx +++ b/apps/mobile/src/components/lightbox/ImageViewing/index.tsx @@ -35,7 +35,7 @@ import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-cont import { isIOS } from "@/src/lib/platform" import type { Lightbox } from "../lightboxState" -import type { Dimensions, ImageSource, Transform } from "./@types" +import type { Dimensions, LightboxImageSource, Transform } from "./@types" import ImageDefaultHeader from "./components/ImageDefaultHeader" import ImageItem from "./components/ImageItem/ImageItem" @@ -377,7 +377,7 @@ function LightboxImage({ openProgress, dismissSwipeTranslateY, }: { - imageSrc: ImageSource + imageSrc: LightboxImageSource onRequestClose: () => void onTap: () => void onZoom: (scaled: boolean) => void @@ -517,7 +517,7 @@ function LightboxFooter({ isAltExpanded, toggleAltExpanded, }: { - images: ImageSource[] + images: LightboxImageSource[] index: number isAltExpanded: boolean toggleAltExpanded: () => void diff --git a/apps/mobile/src/components/lightbox/lightboxState.tsx b/apps/mobile/src/components/lightbox/lightboxState.tsx index 1f0c5b9ad..745db8b03 100644 --- a/apps/mobile/src/components/lightbox/lightboxState.tsx +++ b/apps/mobile/src/components/lightbox/lightboxState.tsx @@ -2,11 +2,11 @@ import { nanoid } from "nanoid/non-secure" import type { PropsWithChildren } from "react" import { createContext, use, useCallback, useMemo, useState } from "react" -import type { ImageSource } from "./ImageViewing/@types" +import type { LightboxImageSource } from "./ImageViewing/@types" export type Lightbox = { id: string - images: ImageSource[] + images: LightboxImageSource[] index: number } diff --git a/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx b/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx index 1c5b434c5..eea79dc80 100644 --- a/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx +++ b/apps/mobile/src/components/ui/carousel/MediaCarousel.tsx @@ -1,5 +1,6 @@ import type { FeedViewType } from "@follow/constants" import type { MediaModel } from "@follow/database/schemas/types" +import type { ImageSource } from "expo-image" import type { Ref } from "react" import { useEffect, useState } from "react" import { ScrollView, TouchableOpacity, View } from "react-native" @@ -14,6 +15,7 @@ import { EntryGridFooter } from "@/src/modules/entry-content/EntryGridFooter" import { Image } from "../image/Image" import { ImageContextMenu } from "../image/ImageContextMenu" +import { getAllSources } from "../image/utils" import { VideoPlayer } from "../video/VideoPlayer" export const MediaCarousel = ({ @@ -27,7 +29,7 @@ export const MediaCarousel = ({ ref?: Ref entryId: string media: MediaModel[] - onPreview?: (index: number) => void + onPreview?: (index: number, placeholder: ImageSource | undefined) => void aspectRatio: number view: FeedViewType }) => { @@ -64,12 +66,18 @@ export const MediaCarousel = ({ if (!imageUrl) { return null } + const proxy = { + height: 400, + } const ImageItem = ( - onPreview?.(index)}> + { + const [placeholder] = getAllSources({ uri: imageUrl }, proxy) + onPreview?.(index, { blurhash: m.blurhash, ...placeholder }) + }} + > { if (!isImageSourceWithUri(source)) { - return [undefined, undefined] + return [undefined, undefined] as const } // Now TypeScript knows source has a uri property @@ -67,7 +67,7 @@ export const getAllSources = ( } })() - return [safeSource, proxiesSafeSource] + return [safeSource, proxiesSafeSource] as const } const getImageData = async (imageUrl: string) => { diff --git a/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx index 8ddd0b3a9..7f0f0406a 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntryPictureItem.tsx @@ -5,6 +5,7 @@ import { getFeedById } from "@follow/store/feed/getter" import { unreadSyncService } from "@follow/store/unread/store" import { tracker } from "@follow/tracker" import { uniqBy } from "es-toolkit/compat" +import type { ImageSource } from "expo-image" import type { Ref } from "react" import { useMemo } from "react" import { Text, View } from "react-native" @@ -44,7 +45,7 @@ export function EntryPictureItem({ id }: { id: string }) { ref={aviRef} media={item.media} entryId={id} - onPreview={(index) => { + onPreview={(index, placeholder) => { const feed = getFeedById(item.feedId!) if (!feed) { return @@ -60,7 +61,7 @@ export function EntryPictureItem({ id }: { id: string }) { runOnJS(openLightbox)({ images: (item.media ?? []).map((media) => ({ uri: media.url, - thumbUri: media.url, + thumbUri: placeholder ?? { uri: media.url }, thumbDimensions: null, thumbRect: rect, dimensions: rect @@ -95,7 +96,7 @@ const MediaItems = ({ ref?: Ref media: MediaModel[] entryId: string - onPreview?: (index: number) => void + onPreview?: (index: number, placeholder: ImageSource | undefined) => void aspectRatio?: number }) => { const firstMedia = media[0] diff --git a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx index 8ade98689..7a3dbbcf7 100644 --- a/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx +++ b/apps/mobile/src/modules/entry-list/templates/EntrySocialItem.tsx @@ -5,17 +5,20 @@ import { useFeedById } from "@follow/store/feed/hooks" import { useEntryTranslation } from "@follow/store/translation/hooks" import { unreadSyncService } from "@follow/store/unread/store" import { tracker } from "@follow/tracker" +import type { ImageSource } from "expo-image" import { memo, useCallback } from "react" import { Pressable, Text, View } from "react-native" import type { MeasuredDimensions } from "react-native-reanimated" import Animated, { measure, runOnJS, runOnUI, useAnimatedRef } from "react-native-reanimated" import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general" +import type { LightboxImageSource } from "@/src/components/lightbox/ImageViewing/@types" import { useLightboxControls } from "@/src/components/lightbox/lightboxState" import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar" import { RelativeDateTime } from "@/src/components/ui/datetime/RelativeDateTime" import { FeedIcon } from "@/src/components/ui/icon/feed-icon" import { Image } from "@/src/components/ui/image/Image" +import { getAllSources } from "@/src/components/ui/image/utils" import { ItemPressableStyle } from "@/src/components/ui/pressable/enum" import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable" import { NativePressable } from "@/src/components/ui/pressable/NativePressable" @@ -72,7 +75,7 @@ export const EntrySocialItem = memo( }, [entry, navigation]) const onPreviewImage = useCallback( - (index: number, rect: MeasuredDimensions | null) => { + (index: number, rect: MeasuredDimensions | null, placeholder: ImageSource | undefined) => { "worklet" runOnJS(openLightbox)({ images: (entry?.media ?? []) @@ -89,11 +92,11 @@ export const EntrySocialItem = memo( width: mediaItem.width ?? 0, height: mediaItem.height ?? 0, }, - thumbUri: imageUrl ?? "", + thumbUri: placeholder ?? { uri: imageUrl }, thumbDimensions: null, thumbRect: rect, type: "image" as const, - } + } satisfies LightboxImageSource }) .filter((i) => !!i.uri), index, @@ -181,7 +184,11 @@ interface EntryMediaItemProps { entryId: string mediaItem: MediaModel fullWidth: boolean - onPreviewImage: (index: number, rect: MeasuredDimensions | null) => void + onPreviewImage: ( + index: number, + rect: MeasuredDimensions | null, + placeholder: ImageSource | undefined, + ) => void } const EntryMediaItem = memo( @@ -196,21 +203,23 @@ const EntryMediaItem = memo( : undefined if (!imageUrl) return null + const proxy = { + width: fullWidth ? 400 : 200, + } const ImageItem = ( { + const [placeholder] = getAllSources({ uri: imageUrl }, proxy) runOnUI(() => { "worklet" const rect = measure(aviRef) - onPreviewImage(index, rect) + onPreviewImage(index, rect, { blurhash: mediaItem.blurhash, ...placeholder }) })() }} >