Optimize user settings

This commit is contained in:
Bernd Bestel
2018-09-30 17:14:04 +02:00
parent 5c79a80f7a
commit f1c78659be
9 changed files with 87 additions and 126 deletions

View File

@@ -25,3 +25,18 @@ Setting('STOCK_BARCODE_LOOKUP_PLUGIN', 'DemoBarcodeLookupPlugin');
# If, however, your webserver does not support URL rewriting, # If, however, your webserver does not support URL rewriting,
# set this to true # set this to true
Setting('DISABLE_URL_REWRITING', false); Setting('DISABLE_URL_REWRITING', false);
# Default user settings
# These settings can be changed per user, here the defaults
# are defined which are used when the user has not changed the setting so far
# Night mode related
DefaultUserSetting('night_mode_enabled', false); // If night mode is enabled always
DefaultUserSetting('auto_night_mode_enabled', false); // If night mode is enabled automatically when inside a given time range (see the two settings below)
DefaultUserSetting('auto_night_mode_time_range_from', "20:00"); // Format HH:mm
DefaultUserSetting('auto_night_mode_time_range_to', "07:00"); // Format HH:mm
# If the page should be automatically reloaded when there was
# an external change
DefaultUserSetting('auto_reload_on_db_change', true);

View File

@@ -145,6 +145,17 @@ function Setting(string $name, $value)
} }
} }
global $GROCY_DEFAULT_USER_SETTINGS;
$GROCY_DEFAULT_USER_SETTINGS = array();
function DefaultUserSetting(string $name, $value)
{
global $GROCY_DEFAULT_USER_SETTINGS;
if (!array_key_exists($name, $GROCY_DEFAULT_USER_SETTINGS))
{
$GROCY_DEFAULT_USER_SETTINGS[$name] = $value;
}
}
function GetUserDisplayName($user) function GetUserDisplayName($user)
{ {
$displayName = ''; $displayName = '';

View File

@@ -41,3 +41,16 @@ IsTouchInputDevice = function()
return false; return false;
} }
BoolVal = function(test)
{
var anything = test.toString().toLowerCase();
if (anything === true || anything === "true" || anything === "1" || anything === "on")
{
return true
}
else
{
return false;
}
}

View File

