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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  56. /**
  57. * Implements {@code Component#componentDidMount}.
  58. *
  59. * @inheritdoc
  60. */
  61. componentDidMount() {
  62. this._maybeToggleAnimation({}, this.props);
  63. }
  64. /**
  65. * Implements {@code Component#componentDidUpdate}.
  66. *
  67. * @inheritdoc
  68. */
  69. componentDidUpdate(prevProps: Props) {
  70. this._maybeToggleAnimation(prevProps, this.props);
  71. }
  72. /**
  73. * Implements React {@link Component}'s render.
  74. *
  75. * @inheritdoc
  76. */
  77. render() {
  78. const { status, label, style } = this.props;
  79. let extraStyle = null;
  80. switch (status) {
  81. case STATUS_IN_PROGRESS:
  82. extraStyle = {
  83. opacity: this.state.pulseAnimation
  84. };
  85. break;
  86. case STATUS_OFF:
  87. extraStyle = styles.labelOff;
  88. break;
  89. }
  90. return (
  91. <Animated.View
  92. style = { [
  93. combineStyles(styles.indicatorContainer, style),
  94. extraStyle
  95. ] }>
  96. <Text style = { styles.indicatorText }>
  97. { label }
  98. </Text>
  99. </Animated.View>
  100. );
  101. }
  102. /**
  103. * Checks if the animation has to be started or stopped and acts
  104. * accordingly.
  105. *
  106. * @param {Props} oldProps - The previous values of the Props.
  107. * @param {Props} newProps - The new values of the Props.
  108. * @returns {void}
  109. */
  110. _maybeToggleAnimation(oldProps, newProps) {
  111. const { status: oldStatus } = oldProps;
  112. const { status: newStatus } = newProps;
  113. const { pulseAnimation } = this.state;
  114. if (newStatus === STATUS_IN_PROGRESS
  115. && oldStatus !== STATUS_IN_PROGRESS) {
  116. // Animation must be started
  117. this.animationReference = Animated.loop(Animated.sequence([
  118. Animated.timing(pulseAnimation, {
  119. delay: 500,
  120. toValue: 1,
  121. useNativeDriver: true
  122. }),
  123. Animated.timing(pulseAnimation, {
  124. toValue: 0.3,
  125. useNativeDriver: true
  126. })
  127. ]));
  128. this.animationReference.start();
  129. } else if (this.animationReference
  130. && newStatus !== STATUS_IN_PROGRESS
  131. && oldStatus === STATUS_IN_PROGRESS) {
  132. // Animation must be stopped
  133. this.animationReference.stop();
  134. }
  135. }
  136. }