From 621085672406145ea9c0e2315fa770bfc3e7515d Mon Sep 17 00:00:00 2001
From: "Biancaniello, Michael" <chepazzo@gmail.com>
Date: Fri, 9 Sep 2016 23:00:41 -0400
Subject: [PATCH] Updated settings view for nicer ui wrt alwayson.

---
 config/jukeberry.example.conf     |  4 ++--
 jukeberry/jukebox.py              |  8 +++++---
 jukeberry/server.py               |  4 ++--
 jukeberry/templates/JukeCtrl.js   | 30 ++++++++++++++++++++++++++----
 jukeberry/templates/settings.html | 24 +++++++++++++++++-------
 5 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/config/jukeberry.example.conf b/config/jukeberry.example.conf
index 4b59ac6..07abcc8 100644
--- a/config/jukeberry.example.conf
+++ b/config/jukeberry.example.conf
@@ -1,10 +1,10 @@
 [GLOBAL]
 DEBUG=True
-THREADED=True
+THREADED=False
 SSL=False
 PLAYER=omxplayer
 #PLAYER=mpg123
-LIB=/var/media/music/
+LIB=/var/media/music/Tomahawk/
 
 [SSL]
 SSL_CRT=/path/to/crt.crt
diff --git a/jukeberry/jukebox.py b/jukeberry/jukebox.py
index 9b915ac..49fad4f 100644
--- a/jukeberry/jukebox.py
+++ b/jukeberry/jukebox.py
@@ -62,7 +62,7 @@ class Jukebox(object):
         Dict Structure:
           status (bool): Is the Jukebox always on? If so, then a random song will play
             if the queue is empty.
-          filter (Dict[str,str]): This will filter the list of songs available to be
+          filters (List[Dict[str,str]]): This will filter the list of songs available to be
             randomly selected to play (if the filter is on), where the ``key`` is an
             attribute of ``catalog.Song``.
 
@@ -84,7 +84,7 @@ def __init__(self,player=None,medialib=None):
         self.playlist = []
         self.alwayson = {
             "status":False,
-            "filter":{}
+            "filters":[]
         }
         self.currsong = None
         self.proc = None
@@ -146,7 +146,9 @@ def play_next_song(self):
         if song is None:
             if self.alwayson['status']:
                 print("No next song ... finding random.")
-                song = self.songlist.get_random_song(**self.alwayson['filter'])
+                kwargs = {self.alwayson['filters'][0]['attr']:self.alwayson['filters'][0]['value']}
+                print("kwargs = ", kwargs)
+                song = self.songlist.get_random_song(**kwargs)
         filename = None
         if type(song) == catalog.Song:
             filename = song.filename
diff --git a/jukeberry/server.py b/jukeberry/server.py
index eb93738..35a14e2 100644
--- a/jukeberry/server.py
+++ b/jukeberry/server.py
@@ -219,8 +219,8 @@ def set_alwayson():
     else:
         if 'status' in content.keys():
             JUKE.alwayson['status'] = content['status']
-        if 'filter' in content.keys():
-            JUKE.alwayson['filter'] = content['filter']
+        if 'filters' in content.keys():
+            JUKE.alwayson['filters'] = content['filters']
     return jsonify(succ(value=JUKE.alwayson))
 
 @app.route('/add', methods = ['POST'])
diff --git a/jukeberry/templates/JukeCtrl.js b/jukeberry/templates/JukeCtrl.js
index 977a96a..0621d04 100644
--- a/jukeberry/templates/JukeCtrl.js
+++ b/jukeberry/templates/JukeCtrl.js
@@ -7,7 +7,10 @@ jukeApp.config(['$interpolateProvider', function ($interpolateProvider) {
 
 jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
     // data
-    $scope.alwayson = false;
+    $scope.alwayson = {
+        "status":false,
+        "filters":[]
+    };
     $scope.tiles="#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     $scope.songs = [];
     $scope.artists = [];
@@ -41,6 +44,7 @@ jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
             if (angular.isDefined(auto)) { return; }
             console.log('turning on auto refresh.');
             auto = $interval(function() {
+                $scope.get_alwayson();
                 $scope.get_playlist();
                 $scope.get_currsong();
             },10000);
@@ -60,7 +64,7 @@ jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
         //$location.replace();
         window.open(url);
     };
-    $scope.setalwayson = function(action) {
+    $scope.set_alwayson = function(action) {
         // The problem with this is that turning the feature off
         // resets the filter to the current attr:value.
         // This means that to put it back, you have to navigate first
@@ -71,10 +75,12 @@ jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
         var filter = {};
         var data = {
             "status":action,
-            "filter":filter
+            "filters":[filter]
         };
+        $scope.alwayson = data;
         if (attr != '' && value != '') {
-            filter[attr] = value;
+            filter['attr'] = attr;
+            filter['value'] = value;
         }
         var url = '{{url_for('set_alwayson')}}';
         var method = 'POST';
@@ -89,6 +95,21 @@ jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
             console.log(status);
         });
     };
+    $scope.get_alwayson = function() {
+        var url = '{{url_for('get_alwayson')}}';
+        var method = 'GET';
+        $http(
+            {method: method, url: url}
+        ).success(function(data, status) {
+            $scope.alwayson = data.data;
+            console.log(data.data);
+            console.log(status);
+        }).error(function(data, status) {
+            console.log('ERROR');
+            console.log(data);
+            console.log(status);
+        });
+    };
     $scope.play = function(artist,title) {
         var data = {'artist':artist,'title':title};
         var url = '{{url_for('add')}}';
@@ -222,6 +243,7 @@ jukeApp.controller('JukeCtrl', function ($scope,$http,$interval,$location) {
     $scope.get_playlist();
     $scope.get_currsong();
     $scope.get_songs();
+    $scope.get_alwayson();
 });
 
 jukeApp.filter('secs2hms', function() {
diff --git a/jukeberry/templates/settings.html b/jukeberry/templates/settings.html
index 64cdb14..f03377b 100644
--- a/jukeberry/templates/settings.html
+++ b/jukeberry/templates/settings.html
@@ -12,6 +12,14 @@
   <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='jukeberry.css')}}" />
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
   <script src="/JukeCtrl.js"></script>
+
+  <style>
+    #alwayson .active { color:red; }
+    #alwayson .button {
+        margin:2px;
+    }
+  </style>
+
  </head>
  <body>
   <div id='head'>
@@ -19,16 +27,18 @@
   </div><!-- id='head' -->
   <div id='body' class='raspi' ng-controller="JukeCtrl">
    <div id='alwayson'>
-     <div id='seton' class='clickable button'
-        ng-click='setalwayson(true);'>
-      on</div>
-     <div id='setoff' class='clickable button'
-        ng-click='setalwayson(false);'>
-      off</div>
+     <span id='seton' class='clickable button'
+        ng-click='set_alwayson(true);'
+        ng-class='{"active":alwayson.status}'
+      >on</span>
+     <span id='setoff' class='clickable button'
+        ng-click='set_alwayson(false);'
+        ng-class='{"active":!alwayson.status}'
+      >off</span>
    </div>
 
    <div>
-    {[currattr]}:{[currvalue]}
+    {[alwayson.filters.length > 0 ? alwayson.filters : "Everything"]}
    </div>
 
 <select ng-model="currattr">