Skip to content

Commit

Permalink
fix: Image cache key (#179)
Browse files Browse the repository at this point in the history
* Create source from image helper.
* Apply image source helper to all images.
* Fix one direct use of expo-image.
  • Loading branch information
lukashaertel authored Sep 15, 2024
1 parent 6fdd5c2 commit 3f3bf81
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/components/announce/AnnouncementCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ImageBackground } from "expo-image";
import moment, { Moment } from "moment-timezone";
import React, { FC } from "react";
import { StyleSheet, View, ViewStyle } from "react-native";
Expand All @@ -8,6 +7,8 @@ import { useThemeBackground, useThemeName } from "../../hooks/themes/useThemeHoo
import { AnnouncementDetails } from "../../store/eurofurence/types";
import { appStyles } from "../AppStyles";
import { Label } from "../generic/atoms/Label";
import { ImageBackground } from "../generic/atoms/ImageBackground";
import { sourceFromImage } from "../generic/atoms/Image.common";
import { colorForArea } from "./utils";

export type AnnouncementDetailsInstance = {
Expand Down Expand Up @@ -45,7 +46,7 @@ export const AnnouncementCard: FC<AnnouncementCardProps> = ({ containerStyle, st
onPress={() => onPress?.(announcement.details)}
onLongPress={() => onLongPress?.(announcement.details)}
>
<ImageBackground style={[styles.pre, stylePre]} source={announcement.details.Image?.Url ?? null}>
<ImageBackground style={[styles.pre, stylePre]} source={sourceFromImage(announcement.details.Image)}>
<View style={[styles.areaIndicator, styleAreaIndicator]} />
</ImageBackground>

Expand Down
8 changes: 7 additions & 1 deletion src/components/artistalley/ArtistAlleyStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ArtistAlleyOwnTableRegistrationRecord } from "../../store/eurofurence/t
import { Image } from "../generic/atoms/Image";
import { Label } from "../generic/atoms/Label";
import { Button } from "../generic/containers/Button";
import { sourceFromImage } from "../generic/atoms/Image.common";

export type ArtistAlleyStatusProps = {
data: ArtistAlleyOwnTableRegistrationRecord;
Expand Down Expand Up @@ -45,7 +46,12 @@ export const ArtistAlleyStatus = ({ data, onEdit }: ArtistAlleyStatusProps) => {
</Label>
<Label type="caption">{t("submission_image_label")}</Label>
<View style={[styles.imageContainer, backgroundStyle]}>
<Image style={[styles.image, { aspectRatio: data.Image.Width / data.Image.Height }]} contentFit={undefined} source={data.Image.Url} placeholder={null} />
<Image
style={[styles.image, { aspectRatio: data.Image.Width / data.Image.Height }]}
contentFit={undefined}
source={sourceFromImage(data.Image)}
placeholder={null}
/>
</View>

<Button style={styles.button} onPress={onEdit}>
Expand Down
3 changes: 2 additions & 1 deletion src/components/dealers/DealerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { assetSource } from "../../util/assets";
import { appStyles } from "../AppStyles";
import { Image } from "../generic/atoms/Image";
import { Label } from "../generic/atoms/Label";
import { sourceFromImage } from "../generic/atoms/Image.common";
import { isPresent, joinOffDays } from "./utils";

export type DealerDetailsInstance = {
Expand Down Expand Up @@ -48,7 +49,7 @@ export const DealerCard: FC<DealerCardProps> = ({ containerStyle, style, dealer,
const present = dealer.present;
const description = dealer.details.Categories?.join(", ");
const offDays = dealer.offDays;
const avatar = dealer.details.ArtistThumbnail ? dealer.details.ArtistThumbnail.Url : dealer.details.Artist ? dealer.details.Artist.Url : assetSource("ych");
const avatar = sourceFromImage(dealer.details.ArtistThumbnail) ?? sourceFromImage(dealer.details.Artist) ?? assetSource("ych");

// Translation object.
const { t } = useTranslation("Dealers");
Expand Down
3 changes: 2 additions & 1 deletion src/components/dealers/DealerContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ImageExButton } from "../generic/containers/ImageButton";
import { LinkItem } from "../maps/LinkItem";
import { conTimeZone } from "../../configuration";
import { platformShareIcon } from "../generic/atoms/Icon";
import { sourceFromImage } from "../generic/atoms/Image.common";

const DealerCategories = ({ t, dealer }: { t: TFunction; dealer: DealerDetails }) => {
// Nothing to display for no categories.
Expand Down Expand Up @@ -130,7 +131,7 @@ export const DealerContent: FC<DealerContentProps> = ({ dealer, parentPad = 0, u

{!dealer.Artist ? null : (
<View style={[appStyles.shadow, styles.avatarCircle]}>
<Image contentFit="cover" style={styles.avatarImage} source={dealer.Artist.Url} />
<Image contentFit="cover" style={styles.avatarImage} source={sourceFromImage(dealer.Artist)} />
</View>
)}

Expand Down
3 changes: 2 additions & 1 deletion src/components/events/EventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Label } from "../generic/atoms/Label";
import { Progress } from "../generic/atoms/Progress";
import { Row } from "../generic/containers/Row";
import { conTimeZone } from "../../configuration";
import { sourceFromImage } from "../generic/atoms/Image.common";
import { EventCardTime } from "./EventCardTime";

const glyphIconSize = 90;
Expand Down Expand Up @@ -136,7 +137,7 @@ export const EventCard: FC<EventCardProps> = ({ containerStyle, style, type = "d

{event.details.Banner ? (
<View style={styles.mainPoster}>
<ImageBackground source={event.details.Banner.Url} contentFit="cover" style={StyleSheet.absoluteFill} recyclingKey={event.details.Id}>
<ImageBackground source={sourceFromImage(event.details.Banner)} contentFit="cover" style={StyleSheet.absoluteFill} recyclingKey={event.details.Id}>
<View style={styles.tagArea2}>
<View style={styles.tagAreaInner}>
<Label style={styles.tag} type="regular" color="white" ellipsizeMode="head" numberOfLines={1}>
Expand Down
3 changes: 2 additions & 1 deletion src/components/generic/atoms/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useAppNavigation } from "../../../hooks/nav/useAppNavigation";
import { useThemeBackground } from "../../../hooks/themes/useThemeHooks";
import { ImageDetails } from "../../../store/eurofurence/types";
import { Image, ImageProps } from "./Image";
import { sourceFromImage } from "./Image.common";

export type BannerProps = {
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ export const Banner: FC<BannerProps> = ({ style, image, placeholder, viewable })
if (viewable && image) navigation.navigate("Viewer", { id: image.Id });
}}
>
<Image style={[styles.image, aspect, style]} contentFit={undefined} source={image.Url} placeholder={placeholder} />
<Image style={[styles.image, aspect, style]} contentFit={undefined} source={sourceFromImage(image)} placeholder={placeholder} />
</TouchableOpacity>
);
};
Expand Down
16 changes: 16 additions & 0 deletions src/components/generic/atoms/Image.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ImageSource } from "expo-image";
import { ImageRecord } from "../../../store/eurofurence/types";

/**
* Converts a record to an image source.
* @param image The image record to convert.
*/
export const sourceFromImage = (image: ImageRecord | null | undefined): ImageSource | null => {
if (!image) return null;
return {
uri: image.Url,
cacheKey: image.ContentHashSha1,
width: image.Width,
height: image.Height,
};
};
5 changes: 3 additions & 2 deletions src/components/generic/atoms/ImageFill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Dimensions, StyleProp, StyleSheet, View, ViewStyle } from "react-native

import { ImageDetails } from "../../../store/eurofurence/types";
import { Image, ImageProps } from "./Image";
import { sourceFromImage } from "./Image.common";

const initialSize = { width: Dimensions.get("window").width - 40, height: 160 };

Expand Down Expand Up @@ -64,9 +65,9 @@ export const ImageFill: FC<ImageFillProps> = ({ style, image, target }) => {

return (
<View style={[StyleSheet.absoluteFill, style]} onLayout={(e) => setSize(e.nativeEvent.layout)}>
<Image style={StyleSheet.absoluteFill} contentFit="cover" blurRadius={20} source={image?.Url} priority="normal" />
<Image style={StyleSheet.absoluteFill} contentFit="cover" blurRadius={20} source={sourceFromImage(image)} priority="normal" />
<View style={arrangerStyle}>
<Image style={imageStyle} contentFit="fill" contentPosition="top left" source={image?.Url} priority="normal" />
<Image style={imageStyle} contentFit="fill" contentPosition="top left" source={sourceFromImage(image)} priority="normal" />
</View>
</View>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/maps/MapContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ImageDetails, LinkFragment, MapDetails, MapEntryDetails } from "../../s
import { Image } from "../generic/atoms/Image";
import { Label } from "../generic/atoms/Label";
import { Marker } from "../generic/atoms/Marker";
import { sourceFromImage } from "../generic/atoms/Image.common";
import { LinkItem } from "./LinkItem";

const distSq = (hx: number, hy: number) => hx * hx + hy * hy;
Expand Down Expand Up @@ -159,7 +160,7 @@ export const MapContent: FC<MapContentProps> = ({ map, entry }) => {
onTransform={onTransform}
>
<View style={styleContainer}>
<Image style={styles.image} allowDownscaling={false} contentFit={undefined} source={map.Image.Url} priority="high" />
<Image style={styles.image} allowDownscaling={false} contentFit={undefined} source={sourceFromImage(map.Image)} priority="high" />
{!entry ? null : <Marker style={styleMarker} markerSize={75} />}
</View>
</ZoomableView>
Expand Down
3 changes: 2 additions & 1 deletion src/components/viewer/ViewerImageRecord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RecordId } from "../../store/eurofurence/types";
import { Image } from "../generic/atoms/Image";
import { Header } from "../generic/containers/Header";
import { platformShareIcon } from "../generic/atoms/Icon";
import { sourceFromImage } from "../generic/atoms/Image.common";
import { minZoomFor, shareImage } from "./Viewer.common";

const viewerPadding = 20;
Expand Down Expand Up @@ -47,7 +48,7 @@ export const ViewerImageRecord: FC<ViewerImageRecordProps> = ({ id }) => {
bindToBorders={true}
>
<View style={styleContainer}>
<Image style={styles.image} allowDownscaling={false} contentFit={undefined} source={image.Url} priority="high" />
<Image style={styles.image} allowDownscaling={false} contentFit={undefined} source={sourceFromImage(image)} priority="high" />
</View>
</ZoomableView>
)}
Expand Down

0 comments on commit 3f3bf81

Please sign in to comment.