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

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-06-16 17:51:51 +02:00
import React from 'react';
2020-07-06 18:01:00 +02:00
import TrackPlayer, { usePlaybackState, STATE_PLAYING, STATE_PAUSED } from 'react-native-track-player';
2020-06-16 17:51:51 +02:00
import { TouchableOpacity } from 'react-native';
import styled from 'styled-components/native';
2020-06-21 10:30:41 +02:00
import { useHasQueue } from 'utility/useQueue';
2020-07-07 13:21:03 +02:00
import ForwardIcon from 'assets/forwards.svg';
import BackwardIcon from 'assets/backwards.svg';
import PlayIcon from 'assets/play.svg';
import PauseIcon from 'assets/pause.svg';
2020-07-26 17:02:18 +02:00
import { useColorScheme } from 'react-native-appearance';
2020-06-16 17:51:51 +02:00
2020-07-07 13:21:03 +02:00
const BUTTON_SIZE = 40;
2020-06-16 17:51:51 +02:00
const pause = () => TrackPlayer.pause();
const play = () => TrackPlayer.play();
const next = () => TrackPlayer.skipToNext();
const previous = () => TrackPlayer.skipToPrevious();
const Container = styled.View`
align-items: center;
margin: 20px 0;
`;
const Buttons = styled.View`
flex-direction: row;
align-items: center;
2020-06-16 21:41:02 +02:00
justify-content: space-between;
width: 100%;
2020-06-16 17:51:51 +02:00
`;
const Button = styled.View`
2020-06-16 21:41:02 +02:00
margin: 20px 40px;
2020-06-16 17:51:51 +02:00
`;
export default function MediaControls() {
2020-07-26 17:02:18 +02:00
const scheme = useColorScheme();
const fill = scheme === 'dark' ? '#ffffff' : '#000000';
2020-06-16 17:51:51 +02:00
return (
<Container>
<Buttons>
<Button>
2020-07-26 17:02:18 +02:00
<PreviousButton fill={fill} />
2020-06-16 17:51:51 +02:00
</Button>
2020-07-26 17:02:18 +02:00
<MainButton fill={fill} />
2020-06-16 17:51:51 +02:00
<Button>
2020-07-26 17:02:18 +02:00
<NextButton fill={fill} />
2020-06-16 17:51:51 +02:00
</Button>
</Buttons>
</Container>
);
}
2020-07-26 17:02:18 +02:00
export function PreviousButton({ fill }: { fill: string }) {
2020-06-16 17:51:51 +02:00
const hasQueue = useHasQueue();
return (
<TouchableOpacity onPress={previous} disabled={!hasQueue} style={{ opacity: hasQueue ? 1 : 0.5 }}>
2020-07-26 17:02:18 +02:00
<BackwardIcon width={BUTTON_SIZE} height={BUTTON_SIZE} fill={fill} />
2020-06-16 17:51:51 +02:00
</TouchableOpacity>
);
}
2020-07-26 17:02:18 +02:00
export function NextButton({ fill }: { fill: string }) {
2020-06-16 17:51:51 +02:00
const hasQueue = useHasQueue();
return (
<TouchableOpacity onPress={next} disabled={!hasQueue} style={{ opacity: hasQueue ? 1 : 0.5 }}>
2020-07-26 17:02:18 +02:00
<ForwardIcon width={BUTTON_SIZE} height={BUTTON_SIZE} fill={fill} />
2020-06-16 17:51:51 +02:00
</TouchableOpacity>
);
}
2020-07-26 17:02:18 +02:00
export function MainButton({ fill }: { fill: string }) {
2020-06-16 17:51:51 +02:00
const state = usePlaybackState();
switch (state) {
case STATE_PLAYING:
return (
<TouchableOpacity onPress={pause}>
2020-07-26 17:02:18 +02:00
<PauseIcon width={BUTTON_SIZE} height={BUTTON_SIZE} fill={fill} />
2020-06-16 17:51:51 +02:00
</TouchableOpacity>
);
case STATE_PAUSED:
return (
<TouchableOpacity onPress={play}>
2020-07-26 17:02:18 +02:00
<PlayIcon width={BUTTON_SIZE} height={BUTTON_SIZE} fill={fill} />
2020-06-16 17:51:51 +02:00
</TouchableOpacity>
);
default:
return (
<TouchableOpacity onPress={pause} disabled>
2020-07-26 17:02:18 +02:00
<PauseIcon width={BUTTON_SIZE} height={BUTTON_SIZE} fill={fill} />
2020-06-16 17:51:51 +02:00
</TouchableOpacity>
);
}
}