forked from lufinkey/react-native-spotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerScreen.js
86 lines (74 loc) · 1.67 KB
/
PlayerScreen.js
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
import React, { PureComponent } from 'react';
import {
Alert,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import Spotify from 'rn-spotify-sdk';
export default class PlayerScreen extends PureComponent {
static navigationOptions = {
title: 'Player',
};
constructor(props) {
super(props);
this.state = {
spotifyUserName: null
};
this.spotifyLogoutButtonWasPressed = this.spotifyLogoutButtonWasPressed.bind(this);
}
componentDidMount() {
// send api request to get user info
Spotify.getMe().then((result) => {
// update state with user info
this.setState({ spotifyUserName: result.display_name });
// play song
return Spotify.playURI("spotify:track:2zk7TQx9Xa4yxYmsjgDCPp", 0, 0);
}).then(() => {
// success
}).catch((error) => {
// error
Alert.alert("Error", error.message);
});
}
goToInitialScreen() {
this.props.navigation.navigate('initial');
}
spotifyLogoutButtonWasPressed() {
Spotify.logout().finally(() => {
this.goToInitialScreen();
});
}
render() {
return (
<View style={styles.container}>
{ this.state.spotifyUserName!=null ? (
<Text style={styles.greeting}>
You are logged in as {this.state.spotifyUserName}
</Text>
) : (
<Text style={styles.greeting}>
Getting user info...
</Text>
)}
<TouchableHighlight onPress={this.spotifyLogoutButtonWasPressed}>
<Text>Logout</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
greeting: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});