-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathTransportControls.tsx
172 lines (164 loc) · 6.24 KB
/
TransportControls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useContext, useCallback } from 'react';
import { View, Button, Text, Toast, Switch, Segment, Icon } from 'native-base';
import styles from '../styles';
import AppContext from '../AppContext';
import { RepeatMode } from 'react-native-spotify-remote';
import Slider from '@react-native-community/slider';
import { Duration } from 'luxon';
/**
* Converts a duration (in ms) to a human readable string
*
* @export
* @param {number} durationMs - Duration in Milliseconds
* @returns
*/
export function displayDuration(durationMs: number) {
const d = Duration.fromMillis(durationMs);
return d.hours > 0 ?
d.toFormat("h:mm:ss")
: d.toFormat("m:ss");
}
const TransportControls: React.SFC = () => {
const {
playerState: {
isPaused = false,
playbackOptions: {
isShuffling = false,
repeatMode = RepeatMode.Off
} = {},
playbackPosition = 0,
playbackRestrictions:{
canRepeatContext = true,
canRepeatTrack = true,
canSkipNext = true,
canSkipPrevious = true,
canToggleShuffle = true
} = {},
track: {
duration = 0,
name = "",
artist: {
name: artistName = ""
} = {}
} = {}
} = {},
onError,
remote
} = useContext(AppContext);
const buttons = [
{
content: <Icon name="md-skip-backward"/>,
action: async () => await remote.skipToPrevious(),
},
{
content: <Icon name="md-skip-forward"/>,
action: async () => await remote.skipToNext(),
}
];
// Put play/pause into the middle of the array
buttons.splice(buttons.length / 2, 0, isPaused ?
{
content: <Icon name="play"/>,
action: async () => await remote.resume()
}
:
{
content: <Icon name="pause"/>,
action: async () => await remote.pause()
}
);
return (
<View style={styles.transportContainer}>
<View style={{ display: "flex", alignItems: "center", justifyContent: "center", marginVertical: 10 }}>
<Text style={{ fontSize: 20, fontWeight: "500" }}>{name}</Text>
{artistName !== "" && <Text style={{ fontSize: 17, fontWeight: "300" }}>{artistName}</Text>}
</View>
<View style={{ display: "flex", flexDirection: "row", alignItems: "center" }}>
<Text style={[styles.textDuration, styles.textCentered]}>{displayDuration(playbackPosition)}</Text>
<Slider
minimumValue={0}
maximumValue={duration}
value={playbackPosition}
onSlidingComplete={(val) => {
remote.seek(Math.round(val));
}}
style={{ flex: 1 }}
/>
<Text style={[styles.textDuration, styles.textCentered]}>{displayDuration(duration)}</Text>
</View>
<View>
<Segment>
{buttons.map(({ content, action }, index) => (
<Button
key={`${index}`}
onPress={() => action().catch(onError)}
transparent
style={{
width: `${(100 / buttons.length)}%`,
height: 60,
backgroundColor: "#FFF",
borderLeftWidth: index === 0 ? 1 : 0,
display:'flex',
justifyContent:'center'
}}
>
{content}
</Button>
))}
</Segment>
<View style={{
marginVertical: 30,
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center'
}}>
<View style={{
display: 'flex',
flexDirection: 'column',
justifyContent: "space-between",
height: 50
}}>
<Switch
disabled={!canToggleShuffle}
value={isShuffling}
onValueChange={(val) => {
remote.setShuffling(val)
}}
/>
<Text>Shuffle</Text>
</View>
<Button
disabled={!canRepeatContext && !canRepeatTrack}
onPress={() => {
const newMode = (repeatMode + 1) % 3;
remote.setRepeatMode(newMode);
}}
rounded
>
<Text style={{ width: 150, textAlign: "center" }}>
{repeatMode === RepeatMode.Off && "Repeat Off"}
{repeatMode === RepeatMode.Track && "Repeat One"}
{repeatMode === RepeatMode.Context && "Repeat Context"}
</Text>
</Button>
</View>
</View>
<Button
onPress={() => {
remote.getPlayerState().then((pstate) => {
Toast.show({
text: pstate.isPaused ? "Spotify Paused" : `Currently playing "${pstate.track.name}"`,
position: "top",
duration: 2000,
type: "warning",
});
});
}}
info style={{ alignItems: "center" }}>
<Text style={[{ width: "100%" }, styles.textCentered]}>Refresh</Text>
</Button>
</View>
)
}
export default TransportControls;