remember volume state

This commit is contained in:
Jonas Heinrich 2018-08-15 17:34:20 +02:00
parent 07ce3e5aeb
commit 59d90e2f31
4 changed files with 107 additions and 1 deletions

View File

@ -6,6 +6,8 @@
[#63](https://git.project-insanity.org/onny/nextcloud-app-radio/issues/63) @onny
- Add icon categories
[#](https://git.project-insanity.org/onny/nextcloud-app-radio/commit/b7e711955b90f388a5e340ab582461fd39df8969) @onny
- Add volume icon
[#](https://git.project-insanity.org/onny/nextcloud-app-radio/commit/07ce3e5aebd5e5b5bf91e3aae996441ae9268402) @onny
### Fixed
- Fix styling issue (overflow hidden) player area

View File

@ -17,7 +17,9 @@ return [
['name' => 'page#categories', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#top', 'url' => '/', 'verb' => 'GET'],
['name' => 'settings#getMenuState', 'url' => '/getMenuState', 'verb' => 'GET'],
['name' => 'settings#getVolumeState', 'url' => '/getVolumeState', 'verb' => 'GET'],
['name' => 'settings#saveMenuState', 'url' => '/saveMenuState', 'verb' => 'POST'],
['name' => 'settings#saveVolumeState', 'url' => '/saveVolumeState', 'verb' => 'POST'],
['name' => 'station_api#preflighted_cors', 'url' => '/api/0.1/{path}',
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']]
]

View File

@ -51,4 +51,37 @@ class SettingsController extends Controller {
return $response;
}
/**
* Save volume for current user
*
* @NoAdminRequired
* @return array response
*/
public function getVolumeState() {
$volume_state = $this->appConfig->getUserValue('volume_state', $this->userId);
$response = array(
'status' => 'success',
'data' => array('message' => 'User volume saved successfully.'),
'volume_state' => $volume_state
);
return $response;
}
/**
* Save volume for current user
*
* @NoAdminRequired
* @param $menu_state string
* @return array response
*/
public function saveVolumeState($volume_state) {
$this->appConfig->setUserValue('volume_state', $this->userId, $volume_state);
$response = array(
'status' => 'success',
'data' => array('message' => 'User volume saved successfully.'),
'volume_state' => $volume_state
);
return $response;
}
}

View File

@ -377,6 +377,22 @@ $(function(){
switch_menu(0);
});
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
// Volume slider
$('#volumeslider').slider({
orientation: "horizontal",
@ -387,7 +403,6 @@ $(function(){
animate: true,
step: .1,
slide: function(e, ui) {
player.volume = ui.value;
if (ui.value < 0.2) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","silent");
@ -403,6 +418,57 @@ $(function(){
}
});
$("#volumeslider").on("slidestop", debounce(function(e, ui) {
player.volume = ui.value;
save_volume_state(ui.value);
}, 700)
);
function load_volume_state() {
var volume_state = 0;
var baseUrl = OC.generateUrl('/apps/radio/getVolumeState');
$.get(baseUrl, function ( data ) {
if ("volume_state" in data) {
volume_state = data["volume_state"];
if (volume_state == "") {
player.volume = 1;
$('#volumeslider').slider('value',1);
} else {
player.volume = Number(volume_state);
$('#volumeslider').slider('value',Number(volume_state));
if (volume_state < 0.2) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","silent");
}
if (volume_state > 0.5) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","full");
}
if (volume_state < 0.5 && volume_state > 0.2) {
$("#volumeicon").removeClass();
$("#volumeicon").attr("class","mid");
}
}
}
return true;
});
}
function save_volume_state(volume_state) {
var baseUrl = OC.generateUrl('/apps/radio/saveVolumeState');
var settings = {
"volume_state": volume_state
};
$.ajax({
url: baseUrl,
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(settings)
}).done(function(data){
return true;
});
}
// On app start, load top list
if(window.location.hash) {
var hash = String(window.location.hash.replace('#',''));
@ -424,4 +490,7 @@ $(function(){
load_menu_state();
};
// Load volume state
load_volume_state();
});