Skip to content

Commit

Permalink
save global ref to prevent deep copies
Browse files Browse the repository at this point in the history
  • Loading branch information
cewert committed Sep 30, 2024
1 parent 3a7882e commit d06dafa
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 186 deletions.
37 changes: 20 additions & 17 deletions components/ItemGrid/ItemGrid.bs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import "pkg:/source/roku_modules/log/LogMixin.brs"
sub init()
m.log = log.Logger("ItemGrid")
m.log.debug("start init()")
myGlobal = m.global

m.options = m.top.findNode("options")

m.showItemCount = m.global.session.user.settings["itemgrid.showItemCount"]
m.showItemCount = myGlobal.session.user.settings["itemgrid.showItemCount"]

m.tvGuide = invalid
m.channelFocused = invalid
Expand Down Expand Up @@ -68,9 +70,9 @@ sub init()
m.alphaMenu = m.alpha.findNode("alphaMenu")

'Get reset folder setting
m.resetGrid = m.global.session.user.settings["itemgrid.reset"]
m.resetGrid = myGlobal.session.user.settings["itemgrid.reset"]

m.top.gridTitles = m.global.session.user.settings["itemgrid.gridTitles"]
m.top.gridTitles = myGlobal.session.user.settings["itemgrid.gridTitles"]
m.log.debug("end init()")
end sub

Expand All @@ -84,6 +86,7 @@ sub loadInitialItems()
m.log.debug("start loadInitialItems()")
m.loadItemsTask.control = "stop"
startLoadingSpinner()
myGlobal = m.global

if m.top.parentItem.json.Type = "CollectionFolder" 'or m.top.parentItem.json.Type = "Folder"
m.top.HomeLibraryItem = m.top.parentItem.Id
Expand All @@ -96,25 +99,25 @@ sub loadInitialItems()
' Read view/sort/filter settings
if m.top.parentItem.collectionType = "livetv"
' Translate between app and server nomenclature
viewSetting = m.global.session.user.settings["display.livetv.landing"]
viewSetting = myGlobal.session.user.settings["display.livetv.landing"]
if viewSetting = "guide"
m.view = "tvGuide"
else
m.view = "livetv"
end if
m.sortField = m.global.session.user.settings["display.livetv.sortField"]
sortAscendingStr = m.global.session.user.settings["display.livetv.sortAscending"]
m.filter = m.global.session.user.settings["display.livetv.filter"]
m.sortField = myGlobal.session.user.settings["display.livetv.sortField"]
sortAscendingStr = myGlobal.session.user.settings["display.livetv.sortAscending"]
m.filter = myGlobal.session.user.settings["display.livetv.filter"]
else if m.top.parentItem.collectionType = "music"
m.view = m.global.session.user.settings["display.music.view"]
m.sortField = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
sortAscendingStr = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]
m.filter = m.global.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
m.view = myGlobal.session.user.settings["display.music.view"]
m.sortField = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
sortAscendingStr = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]
m.filter = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
else
m.sortField = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
sortAscendingStr = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]
m.filter = m.global.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
m.view = m.global.session.user.settings["display." + m.top.parentItem.Id + ".landing"]
m.sortField = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
sortAscendingStr = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]
m.filter = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
m.view = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".landing"]
end if

if m.sortField = invalid
Expand Down Expand Up @@ -180,7 +183,7 @@ sub loadInitialItems()
m.loadItemsTask.itemType = "MusicArtist"
m.loadItemsTask.itemId = m.top.parentItem.Id

m.view = m.global.session.user.settings["display.music.view"]
m.view = myGlobal.session.user.settings["display.music.view"]

if m.view = "music-album"
m.loadItemsTask.itemType = "MusicAlbum"
Expand All @@ -191,7 +194,7 @@ sub loadInitialItems()
' For LiveTV, we want to "Fit" the item images, not zoom
m.top.imageDisplayMode = "scaleToFit"

if m.global.session.user.settings["display.livetv.landing"] = "guide" and m.options.view <> "livetv"
if myGlobal.session.user.settings["display.livetv.landing"] = "guide" and m.options.view <> "livetv"
showTvGuide()
end if
else if m.top.parentItem.collectionType = "CollectionFolder" or m.top.parentItem.type = "CollectionFolder" or m.top.parentItem.collectionType = "boxsets" or m.top.parentItem.Type = "Boxset" or m.top.parentItem.Type = "Boxsets" or m.top.parentItem.Type = "Folder" or m.top.parentItem.Type = "Channel"
Expand Down
22 changes: 12 additions & 10 deletions components/ItemGrid/ItemGridOptions.bs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,13 @@ sub optionsSet()
m.menus[1].content = sortContent
m.menus[1].checkedItem = m.selectedSortIndex

