@momo-kits/avatar 0.0.4 → 0.0.8
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 +61 -6
- package/package.json +1 -1
package/Avatar.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
3
|
import React, { Component } from 'react';
|
|
4
4
|
import {
|
|
5
|
-
View, StyleSheet, TouchableOpacity, Dimensions
|
|
5
|
+
View, StyleSheet, TouchableOpacity, Dimensions, Platform
|
|
6
6
|
} from 'react-native';
|
|
7
7
|
import { get, isEqual } from 'lodash';
|
|
8
8
|
import {
|
|
@@ -35,6 +35,29 @@ export const AvatarSize = {
|
|
|
35
35
|
size96: 'size96'
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
const COLORS = [
|
|
39
|
+
'#3683f3',
|
|
40
|
+
'#ea63af',
|
|
41
|
+
'#f94889',
|
|
42
|
+
'#82b6da',
|
|
43
|
+
'#2fc093',
|
|
44
|
+
'#7b4a41',
|
|
45
|
+
'#ff97bc',
|
|
46
|
+
'#4130d7',
|
|
47
|
+
'#c3c3c3',
|
|
48
|
+
'#f7ae2f',
|
|
49
|
+
'#b04595',
|
|
50
|
+
'#57b748',
|
|
51
|
+
'#ff8900',
|
|
52
|
+
'#7841bf',
|
|
53
|
+
'#be3f3e',
|
|
54
|
+
'#c51515',
|
|
55
|
+
'#8e6472',
|
|
56
|
+
'#fed27d',
|
|
57
|
+
'#ff6f5d',
|
|
58
|
+
'#05b8ae',
|
|
59
|
+
];
|
|
60
|
+
|
|
38
61
|
export const SubIconType = {
|
|
39
62
|
momo: 'momo',
|
|
40
63
|
online: 'online',
|
|
@@ -280,6 +303,9 @@ const shortName = (width) => ({
|
|
|
280
303
|
medium: {
|
|
281
304
|
fontSize: ScaleSize(15, width)
|
|
282
305
|
},
|
|
306
|
+
middle: {
|
|
307
|
+
fontSize: ScaleSize(16, width)
|
|
308
|
+
},
|
|
283
309
|
large: {
|
|
284
310
|
fontSize: ScaleSize(20, width)
|
|
285
311
|
},
|
|
@@ -291,6 +317,7 @@ const shortName = (width) => ({
|
|
|
291
317
|
export default class Avatar extends Component {
|
|
292
318
|
constructor(props) {
|
|
293
319
|
super(props);
|
|
320
|
+
this.color = this.getAvatarColor();
|
|
294
321
|
this.state = {
|
|
295
322
|
hideSource: false,
|
|
296
323
|
ownUpdate: false,
|
|
@@ -306,7 +333,7 @@ export default class Avatar extends Component {
|
|
|
306
333
|
this.subscription = Dimensions.addEventListener(
|
|
307
334
|
'change',
|
|
308
335
|
({ window }) => {
|
|
309
|
-
if(window?.height > 0 && window?.width > 0) {
|
|
336
|
+
if(window?.height > 0 && window?.width > 0 && Platform.OS === 'android') {
|
|
310
337
|
this.setState({ dimensions: window });
|
|
311
338
|
}
|
|
312
339
|
}
|
|
@@ -344,12 +371,24 @@ export default class Avatar extends Component {
|
|
|
344
371
|
return alphabet.join('');
|
|
345
372
|
}
|
|
346
373
|
|
|
347
|
-
|
|
374
|
+
hashUrl(url) {
|
|
375
|
+
let hash = 0;
|
|
376
|
+
let char;
|
|
377
|
+
if (url?.length === 0) return hash;
|
|
378
|
+
for (let i = 0; i < url?.length; i++) {
|
|
379
|
+
char = url?.charCodeAt(i);
|
|
380
|
+
hash = ((hash << 5) - hash) + char;
|
|
381
|
+
hash &= hash; // Convert to 32bit integer
|
|
382
|
+
}
|
|
383
|
+
return hash;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
renderShortName = (name, avatarStyle, size, color,) => {
|
|
348
387
|
if (name) {
|
|
349
388
|
const shortedName = this.getContactShortName(name);
|
|
350
389
|
const shortNameStyle = this.mapShortNameStyle(size);
|
|
351
390
|
return (
|
|
352
|
-
<View style={[styles.shortNameView, avatarStyle]}>
|
|
391
|
+
<View style={[styles.shortNameView, avatarStyle, { backgroundColor: color }]}>
|
|
353
392
|
<Text.H4 style={[styles.shortNameText, shortNameStyle]}>{shortedName}</Text.H4>
|
|
354
393
|
</View>
|
|
355
394
|
);
|
|
@@ -405,6 +444,10 @@ export default class Avatar extends Component {
|
|
|
405
444
|
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).medium;
|
|
406
445
|
break;
|
|
407
446
|
}
|
|
447
|
+
case AvatarSize.size48: {
|
|
448
|
+
shortNameStyle = shortName(dimensions?.width || null).middle;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
408
451
|
case AvatarSize.large: {
|
|
409
452
|
shortNameStyle = shortName(dimensions?.width > 0 ? dimensions?.width : null).large;
|
|
410
453
|
break;
|
|
@@ -475,6 +518,16 @@ export default class Avatar extends Component {
|
|
|
475
518
|
);
|
|
476
519
|
}
|
|
477
520
|
|
|
521
|
+
getAvatarColor() {
|
|
522
|
+
const { source } = this.props;
|
|
523
|
+
const number = Math.abs(this.hashUrl(source));
|
|
524
|
+
if (number && !isNaN(Number(number))) {
|
|
525
|
+
const colorIndex = Number(number) % COLORS.length;
|
|
526
|
+
return COLORS[colorIndex];
|
|
527
|
+
}
|
|
528
|
+
return '#d5d6d6';
|
|
529
|
+
}
|
|
530
|
+
|
|
478
531
|
onPress = () => {
|
|
479
532
|
const { onPress } = this.props;
|
|
480
533
|
if (onPress && typeof onPress === 'function') onPress();
|
|
@@ -502,7 +555,7 @@ export default class Avatar extends Component {
|
|
|
502
555
|
|
|
503
556
|
const inner = (
|
|
504
557
|
<View style={[styles.container, containerSize, style]}>
|
|
505
|
-
{(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : <View />}
|
|
558
|
+
{(showName || hideSource) ? this.renderShortName(name, avatarStyle, size, this.color) : <View />}
|
|
506
559
|
{this.isValidImageUrl(imageSource) && !hideSource
|
|
507
560
|
? (
|
|
508
561
|
<Image
|
|
@@ -545,7 +598,8 @@ Avatar.propTypes = {
|
|
|
545
598
|
loading: PropTypes.bool,
|
|
546
599
|
cached: PropTypes.bool,
|
|
547
600
|
scaleSize: PropTypes.bool,
|
|
548
|
-
extraPropsImage: PropTypes.object
|
|
601
|
+
extraPropsImage: PropTypes.object,
|
|
602
|
+
number: PropTypes.string
|
|
549
603
|
};
|
|
550
604
|
|
|
551
605
|
Avatar.defaultProps = {
|
|
@@ -553,4 +607,5 @@ Avatar.defaultProps = {
|
|
|
553
607
|
cached: true,
|
|
554
608
|
scaleSize: false,
|
|
555
609
|
quality: 'medium',
|
|
610
|
+
size: 'small'
|
|
556
611
|
};
|