Add steam message
This commit is contained in:
parent
1351130f83
commit
f2d808f5d3
8 changed files with 226 additions and 68 deletions
13
lib/classes/stream_element.dart
Normal file
13
lib/classes/stream_element.dart
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
enum StreamElementType { log, alert }
|
||||||
|
|
||||||
|
class StreamElement {
|
||||||
|
StreamElement(this.type, this.message) {
|
||||||
|
uuid = const Uuid().v4();
|
||||||
|
}
|
||||||
|
|
||||||
|
late final String uuid;
|
||||||
|
final String message;
|
||||||
|
final StreamElementType type;
|
||||||
|
}
|
|
@ -1,28 +1,38 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:desktopapp/classes/stream_element.dart';
|
||||||
import 'package:desktopapp/utils/logger.dart';
|
import 'package:desktopapp/utils/logger.dart';
|
||||||
import 'package:desktopapp/utils/requests.dart';
|
|
||||||
import 'package:enough_mail/enough_mail.dart';
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
|
|
||||||
class Mailer {
|
class Mailer {
|
||||||
|
static MailClient? _mailClient;
|
||||||
|
|
||||||
/// High level mail API example
|
/// High level mail API example
|
||||||
static Future<void> mailExample() async {
|
static Future<bool> connect({
|
||||||
const email = 'trexricher1997@gmail.com';
|
required String email,
|
||||||
const password = 'rugnxbafhcwsykzt';
|
required String password,
|
||||||
|
required StreamController<StreamElement> streamController,
|
||||||
|
}) async {
|
||||||
Logger.log(LoggerType.info, Mailer, 'Discovering settings for $email');
|
Logger.log(LoggerType.info, Mailer, 'Discovering settings for $email');
|
||||||
final config = await Discover.discover(email);
|
final config = await Discover.discover(email);
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
Logger.log(LoggerType.info, Mailer,
|
Logger.log(LoggerType.info, Mailer,
|
||||||
'Unable to autodiscover settings for $email');
|
'Unable to autodiscover settings for $email');
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
Logger.log(LoggerType.info, Mailer, 'Settings for $email : $config');
|
Logger.log(LoggerType.info, Mailer, 'Settings for $email : $config');
|
||||||
final account = MailAccount.fromDiscoveredSettings(
|
final account = MailAccount.fromDiscoveredSettings(
|
||||||
'my account', email, password, config);
|
'my account', email, password, config);
|
||||||
final mailClient = MailClient(account, isLogEnabled: true);
|
_mailClient = MailClient(account, isLogEnabled: true);
|
||||||
try {
|
try {
|
||||||
await mailClient.connect();
|
await _mailClient!.connect();
|
||||||
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
|
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
|
||||||
await mailClient.selectInbox();
|
streamController
|
||||||
mailClient.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
.add(StreamElement(StreamElementType.log, 'Connected to $email'));
|
||||||
|
await _mailClient!.selectInbox();
|
||||||
|
streamController.add(
|
||||||
|
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
|
||||||
|
_mailClient!.eventBus.on<ImapEvent>().listen((ImapEvent event) async {
|
||||||
if (event.eventType == ImapEventType.exists) {
|
if (event.eventType == ImapEventType.exists) {
|
||||||
final evt = event as ImapMessagesExistEvent;
|
final evt = event as ImapMessagesExistEvent;
|
||||||
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
|
if (evt.newMessagesExists <= evt.oldMessagesExists) return;
|
||||||
|
@ -32,22 +42,35 @@ class Mailer {
|
||||||
} else {
|
} else {
|
||||||
sequence.add(evt.newMessagesExists);
|
sequence.add(evt.newMessagesExists);
|
||||||
}
|
}
|
||||||
final messages = await mailClient.fetchMessageSequence(sequence,
|
final messages = await _mailClient!.fetchMessageSequence(sequence,
|
||||||
fetchPreference: FetchPreference.full);
|
fetchPreference: FetchPreference.envelope);
|
||||||
for (final message in messages) {
|
for (final message in messages) {
|
||||||
var mailEvt = MailLoadEvent(message, mailClient);
|
var mailEvt = MailLoadEvent(message, _mailClient!);
|
||||||
var content = mailEvt.message.decodeSubject();
|
streamController.add(StreamElement(StreamElementType.alert,
|
||||||
var mess = mailEvt.message.decodeTextPlainPart();
|
mailEvt.message.decodeSubject() ?? ''));
|
||||||
Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
|
// var content = mailEvt.message.decodeSubject();
|
||||||
Requests.sendAlert('*$content* $mess');
|
// var mess = mailEvt.message.decodeTextPlainPart();
|
||||||
|
|
||||||
|
// Logger.log(LoggerType.info, Mailer, 'New message $content : $mess');
|
||||||
|
// Requests.sendAlert('*$content* $mess');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Logger.log(LoggerType.info, Mailer, 'Start polling mail for $email');
|
await _mailClient!.startPolling();
|
||||||
await mailClient.startPolling(const Duration(seconds: 30));
|
return true;
|
||||||
Logger.log(LoggerType.info, Mailer, 'Finish polling mail for $email');
|
|
||||||
} on MailException catch (e) {
|
} on MailException catch (e) {
|
||||||
|
streamController.add(StreamElement(StreamElementType.log, e.toString()));
|
||||||
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
|
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<void> disconnect() async {
|
||||||
|
if (_mailClient == null) return;
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Disconnecting from mail');
|
||||||
|
await _mailClient!.stopPolling();
|
||||||
|
await _mailClient!.disconnect();
|
||||||
|
Logger.log(LoggerType.info, Mailer, 'Disconnected from mail');
|
||||||
|
_mailClient = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class RowInput extends StatelessWidget {
|
class RowInput extends StatelessWidget {
|
||||||
const RowInput({Key? key, required this.label, required this.controller})
|
const RowInput(
|
||||||
|
{Key? key,
|
||||||
|
required this.label,
|
||||||
|
required this.controller,
|
||||||
|
this.obscureText = false})
|
||||||
: super(key: key);
|
: super(key: key);
|
||||||
|
|
||||||
final String label;
|
final String label;
|
||||||
final TextEditingController controller;
|
final TextEditingController controller;
|
||||||
|
final bool obscureText;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -15,6 +20,7 @@ class RowInput extends StatelessWidget {
|
||||||
child: Expanded(
|
child: Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: controller,
|
controller: controller,
|
||||||
|
obscureText: obscureText,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: label,
|
hintText: label,
|
||||||
),
|
),
|
||||||
|
|
44
lib/widgets/components/stream_element_card.dart
Normal file
44
lib/widgets/components/stream_element_card.dart
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import 'package:desktopapp/classes/stream_element.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class StreamElementCard extends StatelessWidget {
|
||||||
|
StreamElementCard({Key? key, required this.streamElement}) : super(key: key);
|
||||||
|
|
||||||
|
final StreamElement streamElement;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
IconData icon;
|
||||||
|
switch (streamElement.type) {
|
||||||
|
case StreamElementType.log:
|
||||||
|
icon = Icons.info;
|
||||||
|
break;
|
||||||
|
case StreamElementType.alert:
|
||||||
|
icon = Icons.notifications_active;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
icon,
|
||||||
|
size: 30,
|
||||||
|
),
|
||||||
|
SizedBox(width: 8),
|
||||||
|
Text(
|
||||||
|
streamElement.message,
|
||||||
|
style: TextStyle(fontSize: 16),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
33
lib/widgets/forms/email_form.dart
Normal file
33
lib/widgets/forms/email_form.dart
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import 'package:desktopapp/widgets/components/row_input.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
class EmailForm extends StatelessWidget {
|
||||||
|
EmailForm({Key? key, required this.onValid, required this.prefs})
|
||||||
|
: super(key: key);
|
||||||
|
|
||||||
|
final void Function(String, String) onValid;
|
||||||
|
final SharedPreferences prefs;
|
||||||
|
final TextEditingController _emailController = TextEditingController();
|
||||||
|
final TextEditingController _passwordController = TextEditingController();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
_emailController.text = prefs.getString('mail_email') ?? '';
|
||||||
|
_passwordController.text = prefs.getString('mail_password') ?? '';
|
||||||
|
return Column(children: [
|
||||||
|
RowInput(label: 'Email', controller: _emailController),
|
||||||
|
RowInput(
|
||||||
|
label: 'Password',
|
||||||
|
controller: _passwordController,
|
||||||
|
obscureText: true),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () async {
|
||||||
|
prefs.setString('mail_email', _emailController.text.trim());
|
||||||
|
prefs.setString('mail_password', _passwordController.text.trim());
|
||||||
|
onValid(_emailController.text, _passwordController.text);
|
||||||
|
},
|
||||||
|
child: const Text('Se connecter')),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,53 +1,91 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:desktopapp/classes/routes.dart';
|
import 'package:desktopapp/classes/routes.dart';
|
||||||
|
import 'package:desktopapp/classes/stream_element.dart';
|
||||||
import 'package:desktopapp/utils/mailer.dart';
|
import 'package:desktopapp/utils/mailer.dart';
|
||||||
import 'package:desktopapp/utils/requests.dart';
|
import 'package:desktopapp/widgets/components/stream_element_card.dart';
|
||||||
|
import 'package:desktopapp/widgets/forms/email_form.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class HomePage extends StatelessWidget {
|
class HomePage extends StatefulWidget {
|
||||||
const HomePage({Key? key}) : super(key: key);
|
const HomePage({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_HomePageState createState() => _HomePageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _HomePageState extends State<HomePage> {
|
||||||
|
bool connected = false;
|
||||||
|
List<StreamElement> streamElements = [];
|
||||||
|
final StreamController<StreamElement> controller =
|
||||||
|
StreamController<StreamElement>();
|
||||||
|
|
||||||
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
||||||
return Column(children: [
|
return Column(children: [
|
||||||
const Padding(
|
if (!connected)
|
||||||
padding: EdgeInsets.symmetric(vertical: 20),
|
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
||||||
child: Text('Appuyez sur le bouton pour envoyer une alerte de test',
|
EmailForm(
|
||||||
style: TextStyle(fontSize: 20)),
|
prefs: prefs,
|
||||||
),
|
onValid: (String email, String password) async {
|
||||||
TextButton(
|
var value = await Mailer.connect(
|
||||||
onPressed: () async {
|
email: email,
|
||||||
if (await Requests.sendAlert('*MrDev023* *Break* Test !')) {
|
password: password,
|
||||||
showDialog(
|
streamController: controller);
|
||||||
context: context,
|
setState(() {
|
||||||
builder: (_) => AlertDialog(
|
connected = value;
|
||||||
title: const Text('Info'),
|
});
|
||||||
content: const Text('Envoie réussie'),
|
})
|
||||||
actions: [
|
]),
|
||||||
TextButton(
|
if (connected)
|
||||||
onPressed: () {
|
Row(
|
||||||
Navigator.pop(context);
|
children: [
|
||||||
},
|
TextButton(
|
||||||
child: const Text('OK'))
|
onPressed: () async {
|
||||||
],
|
await Mailer.disconnect();
|
||||||
));
|
setState(() {
|
||||||
} else {
|
connected = false;
|
||||||
showDialog(
|
});
|
||||||
context: context,
|
},
|
||||||
builder: (_) => AlertDialog(
|
child: const Text('Se déconnecter')),
|
||||||
title: const Text('Erreur'),
|
],
|
||||||
content: const Text('Impossible de se connecter.'),
|
),
|
||||||
actions: [
|
Expanded(
|
||||||
TextButton(
|
child: StreamBuilder<StreamElement>(
|
||||||
onPressed: () => Navigator.pop(context),
|
stream: controller.stream,
|
||||||
child: const Text('OK'))
|
builder:
|
||||||
],
|
(BuildContext context, AsyncSnapshot<StreamElement> snapshot) {
|
||||||
));
|
switch (snapshot.connectionState) {
|
||||||
|
case ConnectionState.none:
|
||||||
|
return const Text('Nothing');
|
||||||
|
case ConnectionState.waiting:
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
case ConnectionState.active:
|
||||||
|
if (snapshot.hasData) {
|
||||||
|
int skipNumber = max(0, streamElements.length - 10);
|
||||||
|
bool alreadyAdded = streamElements
|
||||||
|
.skip(skipNumber)
|
||||||
|
.cast<StreamElement?>()
|
||||||
|
.firstWhere(
|
||||||
|
(element) => element!.uuid == snapshot.data!.uuid,
|
||||||
|
orElse: () => null) !=
|
||||||
|
null;
|
||||||
|
if (!alreadyAdded) streamElements.add(snapshot.data!);
|
||||||
|
}
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: streamElements.length,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
return StreamElementCard(
|
||||||
|
streamElement: streamElements[index]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
case ConnectionState.done:
|
||||||
|
return const Text('Terminate');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text('Envoyer une alerte de test')),
|
),
|
||||||
TextButton(
|
)
|
||||||
onPressed: () => Mailer.mailExample(),
|
|
||||||
child: const Text('Test email')),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,14 +106,7 @@ class HomePage extends StatelessWidget {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Column(
|
return buildContent(context, prefs);
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [buildContent(context, prefs)],
|
|
||||||
)
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
}
|
}
|
||||||
|
|
|
@ -511,6 +511,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.0"
|
version: "3.0.0"
|
||||||
|
uuid:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: uuid
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.6"
|
||||||
vector_math:
|
vector_math:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -39,7 +39,8 @@ dependencies:
|
||||||
shared_preferences: ^2.0.13
|
shared_preferences: ^2.0.13
|
||||||
flash: ^2.0.3+2
|
flash: ^2.0.3+2
|
||||||
dio: ^4.0.6
|
dio: ^4.0.6
|
||||||
enough_mail: ^1.3.4
|
enough_mail: ^1.3.6
|
||||||
|
uuid: ^3.0.6
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
Loading…
Reference in a new issue