From ef3379f1d98ff2a6c19aafcae6b55987ed79a5f0 Mon Sep 17 00:00:00 2001 From: "Florian RICHER (MrDev023)" Date: Tue, 26 Apr 2022 20:29:09 +0200 Subject: [PATCH] Update --- lib/classes/stream_element.dart | 2 +- lib/utils/mailer.dart | 20 ++++++++++++++++++- .../components/stream_element_card.dart | 14 ++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/classes/stream_element.dart b/lib/classes/stream_element.dart index 716c53a..4b6b81c 100644 --- a/lib/classes/stream_element.dart +++ b/lib/classes/stream_element.dart @@ -1,6 +1,6 @@ import 'package:uuid/uuid.dart'; -enum StreamElementType { log, alert } +enum StreamElementType { log, warning, error, alert } class StreamElement { StreamElement(this.type, this.message) { diff --git a/lib/utils/mailer.dart b/lib/utils/mailer.dart index 09bd007..0f2c8ce 100644 --- a/lib/utils/mailer.dart +++ b/lib/utils/mailer.dart @@ -13,6 +13,7 @@ class Mailer { ]; static final List whiteListEmail = ['louis@holyenergy.fr']; static MailClient? _mailClient; + static Timer? _timer; /// High level mail API example static Future connect({ @@ -45,7 +46,8 @@ class Mailer { _mailClient!.eventBus.on().listen((ImapEvent event) async { await _onMessage(event, streamController, email, whitelist); }); - await _mailClient!.startPolling(); + await _mailClient!.startPolling(const Duration(seconds: 1)); + _startDebugProfiler(streamController); return true; } on MailException catch (e) { streamController.add(StreamElement(StreamElementType.log, e.toString())); @@ -59,6 +61,7 @@ class Mailer { }) async { if (_mailClient == null) return; Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail'); + _stopDebugProfiler(); await _mailClient!.stopPolling(); await _mailClient!.disconnect(); Logger.log(LoggerType.info, Mailer, 'Disconnected from mail'); @@ -103,4 +106,19 @@ class Mailer { await Requests.sendAlert(message, type: AlertType.host); streamController.add(StreamElement(StreamElementType.alert, message)); } + + static void _startDebugProfiler(StreamController streamController) { + _timer = Timer.periodic(const Duration(seconds: 1), (timer) async { + if (!_mailClient!.isPolling()) { + streamController.add(StreamElement(StreamElementType.warning, + 'Mail client is not polling | Restart it')); + await _mailClient!.startPolling(); + } + }); + } + + static void _stopDebugProfiler() { + _timer?.cancel(); + _timer = null; + } } diff --git a/lib/widgets/components/stream_element_card.dart b/lib/widgets/components/stream_element_card.dart index 249ff30..47330fb 100644 --- a/lib/widgets/components/stream_element_card.dart +++ b/lib/widgets/components/stream_element_card.dart @@ -12,12 +12,23 @@ class StreamElementCard extends StatelessWidget { @override Widget build(BuildContext context) { IconData icon; + Color textColor; switch (streamElement.type) { case StreamElementType.log: icon = Icons.info; + textColor = Colors.white; break; case StreamElementType.alert: icon = Icons.notifications_active; + textColor = Colors.white; + break; + case StreamElementType.warning: + icon = Icons.warning_rounded; + textColor = Colors.yellow[600]!; + break; + case StreamElementType.error: + icon = Icons.error_rounded; + textColor = Colors.red[300]!; break; } return Card( @@ -36,12 +47,13 @@ class StreamElementCard extends StatelessWidget { Icon( icon, size: 30, + color: textColor, ), const SizedBox(width: 8), Expanded( child: Text( streamElement.message, - style: const TextStyle(fontSize: 16), + style: TextStyle(fontSize: 16, color: textColor), ), ), if (streamElement.type == StreamElementType.alert)