From 2a0ec30bb0d81d8be2d51633fc81993af83f5e6c Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 24 Sep 2018 13:53:18 +0200 Subject: [PATCH] Auto reload the current page when the database has changed and when idling (closes #59) --- controllers/SystemApiController.php | 23 ++++++++++++ grocy.openapi.json | 29 +++++++++++++++ public/js/grocy_dbchangedhandling.js | 53 ++++++++++++++++++++++++++++ routes.php | 3 ++ services/DatabaseService.php | 5 +++ views/layout/default.blade.php | 1 + 6 files changed, 114 insertions(+) create mode 100644 controllers/SystemApiController.php create mode 100644 public/js/grocy_dbchangedhandling.js diff --git a/controllers/SystemApiController.php b/controllers/SystemApiController.php new file mode 100644 index 00000000..b72939a2 --- /dev/null +++ b/controllers/SystemApiController.php @@ -0,0 +1,23 @@ +DatabaseService = new DatabaseService(); + } + + protected $DatabaseService; + + public function GetDbChangedTime(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + return $this->ApiResponse(array( + 'changed_time' => $this->DatabaseService->GetDbChangedTime() + )); + } +} diff --git a/grocy.openapi.json b/grocy.openapi.json index 1566a031..5e356677 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -24,6 +24,26 @@ } ], "paths": { + "/system/get-db-changed-time": { + "get": { + "description": "Returns the time when the database was last changed", + "tags": [ + "System" + ], + "responses": { + "200": { + "description": "An DbChangedTimeResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DbChangedTimeResponse" + } + } + } + } + } + } + }, "/get-objects/{entity}": { "get": { "description": "Returns all objects of the given entity", @@ -2009,6 +2029,15 @@ "format": "date-time" } } + }, + "DbChangedTimeResponse": { + "type": "object", + "properties": { + "changed_time": { + "type": "string", + "format": "date-time" + } + } } }, "examples": { diff --git a/public/js/grocy_dbchangedhandling.js b/public/js/grocy_dbchangedhandling.js new file mode 100644 index 00000000..00fa01fe --- /dev/null +++ b/public/js/grocy_dbchangedhandling.js @@ -0,0 +1,53 @@ +Grocy.Api.Get('system/get-db-changed-time', + function(result) + { + Grocy.DatabaseChangedTime = moment(result.changed_time); + }, + function(xhr) + { + console.error(xhr); + } +); + +// Check if the database has changed once a minute +// If a change is detected, reload the current page, but only if already idling for at least 50 seconds +setInterval(function() +{ + Grocy.Api.Get('system/get-db-changed-time', + function(result) + { + var newDbChangedTime = moment(result.changed_time); + if (newDbChangedTime.isSameOrAfter(Grocy.DatabaseChangedTime)) + { + if (Grocy.IdleTime >= 50) + { + window.location.reload(); + } + + Grocy.DatabaseChangedTime = newDbChangedTime; + } + }, + function(xhr) + { + console.error(xhr); + } + ); +}, 60000); + +Grocy.IdleTime = 0; +Grocy.ResetIdleTime = function() +{ + Grocy.IdleTime = 0; +} +window.onmousemove = Grocy.ResetIdleTime; +window.onmousedown = Grocy.ResetIdleTime; +window.onclick = Grocy.ResetIdleTime; +window.onscroll = Grocy.ResetIdleTime; +window.onkeypress = Grocy.ResetIdleTime; + +// Increase the idle time once every second +// On any interaction it will be reset to 0 (see above) +setInterval(function() +{ + Grocy.IdleTime += 1; +}, 1000); diff --git a/routes.php b/routes.php index fe6c3ddb..b32b22cd 100644 --- a/routes.php +++ b/routes.php @@ -79,6 +79,9 @@ $app->group('/api', function() $this->post('/edit-object/{entity}/{objectId}', '\Grocy\Controllers\GenericEntityApiController:EditObject'); $this->get('/delete-object/{entity}/{objectId}', '\Grocy\Controllers\GenericEntityApiController:DeleteObject'); + // System + $this->get('/system/get-db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime'); + // Users $this->get('/users/get', '\Grocy\Controllers\UsersApiController:GetUsers'); $this->post('/users/create', '\Grocy\Controllers\UsersApiController:CreateUser'); diff --git a/services/DatabaseService.php b/services/DatabaseService.php index dc726d87..e488de79 100644 --- a/services/DatabaseService.php +++ b/services/DatabaseService.php @@ -63,4 +63,9 @@ class DatabaseService return false; } + + public function GetDbChangedTime() + { + return date('Y-m-d H:i:s', filemtime(GROCY_DATAPATH . '/grocy.db')); + } } diff --git a/views/layout/default.blade.php b/views/layout/default.blade.php index 9c7df66c..3d9b0091 100644 --- a/views/layout/default.blade.php +++ b/views/layout/default.blade.php @@ -269,6 +269,7 @@ + @stack('pageScripts') @stack('componentScripts')