Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
1hitsong committed Jul 28, 2024
1 parent 08b1eac commit d7d50f9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
1 change: 0 additions & 1 deletion components/Radio/RadioGrid.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 25 additions & 15 deletions components/scenes/BaseScene.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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"

Expand All @@ -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 = ""
Expand Down Expand Up @@ -167,4 +177,4 @@ function onKeyEvent(key as string, press as boolean) as boolean
end if

return false
end function
end function
1 change: 0 additions & 1 deletion components/tasks/GetRadios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<component name="GetRadios" extends="Task">
<interface>
<field id="clientID" type="string" />
<field id="radioID" type="integer" />
<field id="limit" type="integer" />
<field id="offset" type="integer" />
Expand Down
4 changes: 3 additions & 1 deletion components/tasks/GetTracks.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions source/utilities/utilities.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7d50f9

Please sign in to comment.