-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6cc0db
commit d966f75
Showing
7 changed files
with
215 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
apps/wallet-mobile/src/features/Portfolio/common/MediaGallery/MediaGallery.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import {FlashList, FlashListProps} from '@shopify/flash-list' | ||
import {Balance, Portfolio} from '@yoroi/types' | ||
import React from 'react' | ||
import {StyleSheet, Text, TouchableOpacity, useWindowDimensions, View} from 'react-native' | ||
|
||
import {Spacer} from '../../../../components/Spacer/Spacer' | ||
import {MediaPreview} from './MediaPreview' | ||
|
||
type Props = { | ||
amounts: ReadonlyArray<Portfolio.Token.Amount> | ||
onSelect: (amount: Portfolio.Token.Amount) => void | ||
onRefresh: () => void | ||
isRefreshing: boolean | ||
bounces?: FlashListProps<Balance.TokenInfo>['bounces'] | ||
ListEmptyComponent?: FlashListProps<Balance.TokenInfo>['ListEmptyComponent'] | ||
withVerticalPadding?: boolean | ||
readOnly?: boolean | ||
} | ||
export const MediaGallery = ({ | ||
amounts = [], | ||
onSelect, | ||
onRefresh, | ||
isRefreshing, | ||
readOnly, | ||
bounces = false, | ||
ListEmptyComponent = undefined, | ||
withVerticalPadding = undefined, | ||
}: Props) => { | ||
const dimensions = useWindowDimensions() | ||
const minSize = Math.min(dimensions.width, dimensions.height) | ||
const imageSize = (minSize - horizontalPadding * 2) / numberOfColumns - imageHorizontalPadding | ||
|
||
return ( | ||
<GalleryList | ||
imageSize={imageSize} | ||
data={amounts} | ||
onRefresh={onRefresh} | ||
refreshing={isRefreshing} | ||
bounces={bounces} | ||
ListEmptyComponent={ListEmptyComponent} | ||
withVerticalPadding={withVerticalPadding} | ||
renderMedia={(amount) => ( | ||
<TouchableOpacity onPress={() => onSelect(amount)} disabled={readOnly} key={amount.info.id}> | ||
<Media info={amount.info} imageSize={imageSize} /> | ||
</TouchableOpacity> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
function Media({info, imageSize}: {info: Portfolio.Token.Info; imageSize: number}) { | ||
return ( | ||
<View> | ||
<View style={styles.imageWrapper}> | ||
<MediaPreview info={info} width={imageSize} height={imageSize} style={styles.image} /> | ||
</View> | ||
|
||
<Spacer height={imagePadding} /> | ||
|
||
<Text style={[styles.text, {width: imageSize}]}>{info.name}</Text> | ||
</View> | ||
) | ||
} | ||
|
||
const textHeight = 20 | ||
const imagePadding = 8 | ||
const rowSpacing = 14 | ||
const numberOfColumns = 2 | ||
const horizontalPadding = 16 | ||
const verticalPadding = 16 | ||
const gapBetweenColumns = horizontalPadding | ||
const imageHorizontalPadding = gapBetweenColumns / numberOfColumns | ||
|
||
function GalleryList({ | ||
imageSize, | ||
renderMedia, | ||
withVerticalPadding = false, | ||
...rest | ||
}: Partial<FlashListProps<Portfolio.Token.Amount>> & { | ||
renderMedia: (item: Portfolio.Token.Amount) => React.ReactElement | ||
withVerticalPadding?: boolean | ||
imageSize: number | ||
}) { | ||
return ( | ||
<FlashList | ||
{...rest} | ||
numColumns={2} | ||
renderItem={({item, index}) => ( | ||
<View | ||
style={[index % 2 === 0 ? {paddingRight: imageHorizontalPadding} : {paddingLeft: imageHorizontalPadding}]} | ||
> | ||
<View>{renderMedia(item)}</View> | ||
|
||
<Spacer height={rowSpacing} /> | ||
</View> | ||
)} | ||
contentContainerStyle={{ | ||
paddingHorizontal: horizontalPadding, | ||
paddingVertical: withVerticalPadding ? verticalPadding : undefined, | ||
}} | ||
keyExtractor={(_, index) => index.toString()} | ||
horizontal={false} | ||
estimatedItemSize={imageSize + imagePadding + textHeight + rowSpacing} | ||
/> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
imageWrapper: { | ||
position: 'relative', | ||
overflow: 'hidden', | ||
borderRadius: 8, | ||
}, | ||
image: { | ||
borderRadius: 8, | ||
overlayColor: '#FFFFFF', | ||
}, | ||
text: { | ||
fontSize: 14, | ||
lineHeight: textHeight, | ||
}, | ||
}) |
57 changes: 57 additions & 0 deletions
57
apps/wallet-mobile/src/features/Portfolio/common/MediaGallery/MediaPreview.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {Portfolio} from '@yoroi/types' | ||
import {Image} from 'expo-image' | ||
import React from 'react' | ||
import {ImageStyle, View} from 'react-native' | ||
import SkeletonPlaceholder from 'react-native-skeleton-placeholder' | ||
|
||
import placeholder from '../../../../assets/img/nft-placeholder.png' | ||
import {useSelectedWallet} from '../../../WalletManager/Context' | ||
|
||
type MediaPreviewProps = { | ||
info: Portfolio.Token.Info | ||
showPlaceholder?: boolean | ||
style?: ImageStyle | ||
height: number | ||
width: number | ||
contentFit?: 'cover' | 'contain' | ||
blurRadius?: number | ||
} | ||
|
||
export const MediaPreview = React.memo( | ||
({info, showPlaceholder, style = {}, height, width, contentFit = 'cover', blurRadius}: MediaPreviewProps) => { | ||
const {network} = useSelectedWallet() | ||
const [isLoading, setIsLoading] = React.useState(true) | ||
|
||
const [policy, name] = info.id.split('.') | ||
const uri = showPlaceholder | ||
? placeholder | ||
: `https://${network}.processed-media.yoroiwallet.com/${policy}/${name}?width=256&height=256&kind=metadata&fit=${contentFit}` | ||
|
||
return ( | ||
<View style={{width, height, overflow: 'hidden'}}> | ||
{isLoading && ( | ||
<SkeletonPlaceholder enabled={true}> | ||
<View style={{width, height}} /> | ||
</SkeletonPlaceholder> | ||
)} | ||
|
||
<Image | ||
source={{uri, headers}} | ||
contentFit={contentFit} | ||
style={{width, height, ...style}} | ||
onLoadStart={() => setIsLoading(true)} | ||
onLoadEnd={() => setIsLoading(false)} | ||
blurRadius={blurRadius} | ||
placeholder={blurhash} | ||
/> | ||
</View> | ||
) | ||
}, | ||
) | ||
|
||
const blurhash = | ||
'|rF?hV%2WCj[ayj[a|j[az_NaeWBj@ayfRayfQfQM{M|azj[azf6fQfQfQIpWXofj[ayj[j[fQayWCoeoeaya}j[ayfQa{oLj?j[WVj[ayayj[fQoff7azayj[ayj[j[ayofayayayj[fQj[ayayj[ayfjj[j[ayjuayj[' | ||
|
||
const headers = { | ||
Accept: 'image/webp', | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters