|
@@ -0,0 +1,200 @@
|
|
1
|
+import React, { Component } from 'react';
|
|
2
|
+import { Image, View } from 'react-native';
|
|
3
|
+
|
|
4
|
+import { Platform } from '../../react';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * The default avatar to be used, in case the requested URI is not available
|
|
9
|
+ * or fails to be loaded.
|
|
10
|
+ *
|
|
11
|
+ * This is an inline version of images/avatar2.png.
|
|
12
|
+ *
|
|
13
|
+ * @type {string}
|
|
14
|
+ */
|
|
15
|
+const DEFAULT_AVATAR = require('./defaultAvatar.png');
|
|
16
|
+
|
|
17
|
+/**
|
|
18
|
+ * The amount of time to wait when the avatar URI is undefined before we start
|
|
19
|
+ * showing a default locally generated one. Note that since we have no URI, we
|
|
20
|
+ * have nothing we can cache, so the color will be random.
|
|
21
|
+ *
|
|
22
|
+ * @type {number}
|
|
23
|
+ */
|
|
24
|
+const UNDEFINED_AVATAR_TIMEOUT = 1000;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+/**
|
|
28
|
+ * Implements an Image component wrapper, which returns a default image if the
|
|
29
|
+ * requested one fails to load. The default image background is chosen by
|
|
30
|
+ * hashing the URL of the image.
|
|
31
|
+ */
|
|
32
|
+export default class AvatarImage extends Component {
|
|
33
|
+ /**
|
|
34
|
+ * AvatarImage component's property types.
|
|
35
|
+ *
|
|
36
|
+ * @static
|
|
37
|
+ */
|
|
38
|
+ static propTypes = {
|
|
39
|
+ /**
|
|
40
|
+ * If set to <tt>true</tt> it will not load the URL, but will use the
|
|
41
|
+ * default instead.
|
|
42
|
+ */
|
|
43
|
+ forceDefault: React.PropTypes.bool,
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * The source the {@link Image}.
|
|
47
|
+ */
|
|
48
|
+ source: React.PropTypes.object,
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ * The optional style to add to the {@link Image} in order to customize
|
|
52
|
+ * its base look (and feel).
|
|
53
|
+ */
|
|
54
|
+ style: React.PropTypes.object
|
|
55
|
+ };
|
|
56
|
+
|
|
57
|
+ /**
|
|
58
|
+ * Initializes new AvatarImage component.
|
|
59
|
+ *
|
|
60
|
+ * @param {Object} props - Component props.
|
|
61
|
+ */
|
|
62
|
+ constructor(props) {
|
|
63
|
+ super(props);
|
|
64
|
+
|
|
65
|
+ this.state = {
|
|
66
|
+ failed: false,
|
|
67
|
+ showDefault: false
|
|
68
|
+ };
|
|
69
|
+
|
|
70
|
+ this.componentWillReceiveProps(props);
|
|
71
|
+
|
|
72
|
+ this._onError = this._onError.bind(this);
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ /**
|
|
76
|
+ * Notifies this mounted React Component that it will receive new props.
|
|
77
|
+ * If the URI is undefined, wait {@code UNDEFINED_AVATAR_TIMEOUT} ms and
|
|
78
|
+ * start showing a default locally generated avatar afterwards.
|
|
79
|
+ *
|
|
80
|
+ * Once a URI is passed, it will be rendered instead, except if loading it
|
|
81
|
+ * fails, in which case we fallback to a locally generated avatar again.
|
|
82
|
+ *
|
|
83
|
+ * @inheritdoc
|
|
84
|
+ * @param {Object} nextProps - The read-only React Component props that this
|
|
85
|
+ * instance will receive.
|
|
86
|
+ * @returns {void}
|
|
87
|
+ */
|
|
88
|
+ componentWillReceiveProps(nextProps) {
|
|
89
|
+ const prevURI = this.props.source && this.props.source.uri;
|
|
90
|
+ const nextURI = nextProps.source && nextProps.source.uri;
|
|
91
|
+
|
|
92
|
+ if (typeof prevURI === 'undefined') {
|
|
93
|
+ clearTimeout(this._timeout);
|
|
94
|
+ if (typeof nextURI === 'undefined') {
|
|
95
|
+ this._timeout = setTimeout(() => {
|
|
96
|
+ this.setState({ showDefault: true });
|
|
97
|
+ }, UNDEFINED_AVATAR_TIMEOUT);
|
|
98
|
+ } else {
|
|
99
|
+ this.setState({ showDefault: nextProps.forceDefault });
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ /**
|
|
105
|
+ * Clear the timer just in case. See {@code componentWillReceiveProps} for
|
|
106
|
+ * details.
|
|
107
|
+ *
|
|
108
|
+ * @inheritdoc
|
|
109
|
+ */
|
|
110
|
+ componentWillUnmount() {
|
|
111
|
+ clearTimeout(this._timeout);
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ /**
|
|
115
|
+ * Computes a hash over the URI and returns a HSL background color. We use
|
|
116
|
+ * 75% as lightness, for nice pastel style colors.
|
|
117
|
+ *
|
|
118
|
+ * @returns {string} - The HSL CSS property.
|
|
119
|
+ * @private
|
|
120
|
+ */
|
|
121
|
+ _getBackgroundColor() {
|
|
122
|
+ const uri = this.props.source.uri;
|
|
123
|
+ let hash = 0;
|
|
124
|
+
|
|
125
|
+ // If we have no URI yet we have no data to hash from, so use a random
|
|
126
|
+ // value.
|
|
127
|
+ if (typeof uri === 'undefined') {
|
|
128
|
+ hash = Math.floor(Math.random() * 360);
|
|
129
|
+ } else {
|
|
130
|
+ /* eslint-disable no-bitwise */
|
|
131
|
+
|
|
132
|
+ for (let i = 0; i < uri.length; i++) {
|
|
133
|
+ hash = uri.charCodeAt(i) + ((hash << 5) - hash);
|
|
134
|
+ hash |= 0; // Convert to 32bit integer
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ /* eslint-enable no-bitwise */
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ return `hsl(${hash % 360}, 100%, 75%)`;
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ /**
|
|
144
|
+ * Error handler for image loading. When an image fails to load we'll mark
|
|
145
|
+ * it as failed and load the default URI instead.
|
|
146
|
+ *
|
|
147
|
+ * @private
|
|
148
|
+ * @returns {void}
|
|
149
|
+ */
|
|
150
|
+ _onError() {
|
|
151
|
+ this.setState({ failed: true });
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ /**
|
|
155
|
+ * Implements React's {@link Component#render()}.
|
|
156
|
+ *
|
|
157
|
+ * @inheritdoc
|
|
158
|
+ */
|
|
159
|
+ render() {
|
|
160
|
+ // eslint-disable-next-line no-unused-vars
|
|
161
|
+ const { forceDefault, source, style, ...props } = this.props;
|
|
162
|
+ const { failed, showDefault } = this.state;
|
|
163
|
+
|
|
164
|
+ if (failed || showDefault) {
|
|
165
|
+ const coloredBackground = {
|
|
166
|
+ ...style,
|
|
167
|
+ backgroundColor: this._getBackgroundColor(),
|
|
168
|
+ overflow: 'hidden'
|
|
169
|
+ };
|
|
170
|
+
|
|
171
|
+ let element = React.createElement(Image, {
|
|
172
|
+ ...props,
|
|
173
|
+ source: DEFAULT_AVATAR,
|
|
174
|
+ style: Platform.OS === 'android' ? style : coloredBackground
|
|
175
|
+ });
|
|
176
|
+
|
|
177
|
+ if (Platform.OS === 'android') {
|
|
178
|
+ // Here we need to wrap the Image in a View because of a bug in
|
|
179
|
+ // React Native for Android:
|
|
180
|
+ // https://github.com/facebook/react-native/issues/3198
|
|
181
|
+
|
|
182
|
+ element = React.createElement(View,
|
|
183
|
+ { style: coloredBackground }, element);
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ return element;
|
|
187
|
+ } else if (typeof source.uri === 'undefined') {
|
|
188
|
+ return null;
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ // We have a URI and it's time to render it.
|
|
192
|
+ return (
|
|
193
|
+ <Image
|
|
194
|
+ { ...props }
|
|
195
|
+ onError = { this._onError }
|
|
196
|
+ source = { source }
|
|
197
|
+ style = { style } />
|
|
198
|
+ );
|
|
199
|
+ }
|
|
200
|
+}
|