Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: improve dapp list render. #306

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions apps/mobile/src/components/Image/AppFastImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useRefState } from '@/hooks/common/useRefState';
import { useThemeStyles } from '@/hooks/theme';
import { createGetStyles } from '@/utils/styles';
import React from 'react';
import { StyleSheet, View } from 'react-native';
import FastImage from 'react-native-fast-image';

type FastImageProps = React.ComponentProps<typeof FastImage>;
export default function AppFastImage(
props: FastImageProps & {
PlaceholderContent?: React.ReactNode;
viewProps?: React.ComponentProps<typeof View>;
},
) {
const { styles } = useThemeStyles(getStyles);
const {
PlaceholderContent,
viewProps,
onLoadEnd: propOnLoadEnd,
onError: propOnError,
...rest
} = props;

const {
stateRef: firstLoadedRef,
state: firstLoaded,
setRefState,
} = useRefState(false);
const onLoadEnd = React.useCallback<
FastImageProps['onLoadEnd'] & object
>(() => {
if (!firstLoadedRef.current) setRefState(true, true);

propOnLoadEnd?.();
}, [firstLoadedRef, setRefState, propOnLoadEnd]);

const onError = React.useCallback<FastImageProps['onError'] & object>(() => {
if (!firstLoadedRef.current) setRefState(true, true);

propOnError?.();
}, [firstLoadedRef, setRefState, propOnError]);

return (
<View
{...viewProps}
style={[
styles.container,
// props.style?.width
viewProps?.style,
]}>
<FastImage
{...rest}
style={StyleSheet.flatten([props.style])}
onLoadEnd={onLoadEnd}
onError={onError}
/>
{PlaceholderContent && !firstLoaded && (
<View style={styles.placeholder}>{PlaceholderContent}</View>
)}
</View>
);
}

const getStyles = createGetStyles(colors => {
return {
container: {
position: 'relative',
},
placeholder: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
width: '100%',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
},
};
});
6 changes: 4 additions & 2 deletions apps/mobile/src/screens/Dapps/components/DappCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TouchableOpacity,
TouchableWithoutFeedback,
} from 'react-native-gesture-handler';
import FastImage from 'react-native-fast-image';
import { DappIcon } from './DappIcon';
import { stringUtils } from '@rabby-wallet/base-utils';
import { CHAINS } from '@/constant/chains';
Expand Down Expand Up @@ -47,7 +48,7 @@ export const DappCardListBy = ({
{data.map(item => {
return (
<View style={styles.tooltipListItem} key={item.logo_url}>
<Image
<FastImage
style={styles.tooltipListItemIcon}
source={{ uri: item.logo_url }}
/>
Expand Down Expand Up @@ -112,6 +113,7 @@ export const DappCard = ({
data?.info?.logo_url
? {
uri: data.info?.logo_url,
priority: 'low',
}
: undefined
}
Expand All @@ -121,7 +123,7 @@ export const DappCard = ({
{isActive ? <View style={styles.dappIconCircle} /> : null}
<>
{data?.isConnected && chain ? (
<Image
<FastImage
source={{
uri: chain?.logo,
}}
Expand Down
30 changes: 14 additions & 16 deletions apps/mobile/src/screens/Dapps/components/DappIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import React, { useMemo } from 'react';
import { StyleSheet, Text, View } from 'react-native';

import AppFastImage from '@/components/Image/AppFastImage';
import { useThemeColors } from '@/hooks/theme';
import { getOriginName, hashCode } from '@/utils/common';
import { Image } from '@rneui/themed';
import React, { useMemo } from 'react';
import {
// Image,
ImageStyle,
ImageURISource,
StyleProp,
StyleSheet,
Text,
View,
} from 'react-native';

const bgColorList = [
'#F69373',
Expand All @@ -31,10 +24,9 @@ export const DappIcon = ({
origin,
style,
source,
}: {
style?: StyleProp<ImageStyle>;
...rest
}: React.ComponentProps<typeof AppFastImage> & {
origin: string;
source?: ImageURISource;
}) => {
const colors = useThemeColors();
const styles = React.useMemo(() => getStyles(colors), [colors]);
Expand All @@ -57,9 +49,15 @@ export const DappIcon = ({
</View>
);

if (source?.uri) {
if (source) {
return (
<Image source={source} style={style} PlaceholderContent={Placeholder} />
<AppFastImage
source={source}
style={style}
fallback={false}
{...rest}
PlaceholderContent={Placeholder}
/>
);
}

Expand Down