From 1a5daf3d66ed9fdaa86b4043aac0d547e475643b Mon Sep 17 00:00:00 2001 From: Felix Bruns Date: Thu, 9 May 2024 18:04:31 +0200 Subject: [PATCH] Lock screen after 1 minut off inactivity --- .../proxmox_lister_list_view.dart | 43 +++++++++++++------ lib/src/screen_helper.dart | 43 +++++++++++++++++++ 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/lib/src/proxmox_lister/proxmox_lister_list_view.dart b/lib/src/proxmox_lister/proxmox_lister_list_view.dart index 2a5d8bd..e633706 100644 --- a/lib/src/proxmox_lister/proxmox_lister_list_view.dart +++ b/lib/src/proxmox_lister/proxmox_lister_list_view.dart @@ -36,7 +36,7 @@ class _ProxmoxListerState extends State { nodes = Future.delayed(Duration.zero, () => getVms()); Timer.periodic(const Duration(seconds: 3), (_) { - syncVMs(); + syncVMs((_) {}); }); } @@ -60,23 +60,33 @@ class _ProxmoxListerState extends State { return map; } - void syncVMs() async { + void syncVMs(Function(Exception) onExcept) async { nodes = getVms(); - await nodes; + try { + await nodes; + } on Exception catch (e) { + onExcept(e); + } setState(() {}); } @override Widget build(BuildContext context) { // infinite touch container to turn screen back on - return Stack( - children: [ - Scaffold( - appBar: appbar(context), - body: bodyBuilder(context), - ), - screenActivator(), - ], + return IdleTurnOff( + timeout: const Duration(seconds: 60), + onExpire: () async { + await turnOffScreen(); + }, + child: Stack( + children: [ + Scaffold( + appBar: appbar(context), + body: bodyBuilder(context), + ), + screenActivator(), + ], + ), ); } @@ -93,7 +103,16 @@ class _ProxmoxListerState extends State { IconButton( icon: const Icon(Icons.sync), onPressed: () { - syncVMs(); + syncVMs((e) { + showDialog( + context: context, + builder: (BuildContext ctx) { + return AlertDialog( + title: const Text("Error"), + content: Text(e.toString()), + ); + }); + }); }, ), IconButton( diff --git a/lib/src/screen_helper.dart b/lib/src/screen_helper.dart index 580af19..4443e04 100644 --- a/lib/src/screen_helper.dart +++ b/lib/src/screen_helper.dart @@ -1,5 +1,6 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:async/async.dart'; const _off = "1"; const _on = "0"; @@ -48,3 +49,45 @@ Widget screenActivator() { ), ); } + +class IdleTurnOff extends StatefulWidget { + const IdleTurnOff({ + super.key, + required this.child, + required this.timeout, + required this.onExpire, + }); + + final Widget child; + final Duration timeout; + final Function() onExpire; + + @override + State createState() => _IdleTurnOffState(); +} + +class _IdleTurnOffState extends State { + late RestartableTimer _timer; + + @override + void initState() { + super.initState(); + _timer = RestartableTimer(widget.timeout, widget.onExpire); + } + + @override + Widget build(BuildContext context) { + return KeyboardListener( + focusNode: FocusNode( + onKeyEvent: (_1, _2) { + _timer.reset(); + return KeyEventResult.ignored; + }, + ), + child: Listener( + child: widget.child, + onPointerDown: (_) => _timer.reset(), + ), + ); + } +}