components_video_LoadChannelListForQueueTask.bs

import "pkg:/source/api/ApiClient.bs"
import "pkg:/source/api/apiPool.bs"
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/liveTv.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.log = new log.Logger("LoadChannelListForQueueTask")
  m.top.functionName = "loadChannelList"
end sub

sub loadChannelList()
  currentChannelId = m.top.currentChannelId
  if not isValidAndNotEmpty(currentChannelId) then return

  res = fetchRes(GetApi().BuildGetLiveTVChannelsRequest({
    SortBy: "Number",
    SortOrder: "Ascending",
    UserId: m.global.user.id,
    enableImages: false,
    addCurrentProgram: false,
    enableUserData: false
  }), "channelList")

  if not isValid(res) or not res.ok or not isValid(res.json) or not isValid(res.json.Items)
    m.log.warn("Failed to load channel list")
    return
  end if

  built = buildChannelQueueList(res.json.Items, currentChannelId)

  ' Set currentChannelIndex first to avoid a race with the channelList observer
  m.top.currentChannelIndex = built.currentIndex
  m.top.channelList = built.items
end sub