@momo-kits/avatar 0.0.61-beta → 0.0.65-beta

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 +36 -3
  2. package/Avatar.web.js +36 -3
  3. package/package.json +1 -1
package/Avatar.js CHANGED
@@ -9,6 +9,12 @@ import {
9
9
  ValueUtil, Colors, Text, IconSource, Image, ScaleSize
10
10
  } from '@momo-kits/core';
11
11
 
12
+ export const AvatarQuality = {
13
+ low: 'low',
14
+ medium: 'medium',
15
+ high: 'high',
16
+ };
17
+
12
18
  export const AvatarSize = {
13
19
  tiny: 'tiny',
14
20
  small: 'small',
@@ -354,10 +360,24 @@ export default class Avatar extends Component {
354
360
  }
355
361
 
356
362
  isValidImageUrl = (source) => {
357
- const validSource = source && (((source.uri && this.isUrl(source.uri)) || (typeof source === 'number')));
363
+ const validSource = source && source.uri && this.isUrl(source.uri);
358
364
  return validSource;
359
365
  }
360
366
 
367
+ mapAvatarQuality = (url, quality) => {
368
+ if (!quality || quality === AvatarQuality.medium) {
369
+ return url;
370
+ }
371
+ if (quality === AvatarQuality.low) {
372
+ return url.replace('.png', '_s64.png');
373
+ }
374
+ if (quality === AvatarQuality.high) {
375
+ return url.replace('.png', '_s512.png');
376
+ }
377
+
378
+ return url;
379
+ }
380
+
361
381
  mapStyleFromSize = (size) => {
362
382
  const { dimensions } = this.state;
363
383
  const avatarStyle = avatarSize(dimensions?.width || null).small;
@@ -456,13 +476,24 @@ export default class Avatar extends Component {
456
476
 
457
477
  render() {
458
478
  const {
459
- size, source, name, resizeMode, style, onPress, isShowLoading, loading, cached, extraPropsImage
479
+ size, source, name, resizeMode, style, onPress, isShowLoading, loading, cached, extraPropsImage, quality
460
480
  } = this.props;
461
481
  const { hideSource } = this.state;
462
482
  const avatarStyle = this.mapStyleFromSize(size);
463
483
  const containerSize = this.extractSize(avatarStyle);
464
484
  const imageSource = ValueUtil.getImageSource(source);
465
485
  const showName = typeof imageSource === 'object' && Object.keys(imageSource).length === 0 && typeof imageSource !== 'number';
486
+ // add priority high for avatar
487
+ if (imageSource?.uri) {
488
+ imageSource.priority = 'high';
489
+ imageSource.uri = this.mapAvatarQuality(imageSource.uri, quality);
490
+ // if doest not have time query, set default cache time to 1 day
491
+ if (!imageSource.uri?.includes('?') && cached) {
492
+ const midnight = new Date().setHours(0, 0, 0, 0);
493
+ imageSource.uri = `${imageSource.uri}?time=${midnight}`;
494
+ }
495
+ }
496
+
466
497
  const inner = (
467
498
  <View style={[styles.container, containerSize, style]}>
468
499
  {(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : <View />}
@@ -495,6 +526,7 @@ export default class Avatar extends Component {
495
526
  Avatar.propTypes = {
496
527
  name: PropTypes.string,
497
528
  resizeMode: PropTypes.string,
529
+ quality: PropTypes.oneOf(['low', 'medium', 'high']),
498
530
  size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large', 'giant', 'size8', 'size24', 'size36', 'size44', 'size48', 'size52', 'size56', 'size64', 'size72', 'size80', 'size88', 'size96',
499
531
  PropTypes.shape({
500
532
  width: PropTypes.number, height: PropTypes.number, borderRadius: PropTypes.number
@@ -513,5 +545,6 @@ Avatar.propTypes = {
513
545
  Avatar.defaultProps = {
514
546
  resizeMode: 'cover',
515
547
  cached: true,
516
- scaleSize: false
548
+ scaleSize: false,
549
+ quality: 'medium',
517
550
  };
package/Avatar.web.js CHANGED
@@ -11,6 +11,12 @@ import { Icons as IconSource } from '../../core/icons';
11
11
  import Text from '../../core/components/typography';
12
12
  import { RFValueHorizontal as ScaleSize } from '../../core/components/typography/reponsiveSize';
13
13
 
14
+ export const AvatarQuality = {
15
+ low: 'low',
16
+ medium: 'medium',
17
+ high: 'high',
18
+ };
19
+
14
20
  export const AvatarSize = {
15
21
  tiny: 'tiny',
16
22
  small: 'small',
@@ -356,10 +362,24 @@ export default class Avatar extends Component {
356
362
  }
357
363
 
358
364
  isValidImageUrl = (source) => {
359
- const validSource = source && (((source.uri && this.isUrl(source.uri)) || (typeof source === 'number')));
365
+ const validSource = source && source.uri && this.isUrl(source.uri);
360
366
  return validSource;
361
367
  }
362
368
 
369
+ mapAvatarQuality = (url, quality) => {
370
+ if (!quality || quality === AvatarQuality.medium) {
371
+ return url;
372
+ }
373
+ if (quality === AvatarQuality.low) {
374
+ return url.replace('.png', '_s64.png');
375
+ }
376
+ if (quality === AvatarQuality.high) {
377
+ return url.replace('.png', '_s512.png');
378
+ }
379
+
380
+ return url;
381
+ }
382
+
363
383
  mapStyleFromSize = (size) => {
364
384
  const { dimensions } = this.state;
365
385
  const avatarStyle = avatarSize(dimensions?.width || null).small;
@@ -458,13 +478,24 @@ export default class Avatar extends Component {
458
478
 
459
479
  render() {
460
480
  const {
461
- size, source, name, resizeMode, style, onPress, isShowLoading, loading, cached, extraPropsImage
481
+ size, source, name, resizeMode, style, onPress, isShowLoading, loading, cached, extraPropsImage, quality
462
482
  } = this.props;
463
483
  const { hideSource } = this.state;
464
484
  const avatarStyle = this.mapStyleFromSize(size);
465
485
  const containerSize = this.extractSize(avatarStyle);
466
486
  const imageSource = ValueUtil.getImageSource(source);
467
487
  const showName = typeof imageSource === 'object' && Object.keys(imageSource).length === 0 && typeof imageSource !== 'number';
488
+ // add priority high for avatar
489
+ if (imageSource?.uri) {
490
+ imageSource.priority = 'high';
491
+ imageSource.uri = this.mapAvatarQuality(imageSource.uri, quality);
492
+ // if doest not have time query, set default cache time to 1 day
493
+ if (!imageSource.uri?.includes('?') && cached) {
494
+ const midnight = new Date().setHours(0, 0, 0, 0);
495
+ imageSource.uri = `${imageSource.uri}?time=${midnight}`;
496
+ }
497
+ }
498
+
468
499
  const inner = (
469
500
  <View style={[styles.container, containerSize, style]}>
470
501
  {(showName || hideSource) ? this.renderShortName(name, avatarStyle, size) : <View />}
@@ -497,6 +528,7 @@ export default class Avatar extends Component {
497
528
  Avatar.propTypes = {
498
529
  name: PropTypes.string,
499
530
  resizeMode: PropTypes.string,
531
+ quality: PropTypes.oneOf(['low', 'medium', 'high']),
500
532
  size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large', 'giant', 'size8', 'size24', 'size36', 'size44', 'size48', 'size52', 'size56', 'size64', 'size72', 'size80', 'size88', 'size96',
501
533
  PropTypes.shape({
502
534
  width: PropTypes.number, height: PropTypes.number, borderRadius: PropTypes.number
@@ -515,5 +547,6 @@ Avatar.propTypes = {
515
547
  Avatar.defaultProps = {
516
548
  resizeMode: 'cover',
517
549
  cached: true,
518
- scaleSize: false
550
+ scaleSize: false,
551
+ quality: 'medium',
519
552
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/avatar",
3
- "version": "0.0.61-beta",
3
+ "version": "0.0.65-beta",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "dependencies": {},