-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathSpotifyContent.tsx
164 lines (149 loc) · 5.11 KB
/
SpotifyContent.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
import React, { useState, useCallback, useEffect, useContext } from "react";
import { ContentItem, remote } from "react-native-spotify-remote";
import { ActionSheet, View, Button, Text } from "native-base";
import { FlatList } from "react-native";
import SpotifyContentListItem from "./SpotifyContentListItem";
import AppContext from "../AppContext";
const SpotifyContent: React.SFC = () => {
const { isConnected, onError } = useContext(AppContext)
const [parentItems, setParentItems] = useState<ContentItem[]>([]);
const [recommended, setRecommended] = useState<boolean>(true);
// The current parent is the last parent if there are any
const currentItem = parentItems[parentItems.length - 1];
const back = useCallback(() => {
if (parentItems.length === 1) {
return;
} else {
const newParents = [...parentItems];
newParents.pop();
setParentItems(newParents);
}
}, [parentItems]);
const pushParent = useCallback(async (nextParent: ContentItem) => {
try {
if (nextParent.container && nextParent.children == undefined || nextParent.children.length === 0) {
const loadedChildren = await remote.getChildrenOfItem(nextParent);
const newParent = {
...nextParent,
children: loadedChildren
}
setParentItems([...parentItems, newParent]);
} else {
setParentItems([...parentItems, nextParent]);
}
} catch (err) {
onError(err);
}
}, [parentItems])
const fetchItems = async () => {
try {
let retrieved: ContentItem[] = [];
if (recommended) {
retrieved = await remote.getRecommendedContentItems({ type: "default", flatten: false });
} else {
retrieved = await remote.getRootContentItems("default");
}
const rootItem: ContentItem = {
title: "",
availableOffline: false,
container: true,
id: "",
playable: false,
subtitle: "",
uri: "",
children: retrieved
}
setParentItems([rootItem]);
} catch (err) {
onError(err);
}
};
const showItemActions = useCallback((item: ContentItem) => {
const cancelOption = { text: 'Cancel', action: () => { } };
const actionOptions = [
{ text: 'Play Uri', action: () => remote.playUri(item.uri) },
{ text: 'Queue Uri', action: () => remote.queueUri(item.uri) },
{ text: 'Play Item', action: () => remote.playItem(item) },
]
if (item.uri.includes("spotify:playlist:") || item.uri.includes("spotify:album:")) {
actionOptions.push({
text: 'Play 4th track in playlist',
action: () => remote.playItemWithIndex(item, 3)
});
}
ActionSheet.show({
options: [
...actionOptions,
cancelOption
],
cancelButtonIndex: actionOptions.findIndex(({text})=>text===cancelOption.text),
title: item.title
}, async (index) => {
try {
const chosenOption = actionOptions[index];
if(chosenOption != undefined){
await chosenOption.action();
}
} catch (err) {
onError(err);
}
})
}, []);
const selectContentItem = useCallback(async (item: ContentItem) => {
if (item.container || !item.uri.toLowerCase().includes(":track:")) {
await pushParent(item);
} else if (item.playable) {
await showItemActions(item);
}
}, [pushParent])
useEffect(() => {
if (isConnected) {
fetchItems();
}
}, [isConnected, recommended]);
return (
<View>
{currentItem && (
<View style={{ display: 'flex', flexDirection: 'column' }}>
<View style={{ borderBottomColor: "gray", borderBottomWidth: 1, height: "9%" }}>
<View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
<Button
disabled={parentItems.length === 1}
bordered
small
style={{ margin: 5 }}
onPress={() => back()}
>
<Text>Back</Text>
</Button>
<Text style={{ flex: 1, fontSize: 20 }}>{currentItem.title}</Text>
</View>
</View>
<View style={{ height: "84%" }}>
<FlatList
data={currentItem.children}
renderItem={({ item }) => <SpotifyContentListItem
onPress={selectContentItem}
onLongPress={(it) => {
if (it.playable) {
showItemActions(it);
}
}}
item={item}
/>}
/>
</View>
<View style={{ display: 'flex', height: "7%", flexDirection: 'row', alignContent: "stretch" }}>
<Button dark style={{ flex: 1, height: "100%" }} full bordered={!recommended} onPress={() => setRecommended(true)}>
<Text>Recommended</Text>
</Button>
<Button dark style={{ flex: 1, height: "100%" }} full bordered={recommended} onPress={() => setRecommended(false)}>
<Text>Root</Text>
</Button>
</View>
</View>
)}
</View>
)
}
export default SpotifyContent;