@momo-kits/avatar 0.0.7 → 0.0.12

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.
Files changed (3) hide show
  1. package/Avatar.js +10 -11
  2. package/Avatar.web.js +11 -0
  3. 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 {
@@ -317,7 +317,7 @@ const shortName = (width) => ({
317
317
  export default class Avatar extends Component {
318
318
  constructor(props) {
319
319
  super(props);
320
- this.color = this.getAvatarColor();
320
+ this.color = this.getAvatarColor(props.name);
321
321
  this.state = {
322
322
  hideSource: false,
323
323
  ownUpdate: false,
@@ -333,7 +333,7 @@ export default class Avatar extends Component {
333
333
  this.subscription = Dimensions.addEventListener(
334
334
  'change',
335
335
  ({ window }) => {
336
- if(window?.height > 0 && window?.width > 0) {
336
+ if(window?.height > 0 && window?.width > 0 && Platform.OS === 'android') {
337
337
  this.setState({ dimensions: window });
338
338
  }
339
339
  }
@@ -518,14 +518,13 @@ export default class Avatar extends Component {
518
518
  );
519
519
  }
520
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';
521
+ getAvatarColor(name) {
522
+ const shortName = this.getContactShortName(name)
523
+ if (!shortName) return "#d5d6d6"
524
+ const firstLetter = shortName[0].charCodeAt(0)
525
+ let colorIndex = Number(firstLetter) % 26
526
+ if (colorIndex > 19) colorIndex = 25 - colorIndex
527
+ return COLORS[colorIndex]
529
528
  }
530
529
 
531
530
  onPress = () => {
package/Avatar.web.js CHANGED
@@ -293,6 +293,7 @@ const shortName = (width) => ({
293
293
  export default class Avatar extends Component {
294
294
  constructor(props) {
295
295
  super(props);
296
+ this.color = this.getAvatarColor(props.name);
296
297
  this.state = {
297
298
  hideSource: false,
298
299
  ownUpdate: false,
@@ -440,6 +441,16 @@ export default class Avatar extends Component {
440
441
  };
441
442
  }
442
443
 
444
+ getAvatarColor(name) {
445
+ const shortName = this.getContactShortName(name)
446
+ if (!shortName) return "#d5d6d6"
447
+ const firstLetter = shortName[0].charCodeAt(0)
448
+ let colorIndex = Number(firstLetter) % 26
449
+ if (colorIndex > 19) colorIndex = 25 - colorIndex
450
+ return COLORS[colorIndex]
451
+ }
452
+
453
+
443
454
  onLoadSourceError = () => {
444
455
  this.setState({ hideSource: true, ownUpdate: true });
445
456
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/avatar",
3
- "version": "0.0.7",
3
+ "version": "0.0.12",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "dependencies": {},