From 58e49cc2f5a2a9de98b7c413d4f0312c7837c5c3 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Thu, 29 Oct 2015 14:37:00 +0000 Subject: [PATCH] Implement ng-controllers for cached track downloads --- .../scripts/app/controllers/album.coffee | 18 ++++++++++++++++-- .../scripts/app/controllers/track.coffee | 19 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/resources/assets/scripts/app/controllers/album.coffee b/resources/assets/scripts/app/controllers/album.coffee index ae91498c..1815ea96 100644 --- a/resources/assets/scripts/app/controllers/album.coffee +++ b/resources/assets/scripts/app/controllers/album.coffee @@ -21,8 +21,8 @@ window.pfm.preloaders['album'] = [ ] angular.module('ponyfm').controller "album", [ - '$scope', 'albums', '$state', 'playlists', 'auth', '$dialog' - ($scope, albums, $state, playlists, auth, $dialog) -> + '$scope', 'albums', '$state', 'playlists', 'auth', '$dialog', 'download-cached', '$window', '$timeout' + ($scope, albums, $state, playlists, auth, $dialog, cachedAlbum, $window, $timeout) -> album = null albums.fetch($state.params.id).done (albumResponse) -> @@ -40,4 +40,18 @@ angular.module('ponyfm').controller "album", [ if auth.data.isLogged playlists.refreshOwned().done (lists) -> $scope.playlists.push list for list in lists + + $scope.getCachedAlbum = (id, format) -> + $scope.isEncoding = true + + cachedAlbum.download('albums', id, format).then (response) -> + albumUrl = response + $scope.albumUrl = albumUrl + if albumUrl == 'error' + $scope.isEncoding = false + else if albumUrl == 'pending' + $timeout $scope.getCachedAlbum(id, format), 5000 + else + $scope.isEncoding = false + $window.open albumUrl, '_blank' ] diff --git a/resources/assets/scripts/app/controllers/track.coffee b/resources/assets/scripts/app/controllers/track.coffee index 2ebe9246..76cb49fb 100644 --- a/resources/assets/scripts/app/controllers/track.coffee +++ b/resources/assets/scripts/app/controllers/track.coffee @@ -21,8 +21,8 @@ window.pfm.preloaders['track'] = [ ] angular.module('ponyfm').controller "track", [ - '$scope', 'tracks', '$state', 'playlists', 'auth', 'favourites', '$dialog' - ($scope, tracks, $state, playlists, auth, favourites, $dialog) -> + '$scope', 'tracks', '$state', 'playlists', 'auth', 'favourites', '$dialog', 'download-cached', '$window', '$timeout' + ($scope, tracks, $state, playlists, auth, favourites, $dialog, cachedTrack, $window, $timeout) -> track = null tracks.fetch($state.params.id).done (trackResponse) -> @@ -72,4 +72,19 @@ angular.module('ponyfm').controller "track", [ playlists.addTrackToPlaylist(playlist.id, $scope.track.id).done (res) -> playlist.message = res.message + + $scope.getCachedTrack = (id, format) -> + $scope.isEncoding = true + + cachedTrack.download('tracks', id, format).then (response) -> + trackUrl = response + $scope.trackUrl = trackUrl + console.log(trackUrl); + if trackUrl == 'error' + $scope.isEncoding = false + else if trackUrl == 'pending' + $timeout $scope.getCachedTrack(id, format), 5000 + else + $scope.isEncoding = false + $window.open trackUrl, '_blank' ]