-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
player connected and switch track to cadence
- Loading branch information
Showing
11 changed files
with
381 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { createContext, useContext, useState } from "react" | ||
|
||
export const PlayerContext = createContext(null) | ||
|
||
export const usePlayerContext = () => { | ||
const context = useContext(PlayerContext) | ||
if (!context) { | ||
throw new Error("PlayerContext must be used within PlayerContext.Provider") | ||
} | ||
return context | ||
} | ||
|
||
export const usePlayerContextValues = () => { | ||
const [currentSong, setCurrentSong] = useState(null) | ||
const [devices, setDevices] = useState([]) | ||
const [currentDevice, setCurrDevice] = useState(null) | ||
const getDevices = async (token: string) => { | ||
await ( | ||
await fetch(`https://api.spotify.com/v1/me/player/devices`, { | ||
headers: { | ||
Authorization: "Bearer " + token, | ||
}, | ||
}) | ||
) | ||
.json() | ||
.then((data) => { | ||
console.log("DATA", typeof data) | ||
setDevices( | ||
data.devices.map((device) => { | ||
return { | ||
id: device.id, | ||
name: device.name, | ||
} | ||
}) | ||
) | ||
}) | ||
} | ||
|
||
const playSong = async (song, token) => { | ||
console.log(song.uri) | ||
console.log(currentDevice) | ||
if (currentSong != song && currentDevice) { | ||
//add to queue | ||
|
||
await fetch( | ||
`https://api.spotify.com/v1/me/player/queue?uri=${song.uri}&device_id=${currentDevice.id}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Authorization: "Bearer " + token, | ||
}, | ||
} | ||
) | ||
|
||
//skip to added track | ||
await fetch(`https://api.spotify.com/v1/me/player/next`, { | ||
method: "POST", | ||
headers: { | ||
Authorization: "Bearer " + token, | ||
}, | ||
body: new URLSearchParams({ | ||
device_id: currentDevice.id, | ||
}).toString(), | ||
}) | ||
setCurrentSong(song) | ||
} | ||
} | ||
|
||
return { getDevices, devices, setCurrDevice, currentDevice, playSong } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createContext, useContext, useState } from "react"; | ||
import { getSongsFromPlaylist } from "../utils"; | ||
|
||
|
||
export const PlaylistContext = createContext(null) | ||
|
||
|
||
export const usePlaylistContext = () => { | ||
const context = useContext(PlaylistContext) | ||
|
||
if (!context){ | ||
throw new Error("PlaylistContext must be used within PlaylistContext.Provider") | ||
} | ||
return context | ||
} | ||
|
||
|
||
|
||
export const usePlaylistContextValues = () => { | ||
const [songs, setSongs] = useState({}) | ||
const [matchingTempoArr, setMatchingTempoArr] = useState([]) | ||
const [isPicked, setIsPicked] = useState(false) | ||
|
||
|
||
const handleSongsData = (href:string, token:string) =>{ | ||
getSongsFromPlaylist(href, token) | ||
.then(({idTempoArr, songData}) => { | ||
setSongs(songData) | ||
setMatchingTempoArr(idTempoArr) | ||
}) | ||
setIsPicked(true) | ||
} | ||
|
||
|
||
const findSuitableSong = (currentCadence:number) => { | ||
let min = matchingTempoArr[0]; | ||
let left = 0 | ||
let right = matchingTempoArr.length | ||
|
||
console.log(currentCadence) | ||
while (left<right){ | ||
let middle = Math.floor((right+left)/2) | ||
const distance = Math.abs(currentCadence - matchingTempoArr[middle][1]) | ||
if (distance < min[1]){ | ||
min = matchingTempoArr[middle] | ||
} | ||
if (currentCadence>matchingTempoArr[middle][1]){ | ||
right = middle | ||
} | ||
|
||
else{ | ||
left = middle+1 | ||
} | ||
} | ||
|
||
return songs[min[0]] | ||
} | ||
|
||
|
||
|
||
return {handleSongsData, findSuitableSong, isPicked} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.