components_music_ArtistView.bs

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

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

  m.remoteButtonsActive = true

  m.albumHeader = m.top.findNode("albumHeader")
  m.albumHeader.text = tr("Albums")

  m.appearsOnHeader = m.top.findNode("appearsOnHeader")
  m.appearsOnHeader.text = tr("AppearsOn")

  m.appearsOn = m.top.findNode("appearsOn")
  m.appearsOn.observeField("escape", "onAppearsOnEscape")
  m.appearsOn.observeField("MusicArtistAlbumData", "onAppearsOnData")

  m.albums = m.top.findNode("albums")
  m.albums.observeField("escape", "onAlbumsEscape")
  m.albums.observeField("MusicArtistAlbumData", "onAlbumsData")

  m.sectionScroller = m.top.findNode("sectionScroller")
  m.sectionScroller.observeField("displayedIndex", "onSectionScrollerChange")
  m.overhang = m.top.getScene().findNode("overhang")

  ' Load background image
  m.LoadBackdropImageTask = CreateObject("roSGNode", "LoadItemsTask")
  m.LoadBackdropImageTask.itemsToLoad = "backdropImage"

  m.backDrop = m.top.findNode("backdrop")
  m.artistImage = m.top.findNode("artistImage")
  m.dscr = m.top.findNode("overview")
  m.dscr.ellipsisText = tr("... (Press * to read more)")
  m.dscr.observeField("isTextEllipsized", "onEllipsisChanged")
  createDialogPallete()
end sub

sub onAlbumsData()
  ' We have no album data
  if m.albums.MusicArtistAlbumData.TotalRecordCount = 0
    m.sectionScroller.removeChild(m.top.findNode("albumsSlide"))
    m.top.findNode("appearsOnSlide").callFunc("scrollUpToOnDeck")
  end if
end sub

sub onAppearsOnData()
  ' We have no appears on data
  if m.appearsOn.MusicArtistAlbumData.TotalRecordCount = 0
    m.sectionScroller.removeChild(m.top.findNode("appearsOnSlide"))
  end if
end sub

sub onSectionScrollerChange()
  m.overhang.isVisible = (m.sectionScroller.displayedIndex = 0)
end sub

sub OnScreenShown()
  if m.sectionScroller.displayedIndex = 0
    ' User is on the top section with artist info and buttons
    m.buttonGrp.callFunc("focus")
    m.top.lastFocus = m.buttonGrp
  else
    ' User is scrolled down into albums/appearsOn section
    m.overhang.opacity = "0"
    m.overhang.isVisible = false
    m.overhang.opacity = "1"

    ' Restore focus to previously focused element (set by SceneManager before push)
    if isValid(m.top.lastFocus)
      m.top.lastFocus.setFocus(true)
    end if
  end if
  stopLoadingSpinner()
end sub

sub OnScreenHidden()
  if not m.overhang.isVisible
    m.overhang.disableMoveAnimation = true
    m.overhang.isVisible = true
    m.overhang.disableMoveAnimation = false
    m.overhang.opacity = "1"
  end if
end sub

sub onAlbumsEscape()
  if m.albums.escape = "up"
    m.sectionScroller.displayedIndex = m.sectionScroller.displayedIndex - 1
  else if m.albums.escape = "down"
    newIndex = m.sectionScroller.displayedIndex + 1
    if newIndex < m.sectionScroller.getChildCount()
      m.sectionScroller.displayedIndex = newIndex
    end if
  end if
end sub

sub onAppearsOnEscape()
  if m.appearsOn.escape = "up"
    m.sectionScroller.displayedIndex = m.sectionScroller.displayedIndex - 1
  end if
end sub

' Setup playback buttons, default to Play button selected
sub setupButtons()
  m.buttonGrp = m.top.findNode("buttons")

  ' Set default focus to Play button (index 0)
  m.buttonGrp.buttonFocused = 0

  ' Observe button selection events
  m.buttonGrp.observeField("buttonSelected", "onButtonSelected")
end sub

' Handles button selection and notifies Main.bs via observed fields
sub onButtonSelected()
  selectedIndex = m.buttonGrp.buttonSelected

  if selectedIndex = 0
    ' Play button selected - notify Main.bs to play artist mix
    m.top.playArtistSelected = true
  else if selectedIndex = 1
    ' Instant Mix button selected - notify Main.bs to create instant mix
    m.top.instantMixSelected = true
  end if
end sub

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

' Event fired when page data is loaded
sub pageContentChanged()
  item = m.top.pageContent

  ' Use metadata to load backdrop image
  m.LoadBackdropImageTask.itemId = item.json.id
  m.LoadBackdropImageTask.observeField("content", "onBackdropImageLoaded")
  m.LoadBackdropImageTask.control = "RUN"

  ' Populate scene data
  setScreenTitle(item.json)
  setPosterImage(item.posterURL)
end sub

sub setScreenTitle(json)
  if isValid(json)
    m.top.overhangTitle = json.name
  end if
end sub

sub setPosterImage(posterURL)
  if not isValid(posterURL) or posterURL = ""
    posterURL = "pkg:/images/icons/missingArtist.png"
  end if

  m.artistImage.uri = posterURL
end sub

sub onBackdropImageLoaded()
  data = m.LoadBackdropImageTask.content[0]
  m.LoadBackdropImageTask.unobserveField("content")
  if isValid(data) and data <> ""
    setBackdropImage(data)
  end if
end sub

' Add backdrop image to screen
sub setBackdropImage(data)
  if isValid(data)
    if m.backDrop.uri <> data
      m.backDrop.uri = data
    end if
  end if
end sub

' Event fired when page data is loaded
sub artistOverviewChanged()
  overviewContent = m.top.artistOverview

  if isValid(overviewContent)
    setFieldTextValue("overview", overviewContent)
  end if
end sub

sub onEllipsisChanged()
  if m.dscr.isTextEllipsized
    dscrShowFocus()
  end if
end sub

sub dscrShowFocus()
  if m.dscr.isTextEllipsized
    m.dscr.setFocus(true)
    m.dscr.opacity = 1.0
  end if
end sub

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

function onKeyEvent(key as string, press as boolean) as boolean

  ' Handle down arrow when button group has focus - move to albums section
  if m.buttonGrp.isInFocusChain() and key = "down"
    m.top.lastFocus = m.buttonGrp
    m.sectionScroller.displayedIndex = m.sectionScroller.displayedIndex + 1
    return true
  end if

  if not press then return false

  ' Handle options key for full description dialog
  if key = "options"
    if m.dscr.isTextEllipsized
      createFullDscrDlg()
      return true
    end if
  end if

  ' Handle play key for quick play of focused album
  if key = "play"
    print "play button pressed from ArtistView"
    itemToPlay = invalid

    if isValid(m.albums) and m.albums.isInFocusChain()
      itemToPlay = m.albums.MusicArtistAlbumData.items[m.albums.itemFocused]
    else if isValid(m.appearsOn) and m.appearsOn.isInFocusChain()
      itemToPlay = m.appearsOn.MusicArtistAlbumData.items[m.appearsOn.itemFocused]
    end if

    if isValid(itemToPlay)
      m.top.quickPlayNode = itemToPlay
      return true
    end if
  end if

  return false
end function