components_music_AlbumView.bs

import "pkg:/source/utils/misc.bs"

sub init()
  m.top.optionsAvailable = false
  setupMainNode()

  m.buttons = m.top.findNode("buttons")
  m.albumCover = m.top.findNode("albumCover")
  m.songList = m.top.findNode("songList")
  m.infoGroup = m.top.FindNode("infoGroup")
  m.songListRect = m.top.FindNode("songListRect")

  m.songList.observeField("doneLoading", "onDoneLoading")

  m.dscr = m.top.findNode("overview")
  m.dscr.ellipsisText = tr("... (Press * to read more)")
  createDialogPallete()

  ' Set initial focus to song list
  m.top.lastFocus = m.songList
end sub

sub setupMainNode()
  main = m.top.findNode("toplevel")
  main.translation = [96, 175]
end sub

sub OnScreenShown()
  ' Set backdrop from album data (only if pageContent is already loaded)
  ' This handles the case when returning to an already-loaded screen
  if isValid(m.top.pageContent)
    if isValid(m.top.pageContent.backdropUrl)
      m.global.sceneManager.callFunc("setBackgroundImage", m.top.pageContent.backdropUrl)
    end if
  end if
  ' If pageContent not loaded, backdrop will be set in pageContentChanged()

  ' Restore focus to previously focused element
  if isValid(m.top.lastFocus)
    m.top.lastFocus.setFocus(true)
  else
    m.top.setFocus(true)
  end if
end sub

' Set values for displayed values on screen
sub pageContentChanged()
  item = m.top.pageContent

  ' Set backdrop from album's artist backdrop
  if isValid(item) and isValid(item.backdropUrl)
    m.global.sceneManager.callFunc("setBackgroundImage", item.backdropUrl)
  end if

  setPosterImage(item.posterURL)
  setScreenTitle(item.json)
  setOnScreenTextValues(item.json)
end sub

' Set poster image on screen
sub setPosterImage(posterURL)
  if isValid(posterURL)
    m.albumCover.uri = posterURL
  end if
end sub

' Set screen's title text
sub setScreenTitle(json)
  newTitle = ""
  if isValid(json)
    if isValid(json.AlbumArtist)
      newTitle = json.AlbumArtist
    end if
    if isValid(json.AlbumArtist) and isValid(json.name)
      newTitle = newTitle + " / "
    end if
    if isValid(json.name)
      newTitle = newTitle + json.name
    end if
  end if
  m.top.overhangTitle = newTitle
end sub

' Adjust scene by removing overview node and showing more songs
sub adjustScreenForNoOverview()
  m.infoGroup.removeChild(m.dscr)
  m.songListRect.height = 800
  m.songList.numRows = 12
end sub

' Populate on screen text variables
sub setOnScreenTextValues(json)
  if isValid(json)
    if isValid(json.overview) and json.overview <> ""
      ' We have overview text
      setFieldTextValue("overview", json.overview)
    else
      ' We don't have overview text
      adjustScreenForNoOverview()
    end if

    setFieldTextValue("numberofsongs", stri(json.ChildCount) + " Tracks")

    if type(json.ProductionYear) = "roInt"
      setFieldTextValue("released", "Released " + stri(json.ProductionYear))
    end if

    if json.genres.count() > 0
      setFieldTextValue("genres", json.genres.join(", "))
    end if

    if type(json.RunTimeTicks) = "LongInteger"
      setFieldTextValue("runtime", stri(getMinutes(json.RunTimeTicks)) + " mins")
    end if
  end if
end sub

function onKeyEvent(key as string, press as boolean) as boolean
  ' print "[AlbumView] onKeyEvent: key=", key, ", press=", press
  if not press then return false

  if key = "options"
    if m.dscr.isTextEllipsized
      createFullDscrDlg()
      return true
    end if
    return false
  end if

  if key = "right" and m.buttons.isInFocusChain()
    m.songList.setFocus(true)
    return true
  else if key = "left" and m.songList.hasFocus()
    m.buttons.callFunc("focus")
    return true
  end if

  return false
end function

sub createFullDscrDlg()
  dlg = CreateObject("roSGNode", "OverviewDialog")
  dlg.Title = tr("Press 'Back' to Close")
  dlg.width = 1290
  dlg.palette = m.dlgPalette
  dlg.overview = m.dscr.text
  m.fullDscrDlg = dlg
  m.top.getScene().dialog = dlg
  border = createObject("roSGNode", "Poster")
  border.uri = "pkg:/images/hd_focul_9.png"
  border.blendColor = "#c9c9c9ff"
  border.width = dlg.width + 6
  border.height = dlg.height + 6
  border.translation = [dlg.translation[0] - 3, dlg.translation[1] - 3]
  border.visible = true
end sub

sub createDialogPallete()
  m.dlgPalette = createObject("roSGNode", "RSGPalette")
  m.dlgPalette.colors = {
    DialogBackgroundColor: "0x262828FF",
    DialogItemColor: "0x00EF00FF",
    DialogTextColor: "0xb0b0b0FF",
    DialogFocusColor: "0xcececeFF",
    DialogFocusItemColor: "0x202020FF",
    DialogSecondaryTextColor: "0xf8f8f8ff",
    DialogSecondaryItemColor: "0xcc7ecc4D",
    DialogInputFieldColor: "0x80FF8080",
    DialogKeyboardColor: "0x80FF804D",
    DialogFootprintColor: "0x80FF804D"
  }
end sub

sub onDoneLoading()
  m.songList.unobservefield("doneLoading")
  stopLoadingSpinner()
end sub