Skip to content

Commit

Permalink
Merge pull request #20 from Gabe-H/updates
Browse files Browse the repository at this point in the history
support shuffle repeat local files
  • Loading branch information
Gabe-H authored Sep 18, 2020
2 parents 7d50616 + c19311a commit 2405b02
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 107 deletions.
65 changes: 63 additions & 2 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ ipcMain.on("toggle-play", (event, arg) => {
);
});

ipcMain.on("toggle-shuffle", (event, arg) => {
spotifyApi.getMyCurrentPlaybackState().then(
function (data) {
event.reply("toggle-shuffle-reply", data);
},
function (err) {
event.reply("toggle-shuffle-reply", err);
}
)
})

ipcMain.on("cycle-repeat", (event, arg) => {
switch (arg) {
case 'off':
spotifyApi.setRepeat({state: 'context'})
event.reply("repeat-reply", 'context')
break;
case 'context':
spotifyApi.setRepeat({state: 'track'})
event.reply("repeat-reply", 'track')
break;
case 'track':
spotifyApi.setRepeat({state: 'off'})
event.reply("repeat-reply", 'off')
break;
};
})

ipcMain.on("control", (event, arg) => {
switch (arg) {
case "play":
Expand All @@ -106,10 +134,28 @@ ipcMain.on("control", (event, arg) => {
});
break;
case "backward":
spotifyApi.seek(0).catch((err) => {
catch_error(err);
});
spotifyApi.skipToPrevious().catch((err) => {
catch_error(err);
});
break;
case "shuffle":
spotifyApi.getMyCurrentPlaybackState().then(function(data) {
if (data.body.shuffle_state == true) {
spotifyApi.setShuffle({state: false}).then(function(data) {
})
event.reply("is_shuffle", false);
}
if (data.body.shuffle_state == false) {
spotifyApi.setShuffle({state: true}).then(function(data) {
}).catch((err) => {
console.log(err)
});
event.reply("is_shuffle", true);
}
})
}
});

Expand Down Expand Up @@ -181,6 +227,22 @@ ipcMain.on("buttons", (event, arg) => {
}
});

ipcMain.on("search", (event, args) => {
console.log('searching for ', args)
spotifyApi.search(args, ['track'], {limit : 1}).then(function(data) {
if (data.body.tracks.items[0]) {
var imgURL = data.body.tracks.items[0].album.images[0].url;
console.log(imgURL);
event.reply("local-reply", imgURL);
} else {
console.log('no image');
event.reply("local-reply", '')
}
}, function (err) {
console.log(err)
}).catch((err) => catch_error(err))
})

function restart_express() {
server.listen(8080, "localhost");
}
Expand All @@ -198,6 +260,7 @@ express.get("/callback", function (req, res) {
function (data) {
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body["access_token"]);
console.log(data.body["access_token"])
spotifyApi.setRefreshToken(data.body["refresh_token"]);
win.loadFile("./src/index.html");
setInterval(refresh, (data.body["expires_in"] - 10) * 1000);
Expand Down Expand Up @@ -228,8 +291,6 @@ function refresh() {

function catch_error(error) {
console.log(error);
if (error.statusCode == 403) {
}
}

app.whenReady().then(createWindow);
5 changes: 4 additions & 1 deletion app/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ contextBridge.exposeInMainWorld("actions", {
maximize: () => ipcRenderer.send("buttons", "maximize"),
top: () => ipcRenderer.send("buttons", "top"),
topmac: () => ipcRenderer.send("buttons", "topmac"),
search: (args) => ipcRenderer.send("search", args)
});

contextBridge.exposeInMainWorld("controls", {
toggleplay: () => ipcRenderer.send("toggle-play", ""),
togglePlay: () => ipcRenderer.send("toggle-play", ""),
toggleShuffle: () => ipcRenderer.send("toggle-shuffle", ""),
cycleRepeat: (status) => ipcRenderer.send("cycle-repeat", status)
});

contextBridge.exposeInMainWorld("check", {
Expand Down
1 change: 1 addition & 0 deletions app/videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ initJSON();
setInterval(initJSON, 3600000);

ipcMain.on("isvideo", (event, arg) => {
console.log(arg)
var isSpecial = false;
for (i = 0; i < Object.keys(myJSON).length; i++) {
if (myJSON[i].id == arg) {
Expand Down
53 changes: 52 additions & 1 deletion src/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,25 @@ var togglePlay = function () {
var resetTime = setTimeout(() => {
hasToggled = false;
}, 1000);
window.controls.toggleplay();
window.controls.togglePlay();
};
var toggleShuffle = function () {
hasToggled = true;
clearInterval(resetTime);
var resetTime = setTimeout(() => {
hasToggled = false;
}, 1000);
control('shuffle')
window.controls.toggleShuffle();
};
var cycleRepeat = function () {
hasToggled = true;
clearInterval(resetTime);
var resetTime = setTimeout(() => {
hasToggled = false;
}, 1000);
window.controls.cycleRepeat(myRepeat);
}

ipcRenderer.on("toggle-play-reply", (event, data) => {
var isPlaying = data.body.is_playing;
Expand All @@ -30,6 +47,19 @@ ipcRenderer.on("toggle-play-reply", (event, data) => {
}
});

ipcRenderer.on("toggle-shuffle-reply", (event, data) => {
var isShuffle = data.body.shuffle_state;
if (isShuffle == true) {
$("#shuffle").removeClass().addClass("fa fa-random");
}
})

ipcRenderer.on("repeat-reply", (event, arg) => {
myRepeat = arg;
console.log(myRepeat)
set_repeat(myRepeat);
})

// Skip to next song in queue
var seek = function () {
console.log("Skipping forward");
Expand Down Expand Up @@ -74,6 +104,16 @@ $("#toggle").click(() => {
firing = false;
});

$("#shuffle").click(() => {
if (firing) return;
toggleShuffle();
firing = false;
});

$("#repeat").click(() => {
cycleRepeat();
});

$("#seek").click(() => {
if (firing) return;
seek();
Expand Down Expand Up @@ -154,6 +194,17 @@ ipcRenderer.on("not", (event, arg) => {
}
});

ipcRenderer.on("is_shuffle", (event, arg) => {
switch (arg) {
case true:
$("#shuffle").css("opacity", "100%");
break;
case false:
$("#shuffle").css("opacity", "");
break;
}
});

ipcRenderer.on("mac", (event, arg) => {
switch (arg) {
case true:
Expand Down
4 changes: 3 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ <h2 id="artist"></h2>
<div
style="position: absolute; bottom: 15px; text-align: center; width: 100%"
>
<i id="shuffle" class="fas fa-random"></i>
<i id="previous" class="fa fa-fast-backward"></i>
<i id="toggle"></i>
<i id="toggle" class="fas fa-play"></i>
<i id="seek" class="fa fa-fast-forward"></i>
<i id="repeat" class="fas fa-redo-alt"></i>
</div>
</footer>
<script type="text/javascript" src="control.js"></script>
Expand Down
Loading

0 comments on commit 2405b02

Please sign in to comment.