Rework how we keep track of row item sizes

This commit is contained in:
1hitsong 2023-11-22 12:14:35 -05:00
parent 0a7d74d291
commit 35dfa54a29

View File

@ -12,9 +12,7 @@ sub init()
m.top.rowLabelOffset = [0, 20]
m.top.showRowCounter = [true]
m.homeSectionIndexes = {
count: 0
}
m.homeSections = {}
updateSize()
@ -67,13 +65,12 @@ sub onLibrariesLoaded()
m.LoadLibrariesTask.content = []
content = CreateObject("roSGNode", "ContentNode")
sizeArray = []
loadedSections = 0
' Add sections in order based on user settings
for i = 0 to 6
sectionName = LCase(m.global.session.user.settings["homesection" + i.toStr()])
sectionLoaded = addHomeSection(content, sizeArray, sectionName)
sectionLoaded = addHomeSection(content, sectionName)
' Count how many sections with data are loaded
if sectionLoaded then loadedSections++
@ -88,9 +85,10 @@ sub onLibrariesLoaded()
end for
' Favorites isn't an option on Web settings, so we must manually add it for now
addHomeSection(content, sizeArray, "favorites")
addHomeSection(content, "favorites")
setRowItemSizes()
m.top.rowItemSize = sizeArray
m.top.content = content
end sub
@ -98,57 +96,71 @@ end sub
sub removeHomeSection(sectionType as string)
sectionName = LCase(sectionType)
removedSectionIndex = m.homeSectionIndexes[sectionName]
removedSection = m.homeSections[sectionName]
if not isValid(removedSectionIndex) then return
if not isValid(removedSection) then return
if not isValid(removedSection.index) then return
for each section in m.homeSectionIndexes
if m.homeSectionIndexes[section] > removedSectionIndex
m.homeSectionIndexes[section]--
for each section in m.homeSections
if m.homeSections[section].index > removedSection.index
m.homeSections[section].index--
end if
end for
m.homeSectionIndexes.Delete(sectionName)
m.homeSections.Delete(sectionName)
m.top.content.removeChildIndex(removedSection.index)
m.top.content.removeChildIndex(removedSectionIndex)
setRowItemSizes()
end sub
' setRowItemSizes: Loops through all home sections and sets the correct item sizes per row
'
sub setRowItemSizes()
newSizeArray = CreateObject("roArray", m.homeSections.count(), false)
for each section in m.homeSections
newSizeArray[m.homeSections[section].index] = m.homeSections[section].imagesize
end for
m.top.rowItemSize = newSizeArray
end sub
' Adds a new home section to the home rows.
' Returns a boolean indicating whether the section was handled.
function addHomeSection(content as dynamic, sizeArray as dynamic, sectionName as string) as boolean
function addHomeSection(content as dynamic, sectionName as string) as boolean
' Poster size library items
if sectionName = "livetv"
createLiveTVRow(content, sizeArray)
createLiveTVRow(content)
return true
end if
' Poster size library items
if sectionName = "smalllibrarytiles"
createLibraryRow(content, sizeArray)
createLibraryRow(content)
return true
end if
' Continue Watching items
if sectionName = "resume"
createContinueWatchingRow(content, sizeArray)
createContinueWatchingRow(content)
return true
end if
' Next Up items
if sectionName = "nextup"
createNextUpRow(content, sizeArray)
createNextUpRow(content)
return true
end if
' Latest items in each library
if sectionName = "latestmedia"
createLatestInRows(content, sizeArray)
createLatestInRows(content)
return true
end if
' Favorite Items
if sectionName = "favorites"
createFavoritesRow(content, sizeArray)
createFavoritesRow(content)
return true
end if
@ -156,17 +168,17 @@ function addHomeSection(content as dynamic, sizeArray as dynamic, sectionName as
end function
' Create a row displaying the user's libraries
sub createLibraryRow(content as dynamic, sizeArray as dynamic)
sub createLibraryRow(content as dynamic)
' Ensure we have data
if not isValidAndNotEmpty(m.libraryData) then return
mediaRow = content.CreateChild("HomeRow")
mediaRow.title = tr("My Media")
m.homeSectionIndexes.AddReplace("library", m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
sizeArray.push([464, 331])
m.homeSections.AddReplace("library", {
imageSize: [464, 331],
index: m.homeSections.count()
})
filteredMedia = filterNodeArray(m.libraryData, "id", m.global.session.user.configuration.MyMediaExcludes)
for each item in filteredMedia
@ -175,7 +187,7 @@ sub createLibraryRow(content as dynamic, sizeArray as dynamic)
end sub
' Create a row displaying latest items in each of the user's libraries
sub createLatestInRows(content as dynamic, sizeArray as dynamic)
sub createLatestInRows(content as dynamic)
' Ensure we have data
if not isValidAndNotEmpty(m.libraryData) then return
@ -186,9 +198,18 @@ sub createLatestInRows(content as dynamic, sizeArray as dynamic)
latestInRow = content.CreateChild("HomeRow")
latestInRow.title = tr("Latest in") + " " + lib.name + " >"
m.homeSectionIndexes.AddReplace("latestin" + LCase(lib.name).Replace(" ", ""), m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
sizeArray.Push([464, 331])
imagesize = [464, 331]
if LCase(lib.collectionType) = "movies"
imagesize = [188, 331]
else if LCase(lib.collectionType) = "music"
imagesize = [261, 331]
end if
m.homeSections.AddReplace("latestin" + LCase(lib.name).Replace(" ", ""), {
imageSize: imagesize,
index: m.homeSections.count()
})
loadLatest = createObject("roSGNode", "LoadItemsTask")
loadLatest.itemsToLoad = "latest"
@ -205,24 +226,26 @@ sub createLatestInRows(content as dynamic, sizeArray as dynamic)
end sub
' Create a row displaying the live tv now on section
sub createLiveTVRow(content as dynamic, sizeArray as dynamic)
sub createLiveTVRow(content as dynamic)
contentRow = content.CreateChild("HomeRow")
contentRow.title = tr("On Now")
m.homeSectionIndexes.AddReplace("livetv", m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
sizeArray.push([464, 331])
m.homeSections.AddReplace("livetv", {
imageSize: [464, 331],
index: m.homeSections.count()
})
m.LoadOnNowTask.observeField("content", "updateOnNowItems")
m.LoadOnNowTask.control = "RUN"
end sub
' Create a row displaying items the user can continue watching
sub createContinueWatchingRow(content as dynamic, sizeArray as dynamic)
sub createContinueWatchingRow(content as dynamic)
continueWatchingRow = content.CreateChild("HomeRow")
continueWatchingRow.title = tr("Continue Watching")
m.homeSectionIndexes.AddReplace("resume", m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
sizeArray.push([464, 331])
m.homeSections.AddReplace("resume", {
imageSize: [464, 331],
index: m.homeSections.count()
})
' Load the Continue Watching Data
m.LoadContinueWatchingTask.observeField("content", "updateContinueWatchingItems")
@ -230,12 +253,13 @@ sub createContinueWatchingRow(content as dynamic, sizeArray as dynamic)
end sub
' Create a row displaying next episodes up to watch
sub createNextUpRow(content as dynamic, sizeArray as dynamic)
sub createNextUpRow(content as dynamic)
nextUpRow = content.CreateChild("HomeRow")
nextUpRow.title = tr("Next Up >")
m.homeSectionIndexes.AddReplace("nextup", m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
sizeArray.push([464, 331])
m.homeSections.AddReplace("nextup", {
imageSize: [464, 331],
index: m.homeSections.count()
})
' Load the Next Up Data
m.LoadNextUpTask.observeField("content", "updateNextUpItems")
@ -243,13 +267,14 @@ sub createNextUpRow(content as dynamic, sizeArray as dynamic)
end sub
' Create a row displaying items from the user's favorites list
sub createFavoritesRow(content as dynamic, sizeArray as dynamic)
sub createFavoritesRow(content as dynamic)
favoritesRow = content.CreateChild("HomeRow")
favoritesRow.title = tr("Favorites")
sizeArray.Push([464, 331])
m.homeSectionIndexes.AddReplace("favorites", m.homeSectionIndexes.count)
m.homeSectionIndexes.count++
m.homeSections.AddReplace("favorites", {
imageSize: [464, 331],
index: m.homeSections.count()
})
' Load the Favorites Data
m.LoadFavoritesTask.observeField("content", "updateFavoritesItems")
@ -259,25 +284,25 @@ end sub
' Update home row data
sub updateHomeRows()
' If resume section exists, reload row's data
if m.homeSectionIndexes.doesExist("resume")
if m.homeSections.doesExist("resume")
m.LoadContinueWatchingTask.observeField("content", "updateContinueWatchingItems")
m.LoadContinueWatchingTask.control = "RUN"
end if
' If next up section exists, reload row's data
if m.homeSectionIndexes.doesExist("nextup")
if m.homeSections.doesExist("nextup")
m.LoadNextUpTask.observeField("content", "updateNextUpItems")
m.LoadNextUpTask.control = "RUN"
end if
' If favorites section exists, reload row's data
if m.homeSectionIndexes.doesExist("favorites")
if m.homeSections.doesExist("favorites")
m.LoadFavoritesTask.observeField("content", "updateFavoritesItems")
m.LoadFavoritesTask.control = "RUN"
end if
' If live tv's on now section exists, reload row's data
if m.homeSectionIndexes.doesExist("livetv")
if m.homeSections.doesExist("livetv")
m.LoadOnNowTask.observeField("content", "updateOnNowItems")
m.LoadOnNowTask.control = "RUN"
end if
@ -285,7 +310,7 @@ sub updateHomeRows()
' If latest in library section exists, reload row's data
hasLatestHomeSection = false
for each section in m.homeSectionIndexes
for each section in m.homeSections
if LCase(Left(section, 6)) = "latest"
hasLatestHomeSection = true
exit for
@ -295,6 +320,7 @@ sub updateHomeRows()
if hasLatestHomeSection
updateLatestInRows()
end if
end sub
sub updateFavoritesItems()
@ -304,7 +330,7 @@ sub updateFavoritesItems()
if itemData = invalid then return
rowIndex = m.homeSectionIndexes.favorites
rowIndex = m.homeSections.favorites.index
if itemData.count() < 1
removeHomeSection("favorites")
@ -359,7 +385,7 @@ sub updateContinueWatchingItems()
end for
' replace the old row
m.top.content.replaceChild(row, m.homeSectionIndexes.resume)
m.top.content.replaceChild(row, m.homeSections.resume.index)
end sub
sub updateNextUpItems()
@ -383,7 +409,7 @@ sub updateNextUpItems()
end for
' replace the old row
m.top.content.replaceChild(row, m.homeSectionIndexes.nextup)
m.top.content.replaceChild(row, m.homeSections.nextup.index)
end if
end sub
@ -449,22 +475,23 @@ sub updateLatestItems(msg)
row.appendChild(item)
end for
rowIndex = m.homeSectionIndexes[sectionName]
if isValid(m.homeSections[sectionName])
rowIndex = m.homeSections[sectionName].index
' Replace the old row
if isValid(rowIndex)
updateSizeArray(itemSize, rowIndex, "replace")
m.top.content.replaceChild(row, rowIndex)
return
' Replace the old row
if isValid(rowIndex)
m.top.content.replaceChild(row, rowIndex)
return
end if
end if
' Determine highest index of a Lastest In section so we can append the new section after it
highestLatestHomeSectionIndex = 0
for each section in m.homeSectionIndexes
for each section in m.homeSections
if LCase(Left(section, 6)) = "latest"
if m.homeSectionIndexes[section] > highestLatestHomeSectionIndex
highestLatestHomeSectionIndex = m.homeSectionIndexes[section]
if m.homeSections[section].index > highestLatestHomeSectionIndex
highestLatestHomeSectionIndex = m.homeSections[section].index
end if
end if
end for
@ -473,16 +500,21 @@ sub updateLatestItems(msg)
rowIndex = highestLatestHomeSectionIndex + 1
' Advance all the indexes greater than or equal than our new row
for each section in m.homeSectionIndexes
if m.homeSectionIndexes[section] >= rowIndex
m.homeSectionIndexes[section]++
for each section in m.homeSections
if m.homeSections[section].index >= rowIndex
m.homeSections[section].index++
end if
end for
m.homeSectionIndexes.AddReplace(sectionName, rowIndex)
m.homeSections.AddReplace(sectionName, {
imageSize: itemSize,
index: rowIndex
})
m.top.content.insertChild(row, rowIndex)
updateSizeArray(itemSize, rowIndex)
' We've inserted a new row, we must set the row sizes again to ensure they're correct
setRowItemSizes()
return
end if
@ -502,7 +534,7 @@ sub updateOnNowItems()
' remake row using the new data
row = CreateObject("roSGNode", "HomeRow")
row.title = tr("On Now")
itemSize = [464, 331]
'itemSize = [464, 331]
row.imageWidth = 464
for each item in itemData
row.usePoster = false
@ -510,7 +542,7 @@ sub updateOnNowItems()
item.thumbnailURL = item.json.imageURL
row.usePoster = true
row.imageWidth = 180
itemSize = [188, 331]
'itemSize = [188, 331]
end if
item.usePoster = row.usePoster
item.imageWidth = row.imageWidth
@ -518,37 +550,11 @@ sub updateOnNowItems()
end for
' replace the old row
updateSizeArray(itemSize, m.homeSectionIndexes.livetv, "replace")
m.top.content.replaceChild(row, m.homeSectionIndexes.livetv)
m.top.content.replaceChild(row, m.homeSections.livetv.index)
end if
end sub
sub updateSizeArray(rowItemSize, rowIndex = invalid, action = "insert")
sizeArray = m.top.rowItemSize
' append by default
if rowIndex = invalid
rowIndex = sizeArray.count()
end if
newSizeArray = []
for i = 0 to sizeArray.count()
if rowIndex = i
if action = "replace"
newSizeArray.Push(rowItemSize)
else if action = "insert"
newSizeArray.Push(rowItemSize)
if isValid(sizeArray[i])
newSizeArray.Push(sizeArray[i])
end if
end if
else if isValid(sizeArray[i])
newSizeArray.Push(sizeArray[i])
end if
end for
m.top.rowItemSize = newSizeArray
end sub
sub itemSelected()
m.top.selectedItem = m.top.content.getChild(m.top.rowItemSelected[0]).getChild(m.top.rowItemSelected[1])