From ab4df846572c4c405d987796e5de8cab6a4f560a Mon Sep 17 00:00:00 2001
From: Kevin Whitaker <eyecreate@gmail.com>
Date: Sun, 29 Jan 2017 17:27:27 -0500
Subject: [PATCH] Added base of code to allow sending backend updates to
 connected clients.

---
 src/GroovePlayer.cpp       | 14 ++++++++++++++
 src/GroovePlayer.h         |  1 +
 src/WebInterface.cpp       | 27 ++++++++++++++++++++++++++-
 src/WebInterface.h         |  6 ++++++
 src/db/UserAction.h        |  2 +-
 src/main.cpp               |  1 +
 src/ui/PlayerInterface.cpp |  3 ++-
 src/ui/PlayerInterface.h   |  2 ++
 8 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/src/GroovePlayer.cpp b/src/GroovePlayer.cpp
index f72fe0a..56892cc 100644
--- a/src/GroovePlayer.cpp
+++ b/src/GroovePlayer.cpp
@@ -22,6 +22,9 @@
 #include "db/AudioTrack.h"
 #include "db/UserAction.h"
 #include <Wt/WLogger>
+#include <Wt/WServer>
+#include <Wt/WApplication>
+#include "WebInterface.h"
 
 GroovePlayer::GroovePlayer(std::string dbFile) : sqliteConnection(dbFile)
 {
@@ -37,3 +40,14 @@ GroovePlayer::GroovePlayer(std::string dbFile) : sqliteConnection(dbFile)
         Wt::log("info") << "Using Existing DB.";
     }
 }
+
+    //TODO:use this code when sending to UI.
+//         Wt::WServer::instance()->postAll([]() {
+//             Wt::WApplication* app = Wt::WApplication::instance();
+//             if(app != nullptr)
+//             {
+//                 static_cast<WebInterface*>(app)->updateUIFromServer();
+//             }
+//         }
+//         );
+
diff --git a/src/GroovePlayer.h b/src/GroovePlayer.h
index 3dc26eb..702fd4c 100644
--- a/src/GroovePlayer.h
+++ b/src/GroovePlayer.h
@@ -36,6 +36,7 @@ private:
     GroovePlayer(std::string dbFile);
     Wt::Dbo::backend::Sqlite3 sqliteConnection;
     Wt::Dbo::Session sqlSession;
+    
 };
 
 #endif // GROOVEPLAYER_H
diff --git a/src/WebInterface.cpp b/src/WebInterface.cpp
index 3f8f09a..497e5b8 100644
--- a/src/WebInterface.cpp
+++ b/src/WebInterface.cpp
@@ -33,6 +33,7 @@ WebInterface::WebInterface(const Wt::WEnvironment& env) : Wt::WApplication(env)
 {
     priv_int = new internal;
     setTitle("Arbitrateor - Audio Jukebox");
+    enableUpdates(true);
     priv_int->playerUI = new PlayerInterface(this);
     priv_int->playerUI->hide();
     priv_int->loginUI = new LoginInterface(this);
@@ -53,5 +54,29 @@ void WebInterface::loginCompleted()
     //Login completed, now bring up main inteface.
     priv_int->loginUI->animateHide(Wt::WAnimation(Wt::WAnimation::AnimationEffect::Fade));
     priv_int->playerUI->animateShow(Wt::WAnimation(Wt::WAnimation::AnimationEffect::Fade, Wt::WAnimation::TimingFunction::EaseIn, 500));
-    //TODO: set up player to dynamically update?
+    //TODO: request current player state.
+}
+
+void WebInterface::playPauseActionFromServer(User* userPausing)
+{
+    priv_int->playerUI->tempText->setText(priv_int->playerUI->tempText->text()+"play");
+    triggerUpdate();
+}
+
+void WebInterface::songChangedFromServer(AudioTrack* nextTrack)
+{
+    priv_int->playerUI->tempText->setText(priv_int->playerUI->tempText->text()+"change");
+    triggerUpdate();
+}
+
+void WebInterface::skipVotedFromServer(User* userRequestingToSkipCurrentTrack)
+{
+    priv_int->playerUI->tempText->setText(priv_int->playerUI->tempText->text()+"skip");
+    triggerUpdate();
+}
+
+void WebInterface::voteUpdateFromServer(User* userVoting, bool forSkip)
+{
+    priv_int->playerUI->tempText->setText(priv_int->playerUI->tempText->text()+"vote");
+    triggerUpdate();
 }
diff --git a/src/WebInterface.h b/src/WebInterface.h
index ca5c0b5..752a26e 100644
--- a/src/WebInterface.h
+++ b/src/WebInterface.h
@@ -22,6 +22,8 @@
 
 #include <Wt/WApplication>
 #include "GroovePlayer.h"
+#include "db/AudioTrack.h"
+#include "db/User.h"
 
 class WebInterface : public Wt::WApplication
 {
@@ -29,6 +31,10 @@ public:
     WebInterface(const Wt::WEnvironment& env);
     ~WebInterface();
     void loginCompleted();
+    void playPauseActionFromServer(User* userPausing);
+    void songChangedFromServer(AudioTrack* nextTrack);
+    void skipVotedFromServer(User* userRequestingToSkipCurrentTrack);
+    void voteUpdateFromServer(User* userVoting, bool forSkip);
 private:
     struct internal;
     internal* priv_int = 0;
diff --git a/src/db/UserAction.h b/src/db/UserAction.h
index 93e5d0b..e720e86 100644
--- a/src/db/UserAction.h
+++ b/src/db/UserAction.h
@@ -29,7 +29,7 @@
 class UserAction
 {
 public:
-    enum UAction {Login = 0, Logout = 1, RequestTrack = 2, VoteTrack = 3, UploadTrack = 4, RequestSkip = 5, Pause = 6};
+    enum UAction {Login = 0, Logout = 1, RequestTrack = 2, VoteTrack = 3, UploadTrack = 4, RequestSkip = 5, PlayPause = 6};
     
     Wt::Dbo::ptr<User> user;
     UAction action;
diff --git a/src/main.cpp b/src/main.cpp
index 2f6bf9d..62d3171 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,6 +17,7 @@ int main ( int argc, char** argv )
         server.addEntryPoint(Wt::Application, createApplication);
         if(server.start())
         {
+            GroovePlayer::getInstance();
             int sig = Wt::WServer::waitForShutdown(argv[0]);
             server.stop();
         }
diff --git a/src/ui/PlayerInterface.cpp b/src/ui/PlayerInterface.cpp
index f6f7ff9..a12c73f 100644
--- a/src/ui/PlayerInterface.cpp
+++ b/src/ui/PlayerInterface.cpp
@@ -23,5 +23,6 @@
 PlayerInterface::PlayerInterface(WebInterface* app)
 {
     this->app = app;
-    addChild(new Wt::WText("You did it."));
+    tempText = new Wt::WText("You did it.");
+    addChild(tempText);
 }
diff --git a/src/ui/PlayerInterface.h b/src/ui/PlayerInterface.h
index 84ed2d1..e8df3f4 100644
--- a/src/ui/PlayerInterface.h
+++ b/src/ui/PlayerInterface.h
@@ -22,12 +22,14 @@
 
 #include <Wt/WContainerWidget>
 #include "../WebInterface.h"
+#include <Wt/WText>
 
 class PlayerInterface : public Wt::WContainerWidget
 {
 public:
     PlayerInterface(WebInterface* app);
     WebInterface* app;
+    Wt::WText* tempText;
 };
 
 #endif // PLAYERINTERFACE_H
-- 
GitLab