Files
jellyfin-audio-player/src/screens/Player/components/ProgressBar.tsx

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-06-16 22:50:33 +02:00
import React, { Component } from 'react';
import TrackPlayer from 'react-native-track-player';
2020-06-16 17:51:51 +02:00
import styled from 'styled-components/native';
2020-07-26 12:55:02 +02:00
import { Text, Platform } from 'react-native';
import Slider from '@react-native-community/slider';
2020-07-10 15:25:32 +02:00
import { THEME_COLOR } from 'CONSTANTS';
2020-07-26 14:45:32 +02:00
import { colors } from 'components/Colors';
2020-06-16 17:51:51 +02:00
const NumberBar = styled.View`
flex-direction: row;
justify-content: space-between;
width: 100%;
padding: 20px 0;
`;
function getSeconds(seconds: number): string {
2020-07-07 13:21:03 +02:00
return Math.floor(seconds % 60).toString().padStart(2, '0');
2020-06-16 17:51:51 +02:00
}
function getMinutes(seconds: number): number {
return Math.floor(seconds / 60);
}
2020-06-16 22:50:33 +02:00
interface State {
position: number;
duration: number;
gesture?: number;
2020-06-16 22:50:33 +02:00
}
export default class ProgressBar extends Component<{}, State> {
state: State = {
position: 0,
duration: 0,
}
timer: number = 0;
componentDidMount() {
this.timer = setInterval(this.updateProgress, 500);
}
componentWillUnmount() {
clearInterval(this.timer);
}
updateProgress = async () => {
const [position, duration] = await Promise.all([
TrackPlayer.getPosition(),
TrackPlayer.getDuration(),
]);
this.setState({ position, duration });
}
handleGesture = async (gesture: number) => {
2020-06-16 22:50:33 +02:00
// Set relative translation in state
this.setState({ gesture });
2020-06-16 22:50:33 +02:00
}
handleEndOfGesture = (position: number) => {
2020-06-16 22:50:33 +02:00
// Calculate and set the new position
TrackPlayer.seekTo(position);
this.setState({ gesture: undefined, position });
}
2020-06-16 22:50:33 +02:00
render() {
const { position, duration, gesture } = this.state;
2020-06-16 22:50:33 +02:00
return (
<>
<Slider
value={gesture || position}
minimumValue={0}
maximumValue={duration || 0}
onValueChange={this.handleGesture}
onSlidingComplete={this.handleEndOfGesture}
2020-07-10 15:25:32 +02:00
minimumTrackTintColor={THEME_COLOR}
2020-07-26 12:55:02 +02:00
thumbTintColor={Platform.OS === 'android' ? THEME_COLOR : undefined}
disabled={!duration}
/>
2020-06-16 22:50:33 +02:00
<NumberBar>
2020-07-26 14:45:32 +02:00
<Text style={colors.text}>{getMinutes(gesture || position)}:{getSeconds(gesture || position)}</Text>
<Text style={colors.text}>{getMinutes(duration)}:{getSeconds(duration)}</Text>
2020-06-16 22:50:33 +02:00
</NumberBar>
</>
);
}
2020-06-16 17:51:51 +02:00
}