You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CircularLabel.native.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import React from 'react';
  3. import { Animated, Text } from 'react-native';
  4. import { combineStyles, type StyleType } from '../../styles';
  5. import AbstractCircularLabel, {
  6. type Props as AbstractProps
  7. } from './AbstractCircularLabel';
  8. import styles from './styles';
  9. /**
  10. * Const for status string 'in progress'.
  11. */
  12. const STATUS_IN_PROGRESS = 'in_progress';
  13. /**
  14. * Const for status string 'off'.
  15. */
  16. const STATUS_OFF = 'off';
  17. type Props = AbstractProps & {
  18. /**
  19. * Status of the label. This prop adds some additional styles based on its
  20. * value. E.g. if status = off, it will render the label symbolising that
  21. * the thing it displays (e.g. recording) is off.
  22. */
  23. status: ('in_progress' | 'off' | 'on'),
  24. /**
  25. * Style of the label.
  26. */
  27. style?: ?StyleType
  28. };
  29. type State = {
  30. /**
  31. * An animation object handling the opacity changes of the in progress
  32. * label.
  33. */
  34. pulseAnimation: Object
  35. }
  36. /**
  37. * Renders a circular indicator to be used for status icons, such as recording
  38. * on, audio-only conference, video quality and similar.
  39. */
  40. export default class CircularLabel extends AbstractCircularLabel<Props, State> {
  41. /**
  42. * A reference to the started animation of this label.
  43. */
  44. animationReference: Object;
  45. /**
  46. * Instantiates a new instance of {@code CircularLabel}.
  47. *
  48. * @inheritdoc
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. this.state = {
  53. pulseAnimation: new Animated.Value(0)
  54. };
  55. this._maybeToggleAnimation({}, props);
  56. }
  57. /**
  58. * Implements {@code Component#componentWillReceiveProps}.
  59. *
  60. * @inheritdoc
  61. */
  62. componentWillReceiveProps(newProps: Props) {
  63. this._maybeToggleAnimation(this.props, newProps);
  64. }
  65. /**
  66. * Implements React {@link Component}'s render.
  67. *
  68. * @inheritdoc
  69. */
  70. render() {
  71. const { status, label, style } = this.props;
  72. let extraStyle = null;
  73. switch (status) {
  74. case STATUS_IN_PROGRESS:
  75. extraStyle = {
  76. opacity: this.state.pulseAnimation
  77. };
  78. break;
  79. case STATUS_OFF:
  80. extraStyle = styles.labelOff;
  81. break;
  82. }
  83. return (
  84. <Animated.View
  85. style = { [
  86. combineStyles(styles.indicatorContainer, style),
  87. extraStyle
  88. ] }>
  89. <Text style = { styles.indicatorText }>
  90. { label }
  91. </Text>
  92. </Animated.View>
  93. );
  94. }
  95. /**
  96. * Checks if the animation has to be started or stopped and acts
  97. * accordingly.
  98. *
  99. * @param {Props} oldProps - The previous values of the Props.
  100. * @param {Props} newProps - The new values of the Props.
  101. * @returns {void}
  102. */
  103. _maybeToggleAnimation(oldProps, newProps) {
  104. const { status: oldStatus } = oldProps;
  105. const { status: newStatus } = newProps;
  106. const { pulseAnimation } = this.state;
  107. if (newStatus === STATUS_IN_PROGRESS
  108. && oldStatus !== STATUS_IN_PROGRESS) {
  109. // Animation must be started
  110. this.animationReference = Animated.loop(Animated.sequence([
  111. Animated.timing(pulseAnimation, {
  112. delay: 500,
  113. toValue: 1,
  114. useNativeDriver: true
  115. }),
  116. Animated.timing(pulseAnimation, {
  117. toValue: 0.3,
  118. useNativeDriver: true
  119. })
  120. ]));
  121. this.animationReference.start();
  122. } else if (this.animationReference
  123. && newStatus !== STATUS_IN_PROGRESS
  124. && oldStatus === STATUS_IN_PROGRESS) {
  125. // Animation must be stopped
  126. this.animationReference.stop();
  127. }
  128. }
  129. }