From d7d50f9d893228d2ed52c9837717645b34521308 Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sat, 27 Jul 2024 20:05:22 -0400 Subject: [PATCH] Code clean up --- components/Radio/RadioGrid.bs | 1 - components/scenes/BaseScene.bs | 40 +++++++++++++++++++++------------- components/tasks/GetRadios.xml | 1 - components/tasks/GetTracks.bs | 4 +++- source/utilities/utilities.bs | 9 ++++++++ 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/components/Radio/RadioGrid.bs b/components/Radio/RadioGrid.bs index cbe447c..c0aefd8 100644 --- a/components/Radio/RadioGrid.bs +++ b/components/Radio/RadioGrid.bs @@ -8,7 +8,6 @@ sub getData() m.getRadiosTask = createObject("roSGNode", "GetRadios") m.getRadiosTask.observeField("responseBody", "onResponseBodyLoaded") - m.getRadiosTask.clientID = `7a2ca6c8` m.getRadiosTask.limit = 50 m.getRadiosTask.offset = 0 m.getRadiosTask.control = "RUN" diff --git a/components/scenes/BaseScene.bs b/components/scenes/BaseScene.bs index 98fd9cb..a364788 100644 --- a/components/scenes/BaseScene.bs +++ b/components/scenes/BaseScene.bs @@ -32,7 +32,6 @@ sub onSelectedRadioChange() m.getRadiosTask = createObject("roSGNode", "GetRadios") m.getRadiosTask.observeField("responseBody", "onResponseBodyLoaded") - m.getRadiosTask.clientID = `7a2ca6c8` m.getRadiosTask.radioID = m.radioGrid.selectedRadio m.getRadiosTask.control = "RUN" end sub @@ -45,10 +44,9 @@ sub onResponseBodyLoaded() m.getTracksTask.observeField("responseBody", "onTracksLoaded") m.getTracksTask.params = { - client_id: `7a2ca6c8`, format: "jsonpretty", limit: 200, - lang: "en" + lang: "en", ' featured: 1, ' speed: "veryhigh", type: "single+albumtrack", @@ -86,30 +84,42 @@ sub onTracksLoaded() return end if - if m.getTracksTask.responseBody.results.count() = 0 + if isEmpty(m.getTracksTask.responseBody.results) noTracksFound() return end if ' Create an array of all possible song indexes - m.indexList = [] + indexList = [] for i = 0 to m.getTracksTask.responseBody.results.count() - 1 - m.indexList.push(i) + indexList.push(i) end for - ' Shuffle song indexes - for i = 0 to m.indexList.count() - 1 - j = Rnd(m.indexList.count() - 1) - temp = m.indexList[i] - m.indexList[i] = m.indexList[j] - m.indexList[j] = temp - end for + m.indexList = shuffleArray(indexList) ' Load 1st song m.audioPlayer.content = loadNextSong() m.audioPlayer.control = "play" end sub +function shuffleArray(input as object) as object + + if not isValid(input) then return [] + if isEmpty(input) then return [] + + arrayToShuffle = input.slice() + + ' Shuffle song indexes + for i = 0 to arrayToShuffle.count() - 1 + j = Rnd(arrayToShuffle.count() - 1) + temp = arrayToShuffle[i] + arrayToShuffle[i] = arrayToShuffle[j] + arrayToShuffle[j] = temp + end for + + return arrayToShuffle +end function + sub onStateChange() m.nowPlaying.isPaused = LCase(m.audioPlayer.state) = "paused" @@ -136,7 +146,7 @@ sub onStateChange() end if ' There are no more songs in the queue - if m.indexList.count() = 0 + if isEmpty(m.indexList) m.hideNowPlayingAnimation.control = "start" m.nowPlaying.trackArtist = "" m.nowPlaying.trackTitle = "" @@ -167,4 +177,4 @@ function onKeyEvent(key as string, press as boolean) as boolean end if return false -end function \ No newline at end of file +end function diff --git a/components/tasks/GetRadios.xml b/components/tasks/GetRadios.xml index 9dc252f..3ef2668 100644 --- a/components/tasks/GetRadios.xml +++ b/components/tasks/GetRadios.xml @@ -2,7 +2,6 @@ - diff --git a/components/tasks/GetTracks.bs b/components/tasks/GetTracks.bs index acb904e..5a2dae7 100644 --- a/components/tasks/GetTracks.bs +++ b/components/tasks/GetTracks.bs @@ -7,5 +7,7 @@ sub init() end sub sub getTracks() - m.top.responseBody = api.tracks.get(m.top.params) + params = m.top.params + params.AddReplace("client_id", m.global.clientID) + m.top.responseBody = api.tracks.get(params) end sub diff --git a/source/utilities/utilities.bs b/source/utilities/utilities.bs index 7c5d7bf..66a1ec8 100644 --- a/source/utilities/utilities.bs +++ b/source/utilities/utilities.bs @@ -7,6 +7,15 @@ function isValid(input as dynamic) as boolean return input <> invalid end function +' isEmpty: Returns whether or not passed object is empty +' +' @param {dynamic} input - item to be evaluated +' +' @return {boolean} indicating if passed object is empty +function isEmpty(input as object) as boolean + return input.count() = 0 +end function + ' isAllValid: Returns whether or not all items in passed array are valid ' ' @param {object} input - array of items to be evaluated