Commit df0e044a authored by Kevin Whitaker's avatar Kevin Whitaker

Convert timer to javascript to cut down on requests.

parent 4f3e3d74
......@@ -126,3 +126,19 @@ std::string WebInterface::getNotificationJs(std::string notificationText)
std::string escapedText = std::regex_replace(notificationText,std::regex("\""),"'");
return std::string("function notifyMe(a,b){var c=\"")+escapedText+std::string("\";if('Notification'in window)if('granted'===Notification.permission){new Notification(c)}else'denied'!==Notification.permission&&Notification.requestPermission(function(a){if('granted'===a){new Notification(c)}});else;}");
}
std::string WebInterface::getProgressNextTrackJs(std::string pgObj, std::string incValue, std::string startVal)
{
return pgObj+".incrementProgress = function(){if(1!="+pgObj+".isPaused&&'100%'!="+pgObj+".firstChild.style.width){if(parseFloat("+pgObj+".firstChild.style.width)+"+incValue+">=100)return void("+pgObj+".firstChild.style.width='100%');"+pgObj+".firstChild.style.width=parseFloat("+pgObj+".firstChild.style.width)+"+incValue+"+'%',setTimeout("+pgObj+".incrementProgress,1e3)}};"+pgObj+".firstChild.style.width = '"+startVal+"%';";
}
std::string WebInterface::getProgressPauseJs(std::string pgObj)
{
return pgObj+".isPaused = true;";
}
std::string WebInterface::getProgressResumeJs(std::string pgObj)
{
return pgObj+".isPaused = false;"+pgObj+".incrementProgress();";
}
......@@ -41,6 +41,9 @@ public:
void voteNextPollClosedFromServer(AudioTrack winner);
void createUser(Wt::Dbo::Session* session, std::string username, std::string rawPassword, bool isAdmin);
User currentUser;
std::string getProgressNextTrackJs(std::string pgObj, std::string incValue, std::string startVal);
std::string getProgressResumeJs(std::string pgObj);
std::string getProgressPauseJs(std::string pgObj);
private:
struct internal;
internal* priv_int = 0;
......
......@@ -38,9 +38,6 @@ PlayerInterface::PlayerInterface(WebInterface* app)
currentTrackProgress = new Wt::WProgressBar();
currentTrackProgress->setFormat("");
trackProgress = new Wt::WTimer();
trackProgress->setInterval(1000);
trackProgress->timeout().connect(this,&PlayerInterface::updateProgressFromTimer);
currentTrackDetails = new TrackDetails();
currentTrackDetails->setMinimumSize(Wt::WLength(100,Wt::WLength::Pixel),Wt::WLength(100,Wt::WLength::Pixel));
currentTrackDetails->changeBackgroundColor(Wt::WColor("#6FFF6F"));
......@@ -268,26 +265,13 @@ void PlayerInterface::playpauseUpdated()
{
skipRequest->setEnabled(false);
playpause->setText(Wt::WString::fromUTF8(""));
currentTrackProgress->doJavaScript(app->getProgressPauseJs(currentTrackProgress->jsRef()));
}
else
{
if(!GroovePlayerMgr::getInstance()->voteEndedButNotNextTrackYet) skipRequest->setEnabled(true);
playpause->setText(Wt::WString::fromUTF8(""));
}
}
void PlayerInterface::updateProgressFromTimer()
{
if(currentTrackProgress->maximum() >= currentTrackProgress->value()+1)
{
double secondsIn;
groove_player_position(GroovePlayerMgr::getInstance()->currentPlayer,nullptr, &secondsIn);
currentTrackProgress->setValue(secondsIn);
}
else if(currentTrackProgress->maximum() < currentTrackProgress->value()+1)
{
currentTrackProgress->setValue(currentTrackProgress->maximum());
trackProgress->stop();
currentTrackProgress->doJavaScript(app->getProgressResumeJs(currentTrackProgress->jsRef()));
}
}
......@@ -296,13 +280,10 @@ void PlayerInterface::updateDetailsFromServer(AudioTrack track)
//Find current song playing and set progress state.
double secondsIn;
groove_player_position(GroovePlayerMgr::getInstance()->currentPlayer,nullptr, &secondsIn);
currentTrackProgress->setMaximum(track.trackLengthSeconds);
currentTrackProgress->setValue(secondsIn);
//currentTrackProgress->setMaximum(track.trackLengthSeconds);
//currentTrackProgress->setValue(secondsIn);
isPaused = groove_playlist_playing(GroovePlayerMgr::getInstance()->currentPlaylist) == 0;
if(!isPaused)
{
trackProgress->start();
}
currentTrackProgress->doJavaScript(app->getProgressNextTrackJs(currentTrackProgress->jsRef(),std::to_string(100/track.trackLengthSeconds),std::to_string((100/track.trackLengthSeconds)*secondsIn)));
playpauseUpdated();
if(GroovePlayerMgr::getInstance()->currentSkipRequester.size() > 0)
{
......
......@@ -49,7 +49,6 @@ public:
//Play controls
TrackDetails* currentTrackDetails;
Wt::WProgressBar* currentTrackProgress;
Wt::WTimer* trackProgress;
Wt::WHBoxLayout* currentTrackLayout;
Wt::WContainerWidget* currentTrackContainer;
Wt::WPushButton* playpause;
......@@ -96,7 +95,6 @@ public:
void updateDetailsFromServer(AudioTrack track);
void updateVoteableTracksFromServer(std::list<AudioTrack> tracks);
void updateVoteTrackState();
void updateProgressFromTimer();
void updateSkipRequestedFromServer();
void updateSkipDeniedFromServer();
void updateVotingEnded(AudioTrack winner);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment