@momo-kits/avatar 0.0.34 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Avatar.js +214 -122
- package/package.json +1 -1
package/Avatar.js
CHANGED
|
@@ -2,11 +2,20 @@
|
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
3
|
import React, { Component } from 'react';
|
|
4
4
|
import {
|
|
5
|
-
View,
|
|
5
|
+
View,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
TouchableOpacity,
|
|
8
|
+
Dimensions,
|
|
9
|
+
Platform,
|
|
6
10
|
} from 'react-native';
|
|
7
11
|
import { get, isEqual } from 'lodash';
|
|
8
12
|
import {
|
|
9
|
-
|
|
13
|
+
ValueUtil,
|
|
14
|
+
Colors,
|
|
15
|
+
Text,
|
|
16
|
+
IconSource,
|
|
17
|
+
Image,
|
|
18
|
+
ScaleSize,
|
|
10
19
|
} from '@momo-kits/core';
|
|
11
20
|
|
|
12
21
|
export const AvatarQuality = {
|
|
@@ -32,7 +41,7 @@ export const AvatarSize = {
|
|
|
32
41
|
size72: 'size72',
|
|
33
42
|
size80: 'size80',
|
|
34
43
|
size88: 'size88',
|
|
35
|
-
size96: 'size96'
|
|
44
|
+
size96: 'size96',
|
|
36
45
|
};
|
|
37
46
|
|
|
38
47
|
const COLORS = [
|
|
@@ -62,13 +71,13 @@ export const SubIconType = {
|
|
|
62
71
|
momo: 'momo',
|
|
63
72
|
online: 'online',
|
|
64
73
|
key: 'key',
|
|
65
|
-
none: 'none'
|
|
74
|
+
none: 'none',
|
|
66
75
|
};
|
|
67
76
|
|
|
68
77
|
const styles = StyleSheet.create({
|
|
69
78
|
container: {
|
|
70
79
|
justifyContent: 'center',
|
|
71
|
-
alignItems: 'center'
|
|
80
|
+
alignItems: 'center',
|
|
72
81
|
},
|
|
73
82
|
shortNameView: {
|
|
74
83
|
justifyContent: 'center',
|
|
@@ -76,90 +85,93 @@ const styles = StyleSheet.create({
|
|
|
76
85
|
|
|
77
86
|
backgroundColor: Colors.very_light_pink_four,
|
|
78
87
|
width: '100%',
|
|
79
|
-
height: '100%'
|
|
88
|
+
height: '100%',
|
|
80
89
|
},
|
|
81
90
|
shortNameText: {
|
|
82
91
|
// fontSize: 17,
|
|
83
|
-
color:
|
|
92
|
+
color: Colors.pink_05_b,
|
|
93
|
+
},
|
|
94
|
+
shortNameTextCustom: {
|
|
95
|
+
color: '#f6f6f6',
|
|
84
96
|
},
|
|
85
97
|
absolute: {
|
|
86
98
|
position: 'absolute',
|
|
87
99
|
top: 0,
|
|
88
100
|
left: 0,
|
|
89
101
|
right: 0,
|
|
90
|
-
bottom: 0
|
|
102
|
+
bottom: 0,
|
|
91
103
|
},
|
|
92
104
|
subIcon: {
|
|
93
105
|
position: 'absolute',
|
|
94
106
|
bottom: 0,
|
|
95
|
-
right: 0
|
|
107
|
+
right: 0,
|
|
96
108
|
},
|
|
97
109
|
subIconImage: {
|
|
98
110
|
width: 22,
|
|
99
|
-
height: 22
|
|
111
|
+
height: 22,
|
|
100
112
|
},
|
|
101
113
|
subIconImageOnline: {
|
|
102
114
|
backgroundColor: Colors.kiwi_green,
|
|
103
115
|
borderColor: 'white',
|
|
104
|
-
borderWidth: 1
|
|
105
|
-
}
|
|
116
|
+
borderWidth: 1,
|
|
117
|
+
},
|
|
106
118
|
});
|
|
107
119
|
|
|
108
120
|
const subIconSize = (width) => ({
|
|
109
121
|
tiny: {
|
|
110
122
|
width: 8,
|
|
111
123
|
height: 8,
|
|
112
|
-
borderRadius: 4
|
|
124
|
+
borderRadius: 4,
|
|
113
125
|
},
|
|
114
126
|
small: {
|
|
115
127
|
width: 10,
|
|
116
128
|
height: 10,
|
|
117
|
-
borderRadius: 5
|
|
129
|
+
borderRadius: 5,
|
|
118
130
|
},
|
|
119
131
|
medium: {
|
|
120
132
|
width: 12,
|
|
121
133
|
height: 12,
|
|
122
|
-
borderRadius: 6
|
|
134
|
+
borderRadius: 6,
|
|
123
135
|
},
|
|
124
136
|
large: {
|
|
125
137
|
width: 16,
|
|
126
138
|
height: 16,
|
|
127
|
-
borderRadius: 8
|
|
139
|
+
borderRadius: 8,
|
|
128
140
|
},
|
|
129
141
|
size8: {
|
|
130
142
|
width: 8,
|
|
131
143
|
height: 8,
|
|
132
|
-
borderRadius: 4
|
|
144
|
+
borderRadius: 4,
|
|
133
145
|
},
|
|
134
146
|
size24: {
|
|
135
147
|
width: 10,
|
|
136
148
|
height: 10,
|
|
137
|
-
borderRadius: 5
|
|
149
|
+
borderRadius: 5,
|
|
138
150
|
},
|
|
139
151
|
size36: {
|
|
140
152
|
width: 10,
|
|
141
153
|
height: 10,
|
|
142
|
-
borderRadius: 5
|
|
154
|
+
borderRadius: 5,
|
|
143
155
|
},
|
|
144
156
|
size44: {
|
|
145
157
|
width: 14,
|
|
146
158
|
height: 14,
|
|
147
|
-
borderRadius: 7
|
|
159
|
+
borderRadius: 7,
|
|
148
160
|
},
|
|
149
161
|
size48: {
|
|
150
162
|
width: 14,
|
|
151
163
|
height: 14,
|
|
152
|
-
borderRadius: 7
|
|
164
|
+
borderRadius: 7,
|
|
153
165
|
},
|
|
154
166
|
size52: {
|
|
155
167
|
width: 16,
|
|
156
168
|
height: 16,
|
|
157
|
-
borderRadius: 8
|
|
169
|
+
borderRadius: 8,
|
|
158
170
|
},
|
|
159
171
|
size56: {
|
|
160
172
|
width: 16,
|
|
161
173
|
height: 16,
|
|
162
|
-
borderRadius: 8
|
|
174
|
+
borderRadius: 8,
|
|
163
175
|
},
|
|
164
176
|
size64: {
|
|
165
177
|
width: 16,
|
|
@@ -201,128 +213,130 @@ const subIconSize = (width) => ({
|
|
|
201
213
|
left: -5,
|
|
202
214
|
width: 24,
|
|
203
215
|
height: 24,
|
|
204
|
-
borderRadius: 12
|
|
205
|
-
}
|
|
216
|
+
borderRadius: 12,
|
|
217
|
+
},
|
|
206
218
|
});
|
|
207
219
|
|
|
208
220
|
const avatarSize = (width) => ({
|
|
209
221
|
tiny: {
|
|
210
222
|
width: 16,
|
|
211
223
|
height: 16,
|
|
212
|
-
borderRadius: 8
|
|
224
|
+
borderRadius: 8,
|
|
213
225
|
},
|
|
214
226
|
small: {
|
|
215
227
|
width: 32,
|
|
216
228
|
height: 32,
|
|
217
|
-
borderRadius: 16
|
|
229
|
+
borderRadius: 16,
|
|
218
230
|
},
|
|
219
231
|
medium: {
|
|
220
232
|
width: 40,
|
|
221
233
|
height: 40,
|
|
222
|
-
borderRadius: 20
|
|
234
|
+
borderRadius: 20,
|
|
223
235
|
},
|
|
224
236
|
large: {
|
|
225
237
|
width: 60,
|
|
226
238
|
height: 60,
|
|
227
|
-
borderRadius: 30
|
|
239
|
+
borderRadius: 30,
|
|
228
240
|
},
|
|
229
241
|
giant: {
|
|
230
242
|
width: 120,
|
|
231
243
|
height: 120,
|
|
232
|
-
borderRadius: 60
|
|
244
|
+
borderRadius: 60,
|
|
233
245
|
},
|
|
234
246
|
size8: {
|
|
235
247
|
width: 8,
|
|
236
248
|
height: 8,
|
|
237
|
-
borderRadius: 4
|
|
249
|
+
borderRadius: 4,
|
|
238
250
|
},
|
|
239
251
|
size24: {
|
|
240
252
|
width: 24,
|
|
241
253
|
height: 24,
|
|
242
|
-
borderRadius: 12
|
|
254
|
+
borderRadius: 12,
|
|
243
255
|
},
|
|
244
256
|
size36: {
|
|
245
257
|
width: 36,
|
|
246
258
|
height: 36,
|
|
247
|
-
borderRadius: 18
|
|
259
|
+
borderRadius: 18,
|
|
248
260
|
},
|
|
249
261
|
size44: {
|
|
250
262
|
width: 44,
|
|
251
263
|
height: 44,
|
|
252
|
-
borderRadius: 22
|
|
264
|
+
borderRadius: 22,
|
|
253
265
|
},
|
|
254
266
|
size48: {
|
|
255
267
|
width: 48,
|
|
256
268
|
height: 48,
|
|
257
|
-
borderRadius: 24
|
|
269
|
+
borderRadius: 24,
|
|
258
270
|
},
|
|
259
271
|
size52: {
|
|
260
272
|
width: 52,
|
|
261
273
|
height: 52,
|
|
262
|
-
borderRadius: 26
|
|
274
|
+
borderRadius: 26,
|
|
263
275
|
},
|
|
264
276
|
size56: {
|
|
265
277
|
width: 56,
|
|
266
278
|
height: 56,
|
|
267
|
-
borderRadius: 28
|
|
279
|
+
borderRadius: 28,
|
|
268
280
|
},
|
|
269
281
|
size64: {
|
|
270
282
|
width: 64,
|
|
271
283
|
height: 64,
|
|
272
|
-
borderRadius: 32
|
|
284
|
+
borderRadius: 32,
|
|
273
285
|
},
|
|
274
286
|
size72: {
|
|
275
287
|
width: 72,
|
|
276
288
|
height: 72,
|
|
277
|
-
borderRadius: 36
|
|
289
|
+
borderRadius: 36,
|
|
278
290
|
},
|
|
279
291
|
size80: {
|
|
280
292
|
width: 80,
|
|
281
293
|
height: 80,
|
|
282
|
-
borderRadius: 40
|
|
294
|
+
borderRadius: 40,
|
|
283
295
|
},
|
|
284
296
|
size88: {
|
|
285
297
|
width: 88,
|
|
286
298
|
height: 88,
|
|
287
|
-
borderRadius: 44
|
|
299
|
+
borderRadius: 44,
|
|
288
300
|
},
|
|
289
301
|
size96: {
|
|
290
302
|
width: 96,
|
|
291
303
|
height: 96,
|
|
292
|
-
borderRadius: 48
|
|
293
|
-
}
|
|
304
|
+
borderRadius: 48,
|
|
305
|
+
},
|
|
294
306
|
});
|
|
295
307
|
|
|
296
308
|
const shortName = (width) => ({
|
|
297
309
|
tiny: {
|
|
298
|
-
fontSize: ScaleSize(5, width)
|
|
310
|
+
fontSize: ScaleSize(5, width),
|
|
299
311
|
},
|
|
300
312
|
small: {
|
|
301
|
-
fontSize: ScaleSize(11, width)
|
|
313
|
+
fontSize: ScaleSize(11, width),
|
|
302
314
|
},
|
|
303
315
|
medium: {
|
|
304
|
-
fontSize: ScaleSize(15, width)
|
|
316
|
+
fontSize: ScaleSize(15, width),
|
|
305
317
|
},
|
|
306
318
|
middle: {
|
|
307
|
-
fontSize: ScaleSize(16, width)
|
|
319
|
+
fontSize: ScaleSize(16, width),
|
|
308
320
|
},
|
|
309
321
|
large: {
|
|
310
|
-
fontSize: ScaleSize(20, width)
|
|
322
|
+
fontSize: ScaleSize(20, width),
|
|
311
323
|
},
|
|
312
324
|
giant: {
|
|
313
|
-
fontSize: ScaleSize(25, width)
|
|
314
|
-
}
|
|
325
|
+
fontSize: ScaleSize(25, width),
|
|
326
|
+
},
|
|
315
327
|
});
|
|
316
328
|
|
|
317
329
|
export default class Avatar extends Component {
|
|
318
330
|
constructor(props) {
|
|
319
331
|
super(props);
|
|
320
|
-
this.color =
|
|
332
|
+
this.color = props.custom
|
|
333
|
+
? this.getAvatarColor(props.name)
|
|
334
|
+
: Colors.pink_10;
|
|
321
335
|
this.state = {
|
|
322
336
|
hideSource: false,
|
|
323
337
|
ownUpdate: false,
|
|
324
338
|
source: props.source,
|
|
325
|
-
dimensions: {}
|
|
339
|
+
dimensions: {},
|
|
326
340
|
};
|
|
327
341
|
this.imageSource = '';
|
|
328
342
|
}
|
|
@@ -333,10 +347,14 @@ export default class Avatar extends Component {
|
|
|
333
347
|
this.subscription = Dimensions.addEventListener(
|
|
334
348
|
'change',
|
|
335
349
|
({ window }) => {
|
|
336
|
-
if(
|
|
350
|
+
if (
|
|
351
|
+
window?.height > 0 &&
|
|
352
|
+
window?.width > 0 &&
|
|
353
|
+
Platform.OS === 'android'
|
|
354
|
+
) {
|
|
337
355
|
this.setState({ dimensions: window });
|
|
338
356
|
}
|
|
339
|
-
}
|
|
357
|
+
},
|
|
340
358
|
);
|
|
341
359
|
}
|
|
342
360
|
}
|
|
@@ -353,7 +371,8 @@ export default class Avatar extends Component {
|
|
|
353
371
|
return {
|
|
354
372
|
ownUpdate: false,
|
|
355
373
|
};
|
|
356
|
-
}
|
|
374
|
+
}
|
|
375
|
+
if (!isEqual(nextProps.source, prevState.source)) {
|
|
357
376
|
return { source: nextProps.source, hideSource: false };
|
|
358
377
|
}
|
|
359
378
|
return null;
|
|
@@ -369,7 +388,7 @@ export default class Avatar extends Component {
|
|
|
369
388
|
}
|
|
370
389
|
});
|
|
371
390
|
return alphabet.join('');
|
|
372
|
-
}
|
|
391
|
+
};
|
|
373
392
|
|
|
374
393
|
hashUrl(url) {
|
|
375
394
|
let hash = 0;
|
|
@@ -377,34 +396,48 @@ export default class Avatar extends Component {
|
|
|
377
396
|
if (url?.length === 0) return hash;
|
|
378
397
|
for (let i = 0; i < url?.length; i++) {
|
|
379
398
|
char = url?.charCodeAt(i);
|
|
380
|
-
hash = (
|
|
399
|
+
hash = (hash << 5) - hash + char;
|
|
381
400
|
hash &= hash; // Convert to 32bit integer
|
|
382
401
|
}
|
|
383
402
|
return hash;
|
|
384
403
|
}
|
|
385
404
|
|
|
386
|
-
renderShortName = (name, avatarStyle, size, color
|
|
405
|
+
renderShortName = (name, avatarStyle, size, color) => {
|
|
387
406
|
if (name) {
|
|
388
407
|
const shortedName = this.getContactShortName(name);
|
|
389
408
|
const shortNameStyle = this.mapShortNameStyle(size);
|
|
390
409
|
return (
|
|
391
|
-
<View
|
|
392
|
-
|
|
410
|
+
<View
|
|
411
|
+
style={[
|
|
412
|
+
styles.shortNameView,
|
|
413
|
+
avatarStyle,
|
|
414
|
+
{ backgroundColor: color },
|
|
415
|
+
]}>
|
|
416
|
+
<Text.H4
|
|
417
|
+
style={[
|
|
418
|
+
this.props.custom
|
|
419
|
+
? styles.shortNameTextCustom
|
|
420
|
+
: styles.shortNameText,
|
|
421
|
+
shortNameStyle,
|
|
422
|
+
]}>
|
|
423
|
+
{shortedName}
|
|
424
|
+
</Text.H4>
|
|
393
425
|
</View>
|
|
394
426
|
);
|
|
395
427
|
}
|
|
396
428
|
return <View />;
|
|
397
|
-
}
|
|
429
|
+
};
|
|
398
430
|
|
|
399
431
|
isUrl(url) {
|
|
400
|
-
const expression =
|
|
432
|
+
const expression =
|
|
433
|
+
/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/gi;
|
|
401
434
|
return expression.test(url);
|
|
402
435
|
}
|
|
403
436
|
|
|
404
437
|
isValidImageUrl = (source) => {
|
|
405
438
|
const validSource = source && source.uri && this.isUrl(source.uri);
|
|
406
439
|
return validSource;
|
|
407
|
-
}
|
|
440
|
+
};
|
|
408
441
|
|
|
409
442
|
mapAvatarQuality = (url, quality) => {
|
|
410
443
|
if (!quality || quality === AvatarQuality.medium) {
|
|
@@ -418,30 +451,41 @@ export default class Avatar extends Component {
|
|
|
418
451
|
}
|
|
419
452
|
|
|
420
453
|
return url;
|
|
421
|
-
}
|
|
454
|
+
};
|
|
422
455
|
|
|
423
456
|
mapStyleFromSize = (size) => {
|
|
424
457
|
const { dimensions } = this.state;
|
|
425
|
-
const avatarStyle = avatarSize(
|
|
458
|
+
const avatarStyle = avatarSize(
|
|
459
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
460
|
+
).small;
|
|
426
461
|
if (typeof size === 'object') return size;
|
|
427
|
-
if (avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[size])
|
|
462
|
+
if (avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[size])
|
|
463
|
+
return avatarSize(dimensions?.width > 0 ? dimensions?.width : null)[
|
|
464
|
+
size
|
|
465
|
+
];
|
|
428
466
|
return avatarStyle;
|
|
429
|
-
}
|
|
467
|
+
};
|
|
430
468
|
|
|
431
469
|
mapShortNameStyle = (size) => {
|
|
432
470
|
let shortNameStyle = {};
|
|
433
471
|
const { dimensions } = this.state;
|
|
434
472
|
switch (size) {
|
|
435
473
|
case AvatarSize.tiny: {
|
|
436
|
-
shortNameStyle = shortName(
|
|
474
|
+
shortNameStyle = shortName(
|
|
475
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
476
|
+
).tiny;
|
|
437
477
|
break;
|
|
438
478
|
}
|
|
439
479
|
case AvatarSize.small: {
|
|
440
|
-
shortNameStyle = shortName(
|
|
480
|
+
shortNameStyle = shortName(
|
|
481
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
482
|
+
).small;
|
|
441
483
|
break;
|
|
442
484
|
}
|
|
443
485
|
case AvatarSize.medium: {
|
|
444
|
-
shortNameStyle = shortName(
|
|
486
|
+
shortNameStyle = shortName(
|
|
487
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
488
|
+
).medium;
|
|
445
489
|
break;
|
|
446
490
|
}
|
|
447
491
|
case AvatarSize.size48: {
|
|
@@ -449,25 +493,36 @@ export default class Avatar extends Component {
|
|
|
449
493
|
break;
|
|
450
494
|
}
|
|
451
495
|
case AvatarSize.large: {
|
|
452
|
-
shortNameStyle = shortName(
|
|
496
|
+
shortNameStyle = shortName(
|
|
497
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
498
|
+
).large;
|
|
453
499
|
break;
|
|
454
500
|
}
|
|
455
501
|
case AvatarSize.giant: {
|
|
456
|
-
shortNameStyle = shortName(
|
|
502
|
+
shortNameStyle = shortName(
|
|
503
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
504
|
+
).giant;
|
|
457
505
|
break;
|
|
458
506
|
}
|
|
459
507
|
default:
|
|
460
|
-
shortNameStyle = shortName(
|
|
508
|
+
shortNameStyle = shortName(
|
|
509
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
510
|
+
).small;
|
|
461
511
|
}
|
|
462
512
|
return shortNameStyle;
|
|
463
|
-
}
|
|
513
|
+
};
|
|
464
514
|
|
|
465
515
|
mapSubIconSize = (size) => {
|
|
466
516
|
const { dimensions } = this.state;
|
|
467
|
-
const subIconStyle = subIconSize(
|
|
468
|
-
|
|
517
|
+
const subIconStyle = subIconSize(
|
|
518
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
519
|
+
).small;
|
|
520
|
+
if (subIconSize(dimensions?.width > 0 ? dimensions?.width : null)[size])
|
|
521
|
+
return subIconSize(
|
|
522
|
+
dimensions?.width > 0 ? dimensions?.width : null,
|
|
523
|
+
)[size];
|
|
469
524
|
return subIconStyle;
|
|
470
|
-
}
|
|
525
|
+
};
|
|
471
526
|
|
|
472
527
|
extractSize = (avatarStyle) => {
|
|
473
528
|
const { style } = this.props;
|
|
@@ -478,69 +533,89 @@ export default class Avatar extends Component {
|
|
|
478
533
|
height: containerWidth,
|
|
479
534
|
borderRadius: containerWidth / 2,
|
|
480
535
|
borderWidth,
|
|
481
|
-
borderColor: Colors.black_01
|
|
536
|
+
borderColor: Colors.black_01,
|
|
482
537
|
};
|
|
483
|
-
}
|
|
538
|
+
};
|
|
484
539
|
|
|
485
540
|
onLoadSourceError = (imageSource) => {
|
|
486
|
-
if(this.imageSource?.uri !== imageSource?.uri){
|
|
541
|
+
if (this.imageSource?.uri !== imageSource?.uri) {
|
|
487
542
|
this.imageSource = imageSource;
|
|
488
543
|
this.setState({ hideSource: true, ownUpdate: true });
|
|
489
544
|
}
|
|
490
|
-
}
|
|
545
|
+
};
|
|
491
546
|
|
|
492
547
|
renderSubIcon = () => {
|
|
493
548
|
const { subIcon, size, subIconSource } = this.props;
|
|
494
|
-
if ((!subIcon || subIcon === SubIconType.none) && !subIconSource)
|
|
549
|
+
if ((!subIcon || subIcon === SubIconType.none) && !subIconSource)
|
|
550
|
+
return <View />;
|
|
495
551
|
const subIconSizeStyle = this.mapSubIconSize(size);
|
|
496
552
|
const iconSource = ValueUtil.getImageSource(subIconSource);
|
|
497
553
|
return (
|
|
498
554
|
<View style={styles.subIcon}>
|
|
499
|
-
{subIcon === SubIconType.momo
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
{subIcon === SubIconType.online
|
|
508
|
-
|
|
555
|
+
{subIcon === SubIconType.momo ? (
|
|
556
|
+
<Image
|
|
557
|
+
style={[styles.subIconImage, subIconSizeStyle]}
|
|
558
|
+
source={IconSource.ic_round_momo_tiny}
|
|
559
|
+
/>
|
|
560
|
+
) : (
|
|
561
|
+
<View />
|
|
562
|
+
)}
|
|
563
|
+
{subIcon === SubIconType.online ? (
|
|
564
|
+
<View
|
|
565
|
+
style={[styles.subIconImageOnline, subIconSizeStyle]}
|
|
566
|
+
/>
|
|
567
|
+
) : (
|
|
568
|
+
<View />
|
|
569
|
+
)}
|
|
509
570
|
|
|
510
571
|
{iconSource && subIconSource && !subIcon ? (
|
|
511
572
|
<Image
|
|
512
573
|
style={[styles.subIconImage, subIconSizeStyle]}
|
|
513
574
|
source={iconSource}
|
|
514
575
|
/>
|
|
515
|
-
)
|
|
516
|
-
|
|
576
|
+
) : (
|
|
577
|
+
<View />
|
|
578
|
+
)}
|
|
517
579
|
</View>
|
|
518
580
|
);
|
|
519
|
-
}
|
|
581
|
+
};
|
|
520
582
|
|
|
521
583
|
getAvatarColor(name) {
|
|
522
|
-
const shortName = this.getContactShortName(name)
|
|
523
|
-
if (!shortName) return
|
|
524
|
-
const firstLetter = shortName[0].charCodeAt(0)
|
|
525
|
-
let colorIndex = Number(firstLetter) % 26
|
|
526
|
-
if (colorIndex > 19) colorIndex = 25 - colorIndex
|
|
527
|
-
return COLORS[colorIndex]
|
|
584
|
+
const shortName = this.getContactShortName(name);
|
|
585
|
+
if (!shortName) return '#d5d6d6';
|
|
586
|
+
const firstLetter = shortName[0].charCodeAt(0);
|
|
587
|
+
let colorIndex = Number(firstLetter) % 26;
|
|
588
|
+
if (colorIndex > 19) colorIndex = 25 - colorIndex;
|
|
589
|
+
return COLORS[colorIndex];
|
|
528
590
|
}
|
|
529
591
|
|
|
530
592
|
onPress = () => {
|
|
531
593
|
const { onPress } = this.props;
|
|
532
594
|
if (onPress && typeof onPress === 'function') onPress();
|
|
533
|
-
}
|
|
595
|
+
};
|
|
534
596
|
|
|
535
597
|
render() {
|
|
536
598
|
const {
|
|
537
|
-
size,
|
|
599
|
+
size,
|
|
600
|
+
source,
|
|
601
|
+
name,
|
|
602
|
+
resizeMode,
|
|
603
|
+
style,
|
|
604
|
+
onPress,
|
|
605
|
+
isShowLoading,
|
|
606
|
+
loading,
|
|
607
|
+
cached,
|
|
608
|
+
extraPropsImage,
|
|
609
|
+
quality,
|
|
538
610
|
} = this.props;
|
|
539
611
|
const { hideSource } = this.state;
|
|
540
612
|
const avatarStyle = this.mapStyleFromSize(size);
|
|
541
613
|
const containerSize = this.extractSize(avatarStyle);
|
|
542
614
|
const imageSource = ValueUtil.getImageSource(source);
|
|
543
|
-
const showName =
|
|
615
|
+
const showName =
|
|
616
|
+
typeof imageSource === 'object' &&
|
|
617
|
+
Object.keys(imageSource).length === 0 &&
|
|
618
|
+
typeof imageSource !== 'number';
|
|
544
619
|
// add priority high for avatar
|
|
545
620
|
if (imageSource?.uri) {
|
|
546
621
|
imageSource.priority = 'high';
|
|
@@ -554,25 +629,32 @@ export default class Avatar extends Component {
|
|
|
554
629
|
|
|
555
630
|
const inner = (
|
|
556
631
|
<View style={[styles.container, containerSize, style]}>
|
|
557
|
-
{
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
632
|
+
{showName || hideSource ? (
|
|
633
|
+
this.renderShortName(name, avatarStyle, size, this.color)
|
|
634
|
+
) : (
|
|
635
|
+
<View />
|
|
636
|
+
)}
|
|
637
|
+
{this.isValidImageUrl(imageSource) && !hideSource ? (
|
|
638
|
+
<Image
|
|
639
|
+
cached={cached}
|
|
640
|
+
loading={loading || isShowLoading}
|
|
641
|
+
source={imageSource}
|
|
642
|
+
onError={() => this.onLoadSourceError(imageSource)}
|
|
643
|
+
style={avatarStyle}
|
|
644
|
+
resizeMode={resizeMode}
|
|
645
|
+
{...extraPropsImage}
|
|
646
|
+
/>
|
|
647
|
+
) : (
|
|
648
|
+
<View />
|
|
649
|
+
)}
|
|
570
650
|
{this.renderSubIcon()}
|
|
571
651
|
</View>
|
|
572
652
|
);
|
|
573
653
|
if (onPress) {
|
|
574
654
|
return (
|
|
575
|
-
<TouchableOpacity
|
|
655
|
+
<TouchableOpacity
|
|
656
|
+
activeOpacity={onPress ? 0.5 : 1}
|
|
657
|
+
onPress={this.onPress}>
|
|
576
658
|
{inner}
|
|
577
659
|
</TouchableOpacity>
|
|
578
660
|
);
|
|
@@ -586,16 +668,25 @@ Avatar.propTypes = {
|
|
|
586
668
|
resizeMode: PropTypes.string,
|
|
587
669
|
quality: PropTypes.oneOf(['low', 'medium', 'high']),
|
|
588
670
|
size: PropTypes.any,
|
|
589
|
-
source: PropTypes.oneOfType([
|
|
671
|
+
source: PropTypes.oneOfType([
|
|
672
|
+
PropTypes.shape({ uri: PropTypes.string }),
|
|
673
|
+
PropTypes.number,
|
|
674
|
+
PropTypes.string,
|
|
675
|
+
]),
|
|
590
676
|
subIcon: PropTypes.oneOf(['momo', 'online', 'key', 'none']),
|
|
591
|
-
subIconSource: PropTypes.oneOfType([
|
|
677
|
+
subIconSource: PropTypes.oneOfType([
|
|
678
|
+
PropTypes.shape({ uri: PropTypes.string }),
|
|
679
|
+
PropTypes.number,
|
|
680
|
+
PropTypes.string,
|
|
681
|
+
]),
|
|
592
682
|
style: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
|
|
593
683
|
onPress: PropTypes.func,
|
|
594
684
|
loading: PropTypes.bool,
|
|
595
685
|
cached: PropTypes.bool,
|
|
596
686
|
scaleSize: PropTypes.bool,
|
|
597
687
|
extraPropsImage: PropTypes.object,
|
|
598
|
-
number: PropTypes.string
|
|
688
|
+
number: PropTypes.string,
|
|
689
|
+
custom: PropTypes.bool,
|
|
599
690
|
};
|
|
600
691
|
|
|
601
692
|
Avatar.defaultProps = {
|
|
@@ -603,5 +694,6 @@ Avatar.defaultProps = {
|
|
|
603
694
|
cached: true,
|
|
604
695
|
scaleSize: false,
|
|
605
696
|
quality: 'medium',
|
|
606
|
-
size: 'small'
|
|
697
|
+
size: 'small',
|
|
698
|
+
custom: false,
|
|
607
699
|
};
|