-
Notifications
You must be signed in to change notification settings - Fork 101
/
AppContext.tsx
180 lines (158 loc) · 4.78 KB
/
AppContext.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 from 'react';
import {
auth,
remote,
ApiConfig,
ApiScope,
SpotifyRemoteApi,
PlayerState,
RepeatMode,
ContentItem,
SpotifyAuth
} from 'react-native-spotify-remote';
import {
SPOTIFY_CLIENT_ID,
SPOTIFY_REDIRECT_URL,
SPOTIFY_TOKEN_REFRESH_URL,
SPOTIFY_TOKEN_SWAP_URL
} from 'react-native-dotenv';
interface AuthOptions {
playURI?: string;
showDialog?: boolean;
autoConnect?: boolean;
}
interface AppContextState {
error?: string;
playerState?: PlayerState;
token?: string;
isConnected?: boolean;
}
export interface AppContextProps extends AppContextState {
onError: (err: Error) => void;
authenticate: (options?: AuthOptions) => void;
clearError: () => void;
endSession: () => void;
remote: SpotifyRemoteApi,
auth: SpotifyAuth
}
const noop = () => { };
const DefaultContext: AppContextProps = {
onError: noop,
authenticate: noop,
clearError: noop,
endSession: noop,
remote,
auth
}
const AppContext = React.createContext<AppContextProps>(DefaultContext);
class AppContextProvider extends React.Component<{}, AppContextState> {
state = {
isConnected: false
}
constructor(props: any) {
super(props);
this.onError = this.onError.bind(this);
this.authenticate = this.authenticate.bind(this);
this.clearError = this.clearError.bind(this);
this.onConnected = this.onConnected.bind(this);
this.onDisconnected = this.onDisconnected.bind(this);
this.onPlayerStateChanged = this.onPlayerStateChanged.bind(this);
this.endSession = this.endSession.bind(this);
}
componentDidMount() {
remote.on("remoteConnected", this.onConnected)
.on("remoteDisconnected", this.onDisconnected)
.on("playerStateChanged", this.onPlayerStateChanged);
auth.getSession().then((session) => {
if (session != undefined) {
this.setState((state) => ({ ...state, token: session.accessToken }))
remote.connect(session.accessToken)
.then(() => this.setState((state) => ({
...state,
isConnected: true
})))
.catch(this.onError);
}
});
}
componentWillUnmount() {
remote.removeAllListeners();
}
private onError(error: Error) {
this.setState((state) => ({ ...state, error: error.message }))
}
private clearError() {
this.setState((state) => ({ ...state, error: undefined }));
}
private onConnected() {
this.setState((state) => ({
...state,
isConnected: true
}));
}
private onDisconnected() {
this.setState((state) => ({
...state,
isConnected: false
}));
}
private onPlayerStateChanged(playerState: PlayerState) {
this.setState((state) => ({
...state,
playerState
}))
};
private endSession() {
auth.endSession().then(() => {
remote.disconnect().then(()=>{
this.setState({ isConnected: false, token: undefined });
});
});
}
private authenticate({ playURI, showDialog = false }: AuthOptions = {}) {
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,
playURI,
showDialog
};
// Go and check if things are connected
remote.isConnectedAsync().then(isConnected => {
this.setState((state) => ({
...state,
isConnected
}));
});
// Initialize the session
auth.initialize(config).then(newToken => {
// Automatically connect when authenticating
remote.connect(newToken);
this.setState((state) => ({
...state,
token: newToken
}));
}).catch(this.onError);
}
render() {
const { children } = this.props
return (
<AppContext.Provider
value={{
...DefaultContext,
...this.state,
onError: this.onError,
authenticate: this.authenticate,
clearError: this.clearError,
endSession: this.endSession
}}
>
{children}
</AppContext.Provider>
)
}
}
export default AppContext;
export { AppContextProvider };