myGlobal = m.global
if m.top.sortAscending = 1
m.menus[1].focusedCheckedIconUri = m.global.constants.icons.ascending_black
m.menus[1].checkedIconUri = m.global.constants.icons.ascending_white
m.menus[1].focusedCheckedIconUri = myGlobal.constants.icons.ascending_black
m.menus[1].checkedIconUri = myGlobal.constants.icons.ascending_white
else
m.menus[1].focusedCheckedIconUri = m.global.constants.icons.descending_black
m.menus[1].checkedIconUri = m.global.constants.icons.descending_white
m.menus[1].focusedCheckedIconUri = myGlobal.constants.icons.descending_black
m.menus[1].checkedIconUri = myGlobal.constants.icons.descending_white
end if
end if

Expand Down Expand Up @@ -300,9 +301,10 @@ function onKeyEvent(key as string, press as boolean) as boolean

' Handle Sort screen
if m.selectedItem = 1
myGlobal = m.global
if m.menus[1].itemSelected <> m.selectedSortIndex
m.menus[1].focusedCheckedIconUri = m.global.constants.icons.ascending_black
m.menus[1].checkedIconUri = m.global.constants.icons.ascending_white
m.menus[1].focusedCheckedIconUri = myGlobal.constants.icons.ascending_black
m.menus[1].checkedIconUri = myGlobal.constants.icons.ascending_white

m.selectedSortIndex = m.menus[1].itemSelected
m.top.sortAscending = true
Expand All @@ -311,12 +313,12 @@ function onKeyEvent(key as string, press as boolean) as boolean

if m.top.sortAscending = true
m.top.sortAscending = false
m.menus[1].focusedCheckedIconUri = m.global.constants.icons.descending_black
m.menus[1].checkedIconUri = m.global.constants.icons.descending_white
m.menus[1].focusedCheckedIconUri = myGlobal.constants.icons.descending_black
m.menus[1].checkedIconUri = myGlobal.constants.icons.descending_white
else
m.top.sortAscending = true
m.menus[1].focusedCheckedIconUri = m.global.constants.icons.ascending_black
m.menus[1].checkedIconUri = m.global.constants.icons.ascending_white
m.menus[1].focusedCheckedIconUri = myGlobal.constants.icons.ascending_black
m.menus[1].checkedIconUri = myGlobal.constants.icons.ascending_white
end if
end if
end if
Expand Down
45 changes: 26 additions & 19 deletions components/ItemGrid/LoadVideoContentTask.bs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@ sub init()
end sub

sub loadItems()
myGlobal = m.global
' Reset intro tracker in case task gets reused
m.top.isIntro = false

' Only show preroll once per queue
if m.global.queueManager.callFunc("isPrerollActive")
if myGlobal.queueManager.callFunc("isPrerollActive")
' Prerolls not allowed if we're resuming video
if m.global.queueManager.callFunc("getCurrentItem").startingPoint = 0
if myGlobal.queueManager.callFunc("getCurrentItem").startingPoint = 0
preRoll = GetIntroVideos(m.top.itemId)
if isValid(preRoll) and preRoll.TotalRecordCount > 0 and isValid(preRoll.items[0])
' If an error is thrown in the Intros plugin, instead of passing the error they pass the entire rick roll music video.
' Bypass the music video and treat it as an error message
if lcase(preRoll.items[0].name) <> "rick roll'd"
m.global.queueManager.callFunc("push", m.global.queueManager.callFunc("getCurrentItem"))
myGlobal.queueManager.callFunc("push", myGlobal.queueManager.callFunc("getCurrentItem"))
m.top.itemId = preRoll.items[0].id
m.global.queueManager.callFunc("setPrerollStatus", false)
myGlobal.queueManager.callFunc("setPrerollStatus", false)
m.top.isIntro = true
end if
end if
end if
end if

if m.top.selectedAudioStreamIndex = 0
currentItem = m.global.queueManager.callFunc("getCurrentItem")
currentItem = myGlobal.queueManager.callFunc("getCurrentItem")
if isValid(currentItem) and isValid(currentItem.json)
m.top.selectedAudioStreamIndex = FindPreferredAudioStream(currentItem.json.MediaStreams)
end if
Expand Down Expand Up @@ -79,6 +80,8 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s
return
end if

myGlobal = m.global

videotype = LCase(meta.type)

' Check for any Live TV streams or Recordings coming from other places other than the TV Guide
Expand Down Expand Up @@ -119,15 +122,15 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s
end if

