Skip to content

Commit

Permalink
User favorite status synced across app (fixes #659) (#731)
Browse files Browse the repository at this point in the history
* Heart updates based on saved value when song changes

* Broadcasting favorites and unfavorites to sync them (stream & player)

* All songs now update themselves when broadcasted to
  • Loading branch information
dgattey authored and weblancaster committed Apr 11, 2016
1 parent eb71c04 commit bbd24b3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/public/js/common/favoriteSongDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ app.directive('favoriteSong', function(
notificationFactory.warn("Song removed from likes!");
$scope.favorite = false;
$scope.count -= 1;
$rootScope.$broadcast("track::unfavorited", songId);
}
}, function() {
notificationFactory.error("Something went wrong!");
Expand All @@ -38,6 +39,7 @@ app.directive('favoriteSong', function(
notificationFactory.success("Song added to likes!");
$scope.favorite = true;
$scope.count += 1;
$rootScope.$broadcast("track::favorited", songId);
}
}, function(status) {
notificationFactory.error("Something went wrong!");
Expand Down
18 changes: 18 additions & 0 deletions app/public/js/common/playerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ app.factory('playerService', function(
player.elPlayerProgress = document.getElementById('player-progress');
player.elPlayerDuration = document.getElementById('player-duration');
player.elPlayerTimeCurrent = document.getElementById('player-timecurrent');
player.elPlayerFavorite = angular.element( document.querySelector('.player_favorite') );
player.elThumb = document.getElementById('playerThumb');
player.elTitle = document.getElementById('playerTitle');
player.elUser = document.getElementById('playerUser');
Expand Down Expand Up @@ -165,6 +166,15 @@ app.factory('playerService', function(
};
}

// Make sure the favorite heart is active if user liked it
var fav = this.elPlayerFavorite;
utilsService.updateTracksLikes([trackObj], true) // use cache to start
.then(function() {
if ( trackObj.user_favorite ) {
fav.addClass('active');
}
});

$rootScope.isSongPlaying = true;
$rootScope.$broadcast('activateQueue');

Expand All @@ -173,6 +183,14 @@ app.factory('playerService', function(
document.querySelector('.player_favorite').classList.remove('active');
};

// Updates cache when liking or unliking a song, so future checks will be correct
$rootScope.$on('track::favorited', function(event, trackId) {
utilsService.addCachedFavorite(trackId);
});
$rootScope.$on('track::unfavorited', function(event, trackId) {
utilsService.removeCachedFavorite(trackId);
});

/**
* Responsible to play song
* @method playSong
Expand Down
1 change: 1 addition & 0 deletions app/public/js/common/queueCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ app.controller('QueueCtrl', function(
if ( typeof status == "object" ) {
notificationFactory.success("Song added to likes!");
utilsService.markTrackAsFavorite(songId);
$rootScope.$broadcast("track::favorited", songId);
}
}, function(status) {
notificationFactory.error("Something went wrong!");
Expand Down
12 changes: 12 additions & 0 deletions app/public/js/common/songDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ app.directive('song', function ($rootScope, $window, playerService) {
});
});

// Updating favorites when they get sent from other scope like the queue, stream, and player
$scope.$on('track::favorited', function(event, trackId) {
if ($scope.data.id == parseInt(trackId)) {
$scope.data.user_favorite = true;
}
});
$scope.$on('track::unfavorited', function(event, trackId) {
if ($scope.data.id == parseInt(trackId)) {
$scope.data.user_favorite = false;
}
});

}
}
});
31 changes: 30 additions & 1 deletion app/public/js/common/utilsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,42 @@ app.factory('utilsService', function(
}
collection.forEach(function (item) {
var track = item.track || item;
var id = track.id || track.songId;
// modify each track by reference
track.user_favorite = Utils.likesIds.indexOf(track.id) > -1;
track.user_favorite = Utils.likesIds.indexOf(id) > -1;
});
return collection;
});
};

/**
* When manipulating likes after a page is loaded, it's necessary to
* manipulate the likesIds cache when you modify user_favorite like
* above. Used to sync the likes between player and everything else
* @param {number or string} id - the song id to add to likes
*/
Utils.addCachedFavorite = function(id) {
id = parseInt(id);
var index = Utils.likesIds.indexOf(id);
if (index == -1) {
Utils.likesIds.push(id);
}
}

/**
* When manipulating likes after a page is loaded, it's necessary to
* manipulate the likesIds cache when you modify user_favorite like
* above. Used to sync the likes between player and everything else
* @param {number or string} id - the song id to remove from likes
*/
Utils.removeCachedFavorite = function(id) {
id = parseInt(id);
var index = Utils.likesIds.indexOf(id);
if (index > -1) {
Utils.likesIds.splice(index, 1);
}
}

/**
* Fetch ids of reposted tracks and apply them to existing collection
* @param {array} collection - stream collection or tracks array
Expand Down
18 changes: 18 additions & 0 deletions app/public/js/player/playerCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ app.controller('PlayerCtrl', function (
if ( typeof status == "object" ) {
notificationFactory.warn("Song removed from likes!");
$event.currentTarget.classList.remove('active');
$rootScope.$broadcast("track::unfavorited", track.songId);
}
}, function() {
notificationFactory.error("Something went wrong!");
Expand All @@ -122,13 +123,30 @@ app.controller('PlayerCtrl', function (
if ( typeof status == "object" ) {
notificationFactory.success("Song added to likes!");
$event.currentTarget.classList.add('active');
$rootScope.$broadcast("track::favorited", track.songId);
}
}, function(status) {
notificationFactory.error("Something went wrong!");
});
}
};

// Listen for updates from other scopes about favorites and unfavorites
$scope.$on('track::favorited', function(event, trackId) {
var track = queueService.getTrack();
if ( track && trackId == track.songId ) {
var elFavorite = document.querySelector('.player_favorite');
elFavorite.classList.add('active');
}
});
$scope.$on('track::unfavorited', function(event, trackId) {
var track = queueService.getTrack();
if ( track && trackId == track.songId ) {
var elFavorite = document.querySelector('.player_favorite');
elFavorite.classList.remove('active');
}
});


/*
* Add native media shortcuts
Expand Down

0 comments on commit bbd24b3

Please sign in to comment.