@@ -14,7 +14,7 @@
}, },
function(xhr) function(xhr)
{ {
console.log(xhr) Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
} }
); );
} }
@@ -216,3 +216,35 @@ $("form").on("click", "select", function()
{ {
$(this).closest("form").addClass("is-dirty"); $(this).closest("form").addClass("is-dirty");
}); });
// Auto saving user setting controls
$(".user-setting-control").on("change", function()
{
var element = $(this);
var inputType = element.attr("type").toLowerCase();
var settingKey = element.attr("data-setting-key");
if (inputType === "checkbox")
{
value = element.is(":checked");
}
else
{
var value = element.val();
}
Grocy.UserSettings[settingKey] = value;
jsonData = { };
jsonData.value = value;
Grocy.Api.Post('user/settings/' + settingKey, jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
});

View File

@@ -22,7 +22,7 @@ setInterval(function()
{ {
if (Grocy.IdleTime >= 50) if (Grocy.IdleTime >= 50)
{ {
if (Grocy.AutoReloadOnDatabaseChangeEnabled && $("form.is-dirty").length === 0) if (BoolVal(Grocy.UserSettings.auto_reload_on_db_change) && $("form.is-dirty").length === 0)
{ {
window.location.reload(); window.location.reload();
} }
@@ -56,27 +56,7 @@ setInterval(function()
Grocy.IdleTime += 1; Grocy.IdleTime += 1;
}, 1000); }, 1000);
$("#auto-reload-enabled").on("change", function() if (BoolVal(Grocy.UserSettings.auto_reload_on_db_change))
{
var value = $(this).is(":checked");
Grocy.AutoReloadOnDatabaseChangeEnabled = value;
jsonData = { };
jsonData.value = value;
Grocy.Api.Post('user/settings/auto_reload_on_db_change', jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
});
if (Grocy.AutoReloadOnDatabaseChangeEnabled)
{ {
$("#auto-reload-enabled").prop("checked", true); $("#auto-reload-enabled").prop("checked", true);
} }

View File

@@ -1,20 +1,6 @@
$("#night-mode-enabled").on("change", function() $("#night-mode-enabled").on("change", function()
{ {
var value = $(this).is(":checked"); var value = $(this).is(":checked");
jsonData = { };
jsonData.value = value;
Grocy.Api.Post('user/settings/night_mode_enabled', jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
if (value) if (value)
{ {
$("body").addClass("night-mode"); $("body").addClass("night-mode");
@@ -28,67 +14,9 @@
$("#auto-night-mode-enabled").on("change", function() $("#auto-night-mode-enabled").on("change", function()
{ {
var value = $(this).is(":checked"); var value = $(this).is(":checked");
$("#auto-night-mode-time-range-from").prop("readonly", !value);
jsonData = { }; $("#auto-night-mode-time-range-to").prop("readonly", !value);
jsonData.value = value;
Grocy.Api.Post('user/settings/auto_night_mode_enabled', jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
if (value)
{
$("#auto-night-mode-time-range-from").prop("readonly", false);
$("#auto-night-mode-time-range-to").prop("readonly", false);
}
else
{
$("#auto-night-mode-time-range-from").prop("readonly", true);
$("#auto-night-mode-time-range-to").prop("readonly", true);
}
});
$("#auto-night-mode-time-range-from").on("blur", function()
{
var value = $(this).val();
jsonData = { };
jsonData.value = value;
Grocy.Api.Post('user/settings/auto_night_mode_time_range_from', jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
});
$("#auto-night-mode-time-range-to").on("blur", function()
{
var value = $(this).val();
jsonData = { };
jsonData.value = value;
Grocy.Api.Post('user/settings/auto_night_mode_time_range_to', jsonData,
function(result)
{
// Nothing to do...
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
}); });
$("#night-mode-enabled").prop("checked", Grocy.NightModeEnabled); $("#night-mode-enabled").prop("checked", Grocy.NightModeEnabled);
$("#auto-night-mode-enabled").prop("checked", Grocy.AutoNightModeEnabled); $("#auto-night-mode-enabled").prop("checked", Grocy.UserSettings.auto_night_mode_enabled);

View File

@@ -4,7 +4,6 @@
var jsonData = $('#recipe-pos-form').serializeJSON({ checkboxUncheckedValue: "0" }); var jsonData = $('#recipe-pos-form').serializeJSON({ checkboxUncheckedValue: "0" });
jsonData.recipe_id = Grocy.EditObjectParentId; jsonData.recipe_id = Grocy.EditObjectParentId;
console.log(jsonData);
if (Grocy.EditMode === 'create') if (Grocy.EditMode === 'create')
{ {
Grocy.Api.Post('add-object/recipes_pos', jsonData, Grocy.Api.Post('add-object/recipes_pos', jsonData,

View File

@@ -73,7 +73,9 @@ class UsersService extends BaseService
$settings[$settingRow->key] = $settingRow->value; $settings[$settingRow->key] = $settingRow->value;
} }
return $settings; // Use the configured default values for all missing settings
global $GROCY_DEFAULT_USER_SETTINGS;
return array_merge($GROCY_DEFAULT_USER_SETTINGS, $settings);
} }
public function SetUserSetting($userId, $settingKey, $settingValue) public function SetUserSetting($userId, $settingKey, $settingValue)

View File

@@ -42,30 +42,11 @@
Grocy.ActiveNav = '@yield('activeNav', '')'; Grocy.ActiveNav = '@yield('activeNav', '')';
Grocy.Culture = '{{ GROCY_CULTURE }}'; Grocy.Culture = '{{ GROCY_CULTURE }}';
Grocy.Currency = '{{ GROCY_CURRENCY }}'; Grocy.Currency = '{{ GROCY_CURRENCY }}';
Grocy.UserSettings = {!! json_encode($userSettings) !!};
@if(array_key_exists('auto_reload_on_db_change', $userSettings))
Grocy.AutoReloadOnDatabaseChangeEnabled = {{ BoolToString($userSettings['auto_reload_on_db_change']) }};
@else
Grocy.AutoReloadOnDatabaseChangeEnabled = true;
@endif
@if(array_key_exists('night_mode_enabled', $userSettings))
@php $nightModeEnabled = boolval($userSettings['night_mode_enabled']); @endphp
Grocy.NightModeEnabled = {{ BoolToString($userSettings['night_mode_enabled']) }};
@else
@php $nightModeEnabled = false; @endphp
Grocy.NightModeEnabled = false;
@endif
@if(array_key_exists('auto_night_mode_enabled', $userSettings))
Grocy.AutoNightModeEnabled = {{ BoolToString($userSettings['auto_night_mode_enabled']) }};
@else
Grocy.AutoNightModeEnabled = false;
@endif
</script> </script>
</head> </head>
<body class="fixed-nav @if($nightModeEnabled == true) night-mode @endif"> <body class="fixed-nav @if(boolval($userSettings['night_mode_enabled'])) night-mode @endif">
<nav id="mainNav" class="navbar navbar-expand-lg navbar-light fixed-top"> <nav id="mainNav" class="navbar navbar-expand-lg navbar-light fixed-top">
<a class="navbar-brand py-0" href="{{ $U('/') }}"><img src="{{ $U('/img/grocy_logo.svg?v=', true) }}{{ $version }}" height="30"></a> <a class="navbar-brand py-0" href="{{ $U('/') }}"><img src="{{ $U('/img/grocy_logo.svg?v=', true) }}{{ $version }}" height="30"></a>
@@ -224,7 +205,7 @@
<div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-menu dropdown-menu-right">
<div class="dropdown-item"> <div class="dropdown-item">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" id="auto-reload-enabled"> <input class="form-check-input user-setting-control" type="checkbox" id="auto-reload-enabled" data-setting-key="auto_reload_on_db_change">
<label class="form-check-label" for="auto-reload-enabled"> <label class="form-check-label" for="auto-reload-enabled">
{{ $L('Auto reload on external changes') }} {{ $L('Auto reload on external changes') }}
</label> </label>
@@ -233,7 +214,7 @@
<div class="dropdown-divider"></div> <div class="dropdown-divider"></div>
<div class="dropdown-item"> <div class="dropdown-item">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" id="night-mode-enabled"> <input class="form-check-input user-setting-control" type="checkbox" id="night-mode-enabled" data-setting-key="night_mode_enabled">
<label class="form-check-label" for="night-mode-enabled"> <label class="form-check-label" for="night-mode-enabled">
{{ $L('Enable night mode') }} {{ $L('Enable night mode') }}
</label> </label>
@@ -241,14 +222,14 @@
</div> </div>
<div class="dropdown-item"> <div class="dropdown-item">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" id="auto-night-mode-enabled"> <input class="form-check-input user-setting-control" type="checkbox" id="auto-night-mode-enabled" data-setting-key="auto_night_mode_enabled">
<label class="form-check-label" for="auto-night-mode-enabled"> <label class="form-check-label" for="auto-night-mode-enabled">
{{ $L('Auto enable in time range') }} {{ $L('Auto enable in time range') }}
</label> </label>
</div> </div>
<div class="form-inline"> <div class="form-inline">
<input type="text" class="form-control my-1" readonly id="auto-night-mode-time-range-from" placeholder="{{ $L('From') }} ({{ $L('in format') }} HH:mm)"> <input type="text" class="form-control my-1 user-setting-control" readonly id="auto-night-mode-time-range-from" placeholder="{{ $L('From') }} ({{ $L('in format') }} HH:mm)" data-setting-key="auto_night_mode_time_range_from">
<input type="text" class="form-control" readonly id="auto-night-mode-time-range-to" placeholder="{{ $L('To') }} ({{ $L('in format') }} HH:mm)"> <input type="text" class="form-control user-setting-control" readonly id="auto-night-mode-time-range-to" placeholder="{{ $L('To') }} ({{ $L('in format') }} HH:mm)" data-setting-key="auto_night_mode_time_range_to">
</div> </div>
</div> </div>
</div> </div>