import { ActionButton } from './actionbutton/ActionButton'; import React, { type ReactNode, useCallback, useContext, useRef } from 'react'; import { PlayerContext } from '../util/PlayerContext'; import { ForwardSvg } from './svg/ForwardSvg'; import type { StyleProp, TextStyle } from 'react-native'; import { Animated, Easing, StyleSheet, Text, View } from 'react-native'; import { BackwardSvg } from './svg/BackwardSvg'; import type { ButtonBaseProps } from './ButtonBaseProps'; import { useSeekable } from '../../hooks/useSeekable'; export interface SkipButtonProps extends ButtonBaseProps { /** * The style overrides for the text in the skip button. */ textStyle?: StyleProp; /** /** * The skip value for the skip button. This can be set to negative to skip backwards. */ skip: number; /** * Whether the skip button should do a rotation animation when pressed instead of fading. */ rotate?: boolean; /** * The icon components used in the button. */ icon?: { forward: ReactNode; backward: ReactNode }; } const ANIMATION_DURATION_MSEC = 100; /** * The default skip button for the `react-native-theoplayer` UI. */ export function SkipButton(props: SkipButtonProps) { const { style, skip, rotate, icon, textStyle } = props; const spinValue = useRef(new Animated.Value(0)).current; const { player, ui, style: uiStyle } = useContext(PlayerContext); const seekable = useSeekable(); const spin = spinValue.interpolate({ inputRange: [0, 1], outputRange: skip >= 0 ? ['0deg', '360deg'] : ['360deg', '0deg'], }); const doSkip = useCallback(() => { // eslint-disable-next-line react-hooks/immutability player.currentTime = player.currentTime + skip * 1e3; if (rotate === true) { Animated.timing(spinValue, { toValue: 0.1, duration: ANIMATION_DURATION_MSEC, easing: Easing.linear, useNativeDriver: true, }).start(() => { Animated.timing(spinValue, { toValue: 0, duration: ANIMATION_DURATION_MSEC, easing: Easing.linear, useNativeDriver: true, }).start(); }); } }, [player, skip, rotate, spinValue]); const forwardSvg: ReactNode = icon?.forward ?? ; const backwardSvg: ReactNode = icon?.backward ?? ; if (seekable.length === 0) { return <>; } return ( { doSkip(); ui.onUserAction_(); }}> {Math.abs(skip)} ); }