From df0e044a96f716122756e67c9cbdddaa529f90f4 Mon Sep 17 00:00:00 2001 From: Kevin Whitaker Date: Thu, 23 Feb 2017 18:32:39 -0500 Subject: [PATCH] Convert timer to javascript to cut down on requests. --- src/WebInterface.cpp | 16 ++++++++++++++++ src/WebInterface.h | 3 +++ src/ui/PlayerInterface.cpp | 29 +++++------------------------ src/ui/PlayerInterface.h | 2 -- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/WebInterface.cpp b/src/WebInterface.cpp index 447eb45..a44a717 100644 --- a/src/WebInterface.cpp +++ b/src/WebInterface.cpp @@ -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();"; +} + diff --git a/src/WebInterface.h b/src/WebInterface.h index d4c2f01..84860cc 100644 --- a/src/WebInterface.h +++ b/src/WebInterface.h @@ -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; diff --git a/src/ui/PlayerInterface.cpp b/src/ui/PlayerInterface.cpp index d6262a1..c2ee05e 100644 --- a/src/ui/PlayerInterface.cpp +++ b/src/ui/PlayerInterface.cpp @@ -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) { diff --git a/src/ui/PlayerInterface.h b/src/ui/PlayerInterface.h index 669761a..053c28a 100644 --- a/src/ui/PlayerInterface.h +++ b/src/ui/PlayerInterface.h @@ -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 tracks); void updateVoteTrackState(); - void updateProgressFromTimer(); void updateSkipRequestedFromServer(); void updateSkipDeniedFromServer(); void updateVotingEnded(AudioTrack winner);