Skip to content

Commit

Permalink
Implement playCount database update. Fixes #160.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfollet committed Sep 14, 2016
1 parent db72beb commit 7a6bb6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/js/actions/AppActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const AppActions = {
// Audio Events
Player.getAudio().addEventListener('ended', AppActions.player.next);
Player.getAudio().addEventListener('error', AppActions.player.audioError);
Player.getAudio().addEventListener('timeupdate', () => {
if (Player.isThresholdReached()) {
LibraryActions.incrementPlayCount(Player.getAudio().src);
}
});
Player.getAudio().addEventListener('play', () => {
ipcRenderer.send('playerAction', 'play');
});
Expand Down
13 changes: 13 additions & 0 deletions src/js/actions/LibraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,18 @@ export default {
cover
});
});
},

/**
* Update the play count attribute.
*
* @param src
*/
incrementPlayCount(src) {
app.models.Track.update({ src }, { $inc: { playcount : 1 } }, (err) => {
if(err) {
console.warn(err);
}
});
}
};
12 changes: 12 additions & 0 deletions src/js/lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Player {
this.audio.playbackRate = options.playbackRate || 1;
this.audio.volume = options.volume || 1;
this.audio.muted = options.muted || false;

this.threshold = .75;
this.durationThresholdReached = false;
}

play() {
Expand Down Expand Up @@ -47,12 +50,21 @@ class Player {
}

setAudioSrc(src) {
// When we change song, need to update the thresholdReached indicator.
this.durationThresholdReached = false;
this.audio.src = src;
}

setAudioCurrentTime(currentTime) {
this.audio.currentTime = currentTime;
}

isThresholdReached() {
if(! this.durationThresholdReached && this.audio.currentTime >= this.audio.duration * this.threshold) {
this.durationThresholdReached = true;
return this.durationThresholdReached;
}
}
}

export default new Player({
Expand Down

0 comments on commit 7a6bb6d

Please sign in to comment.