-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathConnectButton.tsx
68 lines (63 loc) · 2.16 KB
/
ConnectButton.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
import React, { useState, useContext, useCallback } from 'react';
import { View, Button, Text, Toast } from 'native-base';
import styles from '../styles';
import AppContext from '../AppContext';
import { Alert } from 'react-native';
const ConnectButton: React.SFC = () => {
const { isConnected, endSession, token, onError, remote } = useContext(AppContext)
const handleClick = useCallback(() => {
if (!isConnected && token != undefined) {
remote.connect(token).catch((e) => {
// Usually if we can't connect, its because
// spotify has been backgrounded and we need to
// reauth to bring it back
endSession();
});
} else {
remote.isConnectedAsync().then((connected) => {
Toast.show({
text: connected ? "Connected!" : "Not Connected",
type: connected ? "success" : "danger",
position: "top"
});
}).catch(onError)
}
}, [isConnected, token])
const handleLongPress = useCallback(()=>{
Alert.alert(
"Disonnect from Spotify?",
"Are you sure you want to disconnect?",
[
{
style:"cancel",
text:"Cancel",
},
{
style:"destructive",
text:"Disconnect",
onPress:()=>{
remote.disconnect().then(()=>{
Toast.show({
text:"Disconnected!",
type:"warning",
position:"top"
})
})
}
}
],
)
},[]);
return (
<Button
onPress={handleClick}
onLongPress={handleLongPress}
danger={!isConnected}
success={isConnected}
full={true}
>
<Text>{isConnected ? "Connected" : "Disconnected"}</Text>
</Button>
)
}
export default ConnectButton;