content: sub init()
      m.rowList = m.top.findNode("rowList")
      m.rowList.observeField("itemSelected", "onItemSelected")
      m.rowList.setFocus(true)
  end sub
  
  sub loadFolder(folderId as Integer)
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/files/list?parent_id=" + folderId.toStr())
      request.addHeader("Authorization", "Bearer " + m.top.accessToken)
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      if request.asyncGetToString()
          msg = wait(5000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              if responseCode = 200
                  response = ParseJson(responseString)
                  if response <> invalid and response.files <> invalid
                      createFileRows(response.files)
                  end if
              end if
          end if
      end if
  end sub
  
  sub createFileRows(files as Object)
      content = CreateObject("roSGNode", "ContentNode")
      
      ' Separate folders and files
      folders = []
      videoFiles = []
      otherFiles = []
      
      for each file in files
          if file.file_type = "FOLDER"
              folders.push(file)
          else if file.content_type <> invalid and left(file.content_type, 5) = "video"
              videoFiles.push(file)
          else
              otherFiles.push(file)
          end if
      end for
      
      ' Create rows
      if folders.count() > 0
          foldersRow = content.createChild("ContentNode")
          foldersRow.title = "Folders"
          for each folder in folders
              folderNode = foldersRow.createChild("ContentNode")
              folderNode.title = folder.name
              folderNode.hdPosterUrl = "pkg:/images/folder_icon.png"
              folderNode.fileData = folder
          end for
      end if
      
      if videoFiles.count() > 0
          videosRow = content.createChild("ContentNode")
          videosRow.title = "Videos"
          for each video in videoFiles
              videoNode = videosRow.createChild("ContentNode")
              videoNode.title = video.name
              if video.screenshot <> invalid
                  videoNode.hdPosterUrl = video.screenshot
              else
                  videoNode.hdPosterUrl = "pkg:/images/video_icon.png"
              end if
              videoNode.fileData = video
          end for
      end if
      
      if otherFiles.count() > 0
          filesRow = content.createChild("ContentNode")
          filesRow.title = "Other Files"
          for each file in otherFiles
              fileNode = filesRow.createChild("ContentNode")
              fileNode.title = file.name
              fileNode.hdPosterUrl = "pkg:/images/file_icon.png"
              fileNode.fileData = file
          end for
      end if
      
      m.rowList.content = content
  end sub
  
  sub onItemSelected(event as Object)
      selection = event.getData()
      rowContent = m.rowList.content.getChild(selection[0])
      itemContent = rowContent.getChild(selection[1])
      
      if itemContent.fileData <> invalid
          m.top.itemSelected = itemContent.fileData
      end if
  end sub
  