if LCase(m.top.itemType) = "episode"
userSession = m.global.session.user
userSession = myGlobal.session.user
if userSession.settings["playback.playnextepisode"] = "enabled" or userSession.settings["playback.playnextepisode"] = "webclient" and userSession.Configuration.EnableNextEpisodeAutoPlay
addNextEpisodesToQueue(video.showID)
end if
end if

playbackPosition = 0!

currentItem = m.global.queueManager.callFunc("getCurrentItem")
currentItem = myGlobal.queueManager.callFunc("getCurrentItem")

if isValid(currentItem) and isValid(currentItem.startingPoint)
playbackPosition = currentItem.startingPoint
Expand Down Expand Up @@ -209,8 +212,8 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s
' transcode is that the Encoding Level is not supported, then try to direct play but silently
' fall back to the transcode if that fails.
if m.playbackInfo.MediaSources[0].MediaStreams.Count() > 0 and meta.live = false
tryDirectPlay = m.global.session.user.settings["playback.tryDirect.h264ProfileLevel"] and m.playbackInfo.MediaSources[0].MediaStreams[0].codec = "h264"
tryDirectPlay = tryDirectPlay or (m.global.session.user.settings["playback.tryDirect.hevcProfileLevel"] and m.playbackInfo.MediaSources[0].MediaStreams[0].codec = "hevc")
tryDirectPlay = myGlobal.session.user.settings["playback.tryDirect.h264ProfileLevel"] and m.playbackInfo.MediaSources[0].MediaStreams[0].codec = "h264"
tryDirectPlay = tryDirectPlay or (myGlobal.session.user.settings["playback.tryDirect.hevcProfileLevel"] and m.playbackInfo.MediaSources[0].MediaStreams[0].codec = "hevc")
if tryDirectPlay and isValid(m.playbackInfo.MediaSources[0].TranscodingUrl) and forceTranscoding = false
transcodingReasons = getTranscodeReasons(m.playbackInfo.MediaSources[0].TranscodingUrl)
if transcodingReasons.Count() = 1 and transcodingReasons[0] = "VideoLevelNotSupported"
Expand All @@ -226,7 +229,7 @@ sub LoadItems_AddVideoContent(video as object, mediaSourceId as dynamic, audio_s
else
if m.playbackInfo.MediaSources[0].TranscodingUrl = invalid
' If server does not provide a transcode URL, display a message to the user
m.global.sceneManager.callFunc("userMessage", tr("Error Getting Playback Information"), tr("An error was encountered while playing this item. Server did not provide required transcoding data."))
myGlobal.sceneManager.callFunc("userMessage", tr("Error Getting Playback Information"), tr("An error was encountered while playing this item. Server did not provide required transcoding data."))
video.errorMsg = "Error getting playback information"
video.content = invalid
return
Expand All @@ -250,7 +253,9 @@ end sub
' @param {dynamic} videoID - id of video user is playing
' @return {integer} indicating the default track's server-side index. Defaults to {SubtitleSelection.none} is one is not found
function defaultSubtitleTrackFromVid(videoID) as integer
if m.global.session.user.configuration.SubtitleMode = "None"
myGlobal = m.global

if myGlobal.session.user.configuration.SubtitleMode = "None"
return SubtitleSelection.none ' No subtitles desired: return none
end if

Expand All @@ -276,7 +281,7 @@ function defaultSubtitleTrackFromVid(videoID) as integer
return defaultTextSubs
end if

if not m.global.session.user.settings["playback.subs.onlytext"]
if not myGlobal.session.user.settings["playback.subs.onlytext"]
return defaultSubtitleTrack(subtitles["all"], selectedAudioLanguage) ' if no appropriate text subs exist, allow non-text
end if

Expand Down Expand Up @@ -476,20 +481,21 @@ end function

' Add next episodes to the playback queue
sub addNextEpisodesToQueue(showID)
myGlobal = m.global
' Don't queue next episodes if we already have a playback queue
maxQueueCount = 1

if m.top.isIntro
maxQueueCount = 2
end if

if m.global.queueManager.callFunc("getCount") > maxQueueCount then return
if myGlobal.queueManager.callFunc("getCount") > maxQueueCount then return

videoID = m.top.itemId

' If first item is an intro video, use the next item in the queue
if m.top.isIntro
currentVideo = m.global.queueManager.callFunc("getItemByIndex", 1)
currentVideo = myGlobal.queueManager.callFunc("getItemByIndex", 1)

if isValid(currentVideo) and isValid(currentVideo.id)
videoID = currentVideo.id
Expand All @@ -503,15 +509,15 @@ sub addNextEpisodesToQueue(showID)
end if

url = Substitute("Shows/{0}/Episodes", showID)
urlParams = { "UserId": m.global.session.user.id }
urlParams = { "UserId": myGlobal.session.user.id }
urlParams.Append({ "StartItemId": videoID })
urlParams.Append({ "Limit": 50 })
resp = APIRequest(url, urlParams)
data = getJson(resp)

if isValid(data) and data.Items.Count() > 1
for i = 1 to data.Items.Count() - 1
m.global.queueManager.callFunc("push", data.Items[i])
myGlobal.queueManager.callFunc("push", data.Items[i])
end for
end if
end sub
Expand Down Expand Up @@ -570,16 +576,17 @@ function sortSubtitles(id as string, MediaStreams)
end function

function FindPreferredAudioStream(streams as dynamic) as integer
preferredLanguage = m.global.session.user.Configuration.AudioLanguagePreference
playDefault = m.global.session.user.Configuration.PlayDefaultAudioTrack
myGlobal = m.global
preferredLanguage = myGlobal.session.user.Configuration.AudioLanguagePreference
playDefault = myGlobal.session.user.Configuration.PlayDefaultAudioTrack

if playDefault <> invalid and playDefault = true
return 1
end if

' Do we already have the MediaStreams or not?
if streams = invalid
url = Substitute("Users/{0}/Items/{1}", m.global.session.user.id, m.top.itemId)
url = Substitute("Users/{0}/Items/{1}", myGlobal.session.user.id, m.top.itemId)
resp = APIRequest(url)
jsonResponse = getJson(resp)

Expand Down
22 changes: 12 additions & 10 deletions components/ItemGrid/MovieLibraryView.bs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ sub setupNodes()
end sub

sub init()
myGlobal = m.global
setupNodes()

m.overhang.isVisible = false
Expand All @@ -39,7 +40,7 @@ sub init()
alphaMicText = m.alpha.findNode("alphaMicText")
alphaMicText.visible = false

m.showItemCount = m.global.session.user.settings["itemgrid.showItemCount"]
m.showItemCount = myGlobal.session.user.settings["itemgrid.showItemCount"]

m.swapAnimation.observeField("state", "swapDone")

Expand Down Expand Up @@ -86,7 +87,7 @@ sub init()
m.loadItemsTask.totalRecordCount = 0

'Get reset folder setting
m.resetGrid = m.global.session.user.settings["itemgrid.reset"]
m.resetGrid = myGlobal.session.user.settings["itemgrid.reset"]
end sub

sub OnScreenHidden()
Expand All @@ -112,6 +113,7 @@ end sub
sub loadInitialItems()
m.loadItemsTask.control = "stop"
startLoadingSpinner(false)
myGlobal = m.global

if m.top.parentItem.json.Type = "CollectionFolder"
m.top.HomeLibraryItem = m.top.parentItem.Id
Expand All @@ -123,15 +125,15 @@ sub loadInitialItems()
SetBackground("")
end if

m.sortField = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
m.filter = m.global.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
m.filterOptions = m.global.session.user.settings["display." + m.top.parentItem.Id + ".filterOptions"]
m.view = m.global.session.user.settings["display." + m.top.parentItem.Id + ".landing"]
m.sortAscending = m.global.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]
m.sortField = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortField"]
m.filter = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".filter"]
m.filterOptions = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".filterOptions"]
m.view = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".landing"]
m.sortAscending = myGlobal.session.user.settings["display." + m.top.parentItem.Id + ".sortAscending"]

