source_LibraryViewFactory.bs

' LibraryViewFactory: Centralized factory for creating library views
'
' Creates BaseGridView instances with the appropriate presenter based on
' the library's collection type. Replaces direct component creation in
' ShowScenes.bs and Main.bs.
'
' Note: Presenter creation happens inside BaseGridView based on presenterType field

' Creates a library view with the appropriate presenter for the given library item
' @param {object} libraryItem - The library item node (CollectionFolder, Folder, etc.)
' @return {object} Configured BaseGridView component or invalid
function CreateLibraryView(libraryItem as object) as dynamic
  if not isValid(libraryItem) then return invalid

  view = CreateObject("roSGNode", "BaseGridView")
  if not isValid(view) then return invalid

  ' Set presenter type - this triggers presenter creation inside the component
  ' Must be set BEFORE parentItem since parentItem triggers loadInitialItems
  presenterType = DeterminePresenterType(libraryItem)
  view.presenterType = presenterType

  view.parentItem = libraryItem
  view.optionsAvailable = true

  return view
end function

' Determines the presenter type based on the library item's collection type
' @param {object} libraryItem - The library item node
' @return {string} Presenter type: "movie" | "music" | "tvshow" | "livetv" | "photo" | "generic"
function DeterminePresenterType(libraryItem as object) as string
  if not isValid(libraryItem) then return "generic"

  collectionType = LCase(libraryItem.collectionType)
  folderType = LCase(libraryItem.folderType)

  ' Movie library or movie genre
  if collectionType = "movies"
    return "movie"
  end if

  ' Music library or music genre
  if collectionType = "music"
    return "music"
  end if

  ' TV shows library
  if collectionType = "tvshows"
    return "tvshow"
  end if

  ' LiveTV library
  if collectionType = "livetv"
    return "livetv"
  end if

  ' Photo libraries - homevideos excluded since it's mixed-content (folders, videos, photos)
  if collectionType = "photos" or collectionType = "photoalbum"
    return "photo"
  end if

  ' Studio items with IsFolder=false arrive with type="Studio" directly (not via folderType).
  ' collectionType is set by LoadItemsTask2 based on the content being fetched.
  if LCase(libraryItem.type) = "studio"
    if collectionType = "movies" then return "movie"
    if collectionType = "tvshows" then return "tvshow"
    return "movie" ' default: studios are most commonly in movie libraries
  end if

  ' Genre folder - use collectionType set by LoadItemsTask2 first, fall back to item counts
  if folderType = "genre"
    if collectionType = "movies" then return "movie"
    if collectionType = "music" then return "music"
    if collectionType = "tvshows" then return "tvshow"
    ' Fallback for server responses that include item counts
    if libraryItem.movieCount > 0 then return "movie"
    if libraryItem.seriesCount > 0 then return "tvshow"
    return "generic"
  end if

  ' Studio folder (IsFolder=true) - use collectionType first, fall back to item counts
  if folderType = "studio"
    if collectionType = "movies" then return "movie"
    if collectionType = "tvshows" then return "tvshow"
    if libraryItem.movieCount > 0 then return "movie"
    if libraryItem.seriesCount > 0 then return "tvshow"
    return "movie" ' default studios to movie
  end if

  ' Music genre — either via folderType (when IsFolder=true) or raw type (when IsFolder is absent)
  if folderType = "musicgenre" or LCase(libraryItem.type) = "musicgenre"
    return "music"
  end if

  ' Default to generic for everything else (LiveTV, Collections, Boxsets, Folders, etc.)
  return "generic"
end function