-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avatar.tsx
131 lines (120 loc) · 3.17 KB
/
Avatar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as React from 'react';
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native';
import { useThemeContext } from '../contexts/ThemeContext';
import { getScaleFactor } from '../styles/createScaleFactor';
import createStyleSheet from '../styles/createStyleSheet';
import { LocalIcon, LocalIconName } from './Icon';
import { getImageComponent } from './Image';
type SubComponents = {};
const sf = getScaleFactor();
const AVATAR_SIZE = sf(56);
type AvatarStateProps = {
stateUri: LocalIconName;
stateSize?: number | undefined;
stateRadius?: number | undefined;
stateOverflow?: 'visible' | 'hidden' | 'scroll' | undefined;
};
export type AvatarProps = {
uri: string | number;
uriOnError?: LocalIconName | undefined;
size?: number | undefined;
radius?: number | undefined;
containerStyle?: StyleProp<ViewStyle> | undefined;
state?: AvatarStateProps | undefined;
useFastImage?: boolean;
};
const Avatar: ((props: AvatarProps) => JSX.Element) & SubComponents = ({
uri,
uriOnError,
size = AVATAR_SIZE,
radius = AVATAR_SIZE / 2,
containerStyle,
state,
useFastImage,
}): JSX.Element => {
const { colors } = useThemeContext();
const [loadDefault, setLoadDefault] = React.useState(false);
const uriS = typeof uri === 'number' ? uri : { uri: uri };
const Image = getImageComponent(useFastImage);
return (
<View
style={[
styles.container,
{
width: size,
height: size,
borderRadius: radius,
backgroundColor: colors.background,
},
containerStyle,
]}
>
{loadDefault ? (
<LocalIcon name={uriOnError ?? 'default_avatar'} size={size * 1.0} />
) : (
<Image
onError={() => {
setLoadDefault(true);
}}
source={uriS}
resizeMode="cover"
style={[StyleSheet.absoluteFill, { width: size, height: size }]}
/>
)}
{state && (
<StateAvatar
stateSize={state.stateSize ? state.stateSize : size / 2}
stateUri={state.stateUri ? state.stateUri : 'Icon_Image'}
stateRadius={state.stateRadius ? state.stateRadius : size / 4}
stateOverflow={state.stateOverflow}
/>
)}
</View>
);
};
const StateAvatar = ({
stateUri,
stateSize,
stateRadius,
stateOverflow,
}: AvatarStateProps) => {
const { colors } = useThemeContext();
return (
<View
style={[
styles.state_container,
StyleSheet.absoluteFill,
stateOverflow ? { overflow: stateOverflow } : null,
]}
>
<View
style={[
StyleSheet.absoluteFill,
{
backgroundColor: colors.primary,
opacity: 0.0,
borderRadius: stateRadius,
},
]}
/>
<LocalIcon
color={colors.primary}
name={stateUri ?? 'alert_default'}
size={stateSize}
/>
</View>
);
};
const styles = createStyleSheet({
container: {
alignItems: 'center',
justifyContent: 'center',
overflow: 'hidden',
},
state_container: {
justifyContent: 'flex-end',
alignItems: 'flex-end',
overflow: 'hidden',
},
});
export default Avatar;