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')