@@ -88,6 +88,34 @@ ipcMain.on("toggle-play", (event, arg) => {
8888 ) ;
8989} ) ;
9090
91+ ipcMain . on ( "toggle-shuffle" , ( event , arg ) => {
92+ spotifyApi . getMyCurrentPlaybackState ( ) . then (
93+ function ( data ) {
94+ event . reply ( "toggle-shuffle-reply" , data ) ;
95+ } ,
96+ function ( err ) {
97+ event . reply ( "toggle-shuffle-reply" , err ) ;
98+ }
99+ )
100+ } )
101+
102+ ipcMain . on ( "cycle-repeat" , ( event , arg ) => {
103+ switch ( arg ) {
104+ case 'off' :
105+ spotifyApi . setRepeat ( { state : 'context' } )
106+ event . reply ( "repeat-reply" , 'context' )
107+ break ;
108+ case 'context' :
109+ spotifyApi . setRepeat ( { state : 'track' } )
110+ event . reply ( "repeat-reply" , 'track' )
111+ break ;
112+ case 'track' :
113+ spotifyApi . setRepeat ( { state : 'off' } )
114+ event . reply ( "repeat-reply" , 'off' )
115+ break ;
116+ } ;
117+ } )
118+
91119ipcMain . on ( "control" , ( event , arg ) => {
92120 switch ( arg ) {
93121 case "play" :
@@ -106,10 +134,28 @@ ipcMain.on("control", (event, arg) => {
106134 } ) ;
107135 break ;
108136 case "backward" :
137+ spotifyApi . seek ( 0 ) . catch ( ( err ) => {
138+ catch_error ( err ) ;
139+ } ) ;
109140 spotifyApi . skipToPrevious ( ) . catch ( ( err ) => {
110141 catch_error ( err ) ;
111142 } ) ;
112143 break ;
144+ case "shuffle" :
145+ spotifyApi . getMyCurrentPlaybackState ( ) . then ( function ( data ) {
146+ if ( data . body . shuffle_state == true ) {
147+ spotifyApi . setShuffle ( { state : false } ) . then ( function ( data ) {
148+ } )
149+ event . reply ( "is_shuffle" , false ) ;
150+ }
151+ if ( data . body . shuffle_state == false ) {
152+ spotifyApi . setShuffle ( { state : true } ) . then ( function ( data ) {
153+ } ) . catch ( ( err ) => {
154+ console . log ( err )
155+ } ) ;
156+ event . reply ( "is_shuffle" , true ) ;
157+ }
158+ } )
113159 }
114160} ) ;
115161
@@ -181,6 +227,22 @@ ipcMain.on("buttons", (event, arg) => {
181227 }
182228} ) ;
183229
230+ ipcMain . on ( "search" , ( event , args ) => {
231+ console . log ( 'searching for ' , args )
232+ spotifyApi . search ( args , [ 'track' ] , { limit : 1 } ) . then ( function ( data ) {
233+ if ( data . body . tracks . items [ 0 ] ) {
234+ var imgURL = data . body . tracks . items [ 0 ] . album . images [ 0 ] . url ;
235+ console . log ( imgURL ) ;
236+ event . reply ( "local-reply" , imgURL ) ;
237+ } else {
238+ console . log ( 'no image' ) ;
239+ event . reply ( "local-reply" , '' )
240+ }
241+ } , function ( err ) {
242+ console . log ( err )
243+ } ) . catch ( ( err ) => catch_error ( err ) )
244+ } )
245+
184246function restart_express ( ) {
185247 server . listen ( 8080 , "localhost" ) ;
186248}
@@ -198,6 +260,7 @@ express.get("/callback", function (req, res) {
198260 function ( data ) {
199261 // Set the access token on the API object to use it in later calls
200262 spotifyApi . setAccessToken ( data . body [ "access_token" ] ) ;
263+ console . log ( data . body [ "access_token" ] )
201264 spotifyApi . setRefreshToken ( data . body [ "refresh_token" ] ) ;
202265 win . loadFile ( "./src/index.html" ) ;
203266 setInterval ( refresh , ( data . body [ "expires_in" ] - 10 ) * 1000 ) ;
@@ -228,8 +291,6 @@ function refresh() {
228291
229292function catch_error ( error ) {
230293 console . log ( error ) ;
231- if ( error . statusCode == 403 ) {
232- }
233294}
234295
235296app . whenReady ( ) . then ( createWindow ) ;
0 commit comments