' If user has not set a preferred view for this folder, check if they've set a default view
if not isValid(m.view)
m.view = m.global.session.user.settings["itemgrid.movieDefaultView"]
m.view = myGlobal.session.user.settings["itemgrid.movieDefaultView"]
end if

if not isValid(m.sortField) then m.sortField = "SortName"
Expand Down Expand Up @@ -200,7 +202,7 @@ sub loadInitialItems()
m.itemGrid.numRows = "3"
m.selectedMovieOverview.visible = false
m.infoGroup.visible = false
m.top.showItemTitles = m.global.session.user.settings["itemgrid.gridTitles"]
m.top.showItemTitles = myGlobal.session.user.settings["itemgrid.gridTitles"]
if LCase(m.top.showItemTitles) = "hidealways"
m.itemGrid.itemSize = "[230, 315]"
m.itemGrid.rowHeights = "[315]"
Expand All @@ -222,7 +224,7 @@ sub loadInitialItems()

m.getFiltersTask.observeField("filters", "FilterDataLoaded")
m.getFiltersTask.params = {
userid: m.global.session.user.id,
userid: myGlobal.session.user.id,
parentid: m.top.parentItem.Id,
includeitemtypes: "Movie"
}
Expand Down
Loading

0 comments on commit d06dafa

Please sign in to comment.