components_liveTv_LoadSheduleTask.bs

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

sub init()
  m.log = new log.Logger("LoadScheduleTask")
  m.top.functionName = "loadSchedule"
end sub

' Orchestrator Task: submits to ApiTask pool and waits off the render thread.
sub loadSchedule()
  params = {
    UserId: m.global.user.id,
    SortBy: "startDate",
    EnableImages: false,
    EnableTotalRecordCount: false,
    EnableUserData: false,
    channelIds: m.top.channelIds,
    MaxStartDate: m.top.endTime,
    MinEndDate: m.top.startTime
  }

  m.log.info("loadSchedule: starting fetch", "startTime", m.top.startTime, "endTime", m.top.endTime)
  timer = CreateObject("roTimespan")
  res = fetchRes(GetApi().BuildGetLiveTvScheduleRequest(params), "liveTvSchedule")
  elapsed = timer.TotalMilliseconds()
  m.log.info("loadSchedule: fetch completed", "elapsed_ms", elapsed, "ok", isValid(res) and res.ok)

  results = []

  if isValid(res) and res.ok and isValid(res.json)
    transformer = JellyfinDataTransformer()
    for each item in res.json.Items
      node = transformer.transformBaseItem(item)
      ' Set hdSmallIconUrl for TimeGrid recording indicator (ContentNode built-in read by Roku TimeGrid)
      if isValidAndNotEmpty(node.timerId)
        node.hdsmalliconurl = "pkg:/images/red.png"
      else
        node.hdsmalliconurl = ""
      end if
      results.push(node)
    end for
  end if

  m.log.info("loadSchedule: result", "programCount", results.Count())
  m.top.schedule = results
end sub