-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathInitialScreen.js
More file actions
151 lines (136 loc) · 2.94 KB
/
Copy pathInitialScreen.js
File metadata and controls
151 lines (136 loc) · 2.94 KB
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
import React, { PureComponent } from 'react';
import {
ActivityIndicator,
Alert,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import Spotify from 'rn-spotify-sdk';
export default class InitialScreen extends PureComponent {
static navigationOptions = {
header: null
};
constructor(props) {
super(props);
this.state = {
spotifyInitialized: false
};
this.spotifyLoginButtonWasPressed = this.spotifyLoginButtonWasPressed.bind(this);
}
goToPlayer() {
this.props.navigation.navigate('player');
}
async initializeIfNeeded() {
// initialize Spotify if it hasn't been initialized yet
if(!await Spotify.isInitializedAsync()) {
// initialize spotify
const spotifyOptions = {
"clientID":"<INSERT-YOUR-CLIENT-ID-HERE>",
"sessionUserDefaultsKey":"SpotifySession",
"redirectURL":"examplespotifyapp://auth",
"scopes":["user-read-private", "playlist-read", "playlist-read-private", "streaming"],
};
const loggedIn = await Spotify.initialize(spotifyOptions);
// update UI state
this.setState({
spotifyInitialized: true
});
// handle initialization
if(loggedIn) {
this.goToPlayer();
}
}
else {
// update UI state
this.setState({
spotifyInitialized: true
});
// handle logged in
if(await Spotify.isLoggedInAsync()) {
this.goToPlayer();
}
}
}
componentDidMount() {
this.initializeIfNeeded().catch((error) => {
Alert.alert("Error", error.message);
});
}
spotifyLoginButtonWasPressed() {
// log into Spotify
Spotify.login().then((loggedIn) => {
if(loggedIn) {
// logged in
this.goToPlayer();
}
else {
// cancelled
}
}).catch((error) => {
// error
Alert.alert("Error", error.message);
});
}
render() {
if(!this.state.spotifyInitialized) {
return (
<View style={styles.container}>
<ActivityIndicator animating={true} style={styles.loadIndicator}>
</ActivityIndicator>
<Text style={styles.loadMessage}>
Loading...
</Text>
</View>
);
}
else {
return (
<View style={styles.container}>
<Text style={styles.greeting}>
Hey! You! Log into your spotify
</Text>
<TouchableHighlight onPress={this.spotifyLoginButtonWasPressed} style={styles.spotifyLoginButton}>
<Text style={styles.spotifyLoginButtonText}>Log into Spotify</Text>
</TouchableHighlight>
</View>
);
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
loadIndicator: {
//
},
loadMessage: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
spotifyLoginButton: {
justifyContent: 'center',
borderRadius: 18,
backgroundColor: 'green',
overflow: 'hidden',
width: 200,
height: 40,
margin: 20,
},
spotifyLoginButtonText: {
fontSize: 20,
textAlign: 'center',
color: 'white',
},
greeting: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});