forked from cjam/react-native-spotify-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
180 lines (161 loc) · 4.58 KB
/
App.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
173
174
175
176
177
178
179
180
import React, { useState, useEffect } from 'react';
import { Platform, StyleSheet, View, FlatList, Alert } from 'react-native';
import { Button, Text } from 'native-base';
import { auth, remote, ApiConfig, ApiScope, SpotifyRemoteApi } from 'react-native-spotify-remote';
import {
SPOTIFY_CLIENT_ID,
SPOTIFY_REDIRECT_URL,
SPOTIFY_TOKEN_REFRESH_URL,
SPOTIFY_TOKEN_SWAP_URL
} from 'react-native-dotenv';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
const SpotifyStatus = (props: { isConnected: boolean }) => {
const { isConnected } = props;
return isConnected ? (
<Text style={styles.connected}>Connected to Spotify!</Text>
)
: (
<Text style={styles.disconnected}>Not Connected to Spotify!</Text>
)
}
const SpotifyControls = (props: { isConnected: boolean,onError:(err:Error)=>void }) => {
const { isConnected, onError } = props;
const actions = [
{
name:"Play Track",
action: async()=>{
await remote.playUri("spotify:track:5ckVDfifdJnKEabvjojde4");
}
},
{
name: "Play Epic Track",
action: async () =>{
await remote.playUri("spotify:track:6IA8E2Q5ttcpbuahIejO74");
await remote.seek(58000);
}
},
{
name: "Pause",
action: async () => await remote.pause()
}
]
return (
<View style={{ display: 'flex', flexDirection: 'column', alignItems: 'stretch', justifyContent: 'center', width: "100%", margin: 10, padding: 10 }}>
{actions.map(({ name, action }) => (
<Button
disabled={!isConnected}
key={name}
full primary
style={{ margin: 2, height: 70, opacity: isConnected ? 1.0 : 0.3, backgroundColor: isConnected ? undefined : 'gray' }}
onPress={async ()=>{
try{
await action();
}catch(err){
onError && onError(err);
}
}}
>
<Text>{name}</Text>
</Button>
))}
</View>
);
}
interface AppProps {
}
const App: React.FunctionComponent<AppProps> = (props) => {
const [isConnected, setIsConnected] = useState(false);
const [error, setError] = useState<string>();
// Setup our connect async function so that it can be called
// from useEffect (i.e. onComponentMount)
const connect = async () => {
const config: ApiConfig = {
clientID: SPOTIFY_CLIENT_ID,
redirectURL: SPOTIFY_REDIRECT_URL,
tokenRefreshURL: SPOTIFY_TOKEN_REFRESH_URL,
tokenSwapURL: SPOTIFY_TOKEN_SWAP_URL,
scope: ApiScope.AppRemoteControlScope // Can add more scopes here as flags i.e. Scope1 | Scope2
}
// Store handlers in variables so that we can remove them
const onConnected = () => {
setIsConnected(true);
}
const onDisconnected = () => {
setIsConnected(false);
}
// Add event listeners for connection / disconnection
remote.on("remoteConnected", onConnected)
.on("remoteDisconnected", onDisconnected);
try {
const token = await auth.initialize(config);
await remote.connect(token);
} catch (err) {
setError(err.message);
}
// Return our subscription cleanup function
return () => {
remote.off("remoteConnected", onConnected)
.off("remoteDisconnected", onDisconnected);
}
}
useEffect(() => {
connect();
}, []);
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>{instructions}</Text>
<Text style={styles.instructions}>{SPOTIFY_REDIRECT_URL}</Text>
<SpotifyStatus isConnected={isConnected} />
<SpotifyControls isConnected={isConnected} onError={(err)=>setError(err.message)} />
{error && (
<Text style={styles.error}>{error}</Text>
)}
</View>
);
};
export default App;
const styles = StyleSheet.create({
actionButton: {
borderColor: "#0000BB",
borderWidth: 2,
},
error: {
color: "#AA0000",
borderColor: "#AA0000",
borderWidth: 2,
fontSize: 18,
padding: 10,
margin: 10,
marginTop: 20
},
connected: {
color: "#00AA00",
fontSize: 16,
},
disconnected: {
color: "#880000",
fontSize: 16
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});