YouTube Video Player App
import QtQuick 2.0
import Felgo 4.0
Item {
id: dataModel
property alias dispatcher: logicConnection.target
readonly property alias userName: _.userName
readonly property alias channelId: _.channelId
readonly property alias mainPlaylistId: _.mainPlaylistId
readonly property alias uploadsPlaylistId: _.uploadsPlaylistId
readonly property alias channelInfo: _.channelInfo
readonly property alias channelPlaylists: _.channelPlaylists
readonly property alias playlistVideos: _.playlistVideos
readonly property alias mainPlaylist: _.mainPlaylist
readonly property alias mainPlaylistItems: _.mainPlaylistItems
readonly property alias mainPlaylistVideos: _.mainPlaylistVideos
readonly property alias requestsPending: api.requestsPending
Connections {
id: logicConnection
function onLoadYouTubeData() {
if(!_.userName)
return
_.playlistVideos = { }
api.getChannelForUsername(_.userName, function(channelData) {
if(!channelData)
return
_.channelInfo = channelData
_.channelId = channelData.id
_.uploadsPlaylistId = channelData.contentDetails.relatedPlaylists.uploads
api.getChannelPlaylists(_.channelId, function(playlists) {
_.channelPlaylists = playlists
api.getPlaylistById(_.uploadsPlaylistId, function(playlist) {
_.channelPlaylists.unshift(playlist)
_.channelPlaylistsChanged()
})
})
})
}
function onLoadMainPlaylistVideos() {
if(_.mainPlaylistId) {
api.getPlaylistItems(_.mainPlaylistId, function(items) {
_.mainPlaylistItems = items
var videoIds = _.mainPlaylistItems.map(function(item) { return item.contentDetails.videoId })
api.getVideos(videoIds, function(videos) {
_.mainPlaylistVideos = videos
})
})
}
}
function onFetchPlaylistVideos(playlist) {
api.getPlaylistItems(playlist.id, function(items) {
var videoIds = items.map(function(item) { return item.contentDetails.videoId })
api.getVideos(videoIds, function(videos) {
_.playlistVideos[playlist.id] = videos
_.playlistVideosChanged()
})
})
}
function onClearData() {
_.channelId = ""
_.mainPlaylistId = ""
_.channelInfo = undefined
_.channelPlaylists = []
_.mainPlaylist = undefined
_.mainPlaylistItems = []
_.mainPlaylistVideos = []
_.playlistVideos = { }
}
function onSetSpotlightPlaylist(playlist) {
if(playlist.id === "") {
_.mainPlaylistId = ""
_.mainPlaylist = undefined
_.mainPlaylistItems = undefined
_.mainPlaylistVideos = undefined
}
else {
_.mainPlaylistId = playlist.id
_.mainPlaylist = playlist
}
}
function onSetUserName(name) {
_.userName = name
}
}
Storage {
id: storage
Component.onCompleted: {
_.userName = storage.getValue("ytDemoUserName") || _.userName
_.mainPlaylistId = storage.getValue("mainPlaylistId") || ""
}
}
YouTubeAPI {
id: api
}
Item {
id: _
property string userName: "VPlayEngine"
property string channelId: ""
property string mainPlaylistId: ""
property string uploadsPlaylistId: ""
property var channelInfo: undefined
property var channelPlaylists: []
property var playlistVideos: null
property var mainPlaylist: undefined
property var mainPlaylistItems: undefined
property var mainPlaylistVideos: undefined
onChannelPlaylistsChanged: {
var newMainPlaylist = undefined
for(var i = 0; i < _.channelPlaylists.length; i++) {
if(_.channelPlaylists[i].id === _.mainPlaylistId)
newMainPlaylist = _.channelPlaylists[i]
}
_.mainPlaylist = newMainPlaylist
}
onUserNameChanged: storage.setValue("ytDemoUserName", _.userName)