Update
This commit is contained in:
parent
5865aa445f
commit
ef3379f1d9
3 changed files with 33 additions and 3 deletions
|
@ -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) {
|
||||
|
|
|
@ -13,6 +13,7 @@ class Mailer {
|
|||
];
|
||||
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
|
||||
static MailClient? _mailClient;
|
||||
static Timer? _timer;
|
||||
|
||||
/// High level mail API example
|
||||
static Future<bool> connect({
|
||||
|
@ -45,7 +46,8 @@ class Mailer {
|
|||
_mailClient!.eventBus.on<ImapEvent>().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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue