From 2d8cb121b5e8dd5d17e805f2989988b2a5f88204 Mon Sep 17 00:00:00 2001 From: "Florian RICHER (MrDev023)" Date: Tue, 26 Apr 2022 22:51:48 +0200 Subject: [PATCH] Begin add settings --- lib/classes/routes.dart | 6 +- lib/utils/mailer.dart | 50 ++++++++++---- lib/widgets/pages/home.dart | 9 ++- lib/widgets/pages/settings.dart | 47 +++++++++++++ .../pages/settings/messages_settings.dart | 66 +++++++++++++++++++ .../pages/settings/whitelist_settings.dart | 66 +++++++++++++++++++ 6 files changed, 229 insertions(+), 15 deletions(-) create mode 100644 lib/widgets/pages/settings.dart create mode 100644 lib/widgets/pages/settings/messages_settings.dart create mode 100644 lib/widgets/pages/settings/whitelist_settings.dart diff --git a/lib/classes/routes.dart b/lib/classes/routes.dart index b6e17ec..89a4ff4 100644 --- a/lib/classes/routes.dart +++ b/lib/classes/routes.dart @@ -1,8 +1,9 @@ import 'package:desktopapp/widgets/pages/home.dart'; +import 'package:desktopapp/widgets/pages/settings.dart'; import 'package:desktopapp/widgets/pages/tutorial.dart'; import 'package:flutter/material.dart'; -enum Routes { streamlabsTutorial, home } +enum Routes { streamlabsTutorial, home, mailSettings } extension RoutesExtension on Routes { String get path { @@ -11,6 +12,8 @@ extension RoutesExtension on Routes { return '/streamlabs/tutorial'; case Routes.home: return '/home'; + case Routes.mailSettings: + return '/mail/settings'; } } } @@ -18,6 +21,7 @@ extension RoutesExtension on Routes { var routes = { Routes.streamlabsTutorial.path: (context) => TutorialPage(), Routes.home.path: (context) => const HomePage(), + Routes.mailSettings.path: (context) => const Settings(), }; var initialRoute = Routes.home.path; diff --git a/lib/utils/mailer.dart b/lib/utils/mailer.dart index 8ba20e8..743e1b9 100644 --- a/lib/utils/mailer.dart +++ b/lib/utils/mailer.dart @@ -7,13 +7,34 @@ import 'package:desktopapp/utils/requests.dart'; import 'package:desktopapp/widgets/popups/mailbox_select.dart'; import 'package:enough_mail/enough_mail.dart'; import 'package:flutter/cupertino.dart'; +import 'package:shared_preferences/shared_preferences.dart'; -class Mailer { - static final List customMessages = [ +class MailerSettings { + static final List defaultMessages = [ 'Un gueux vient de commander de la HOLY', 'Un nouveau gueux vient de commander de la HOLY' ]; - static final List whiteListEmail = ['louis@holyenergy.fr']; + static final List defaultWhitelist = ['louis@holyenergy.fr']; + + const MailerSettings._({required this.messages, required this.whitelist}); + + static MailerSettings loadSettings(SharedPreferences prefs) { + var messages = prefs.getStringList('mailer_messages') ?? defaultMessages; + var whitelist = prefs.getStringList('mailer_whitelist') ?? defaultWhitelist; + + return MailerSettings._(messages: messages, whitelist: whitelist); + } + + final List messages; + final List whitelist; + + Future saveSettings(SharedPreferences prefs) async { + await prefs.setStringList('mailer_messages', messages); + await prefs.setStringList('mailer_whitelist', whitelist); + } +} + +class Mailer { static MailClient? _mailClient; static Timer? _timer; @@ -45,11 +66,15 @@ class Mailer { await _mailClient!.selectMailbox(mailbox!); streamController.add(StreamElement( StreamElementType.log, '${mailbox.name} selected for $email')); - List whitelist = [email, ...whiteListEmail]; - streamController.add(StreamElement( - StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}')); + + var mailerSettings = + MailerSettings.loadSettings(await SharedPreferences.getInstance()); + List whitelist = [email, ...mailerSettings.whitelist]; + streamController.add(StreamElement(StreamElementType.log, + "Listen for emails: ${whitelist.join(', ')}\nMessages : ${mailerSettings.messages.join(', ')}")); _mailClient!.eventBus.on().listen((ImapEvent event) async { - await _onMessage(event, streamController, email, whitelist); + await _onMessage( + event, streamController, email, mailerSettings, whitelist); }); await _mailClient!.startPolling(const Duration(seconds: 1)); _startDebugProfiler(streamController); @@ -79,6 +104,7 @@ class Mailer { ImapEvent event, StreamController streamController, String email, + MailerSettings mailerSettings, List whitelist) async { if (event.eventType == ImapEventType.exists) { final evt = event as ImapMessagesExistEvent; @@ -99,15 +125,15 @@ class Mailer { if (mail == null) continue; var mailEvt = MailLoadEvent(message, _mailClient!); - _onMailEvent(mailEvt, streamController); + _onMailEvent(mailEvt, streamController, mailerSettings); } } } - static Future _onMailEvent( - MailLoadEvent mailLoadEvent, StreamController streamController) async { - final message = - customMessages[Random.secure().nextInt(customMessages.length)]; + static Future _onMailEvent(MailLoadEvent mailLoadEvent, + StreamController streamController, MailerSettings mailerSettings) async { + final message = mailerSettings + .messages[Random.secure().nextInt(mailerSettings.messages.length)]; await Requests.sendAlert(message, type: AlertType.host); streamController.add(StreamElement(StreamElementType.alert, message)); } diff --git a/lib/widgets/pages/home.dart b/lib/widgets/pages/home.dart index 202475c..bed64ce 100644 --- a/lib/widgets/pages/home.dart +++ b/lib/widgets/pages/home.dart @@ -27,7 +27,7 @@ class _HomePageState extends State { Widget buildContent(BuildContext context, SharedPreferences prefs) { return Column(children: [ if (!connected) - Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ EmailForm( prefs: prefs, onValid: (String email, String password) async { @@ -39,7 +39,12 @@ class _HomePageState extends State { setState(() { connected = value; }); - }) + }), + TextButton( + onPressed: () async { + Navigator.of(context).pushNamed(Routes.mailSettings.path); + }, + child: const Text('Paramètres')), ]), if (connected) Row( diff --git a/lib/widgets/pages/settings.dart b/lib/widgets/pages/settings.dart new file mode 100644 index 0000000..25c4470 --- /dev/null +++ b/lib/widgets/pages/settings.dart @@ -0,0 +1,47 @@ +import 'package:desktopapp/utils/mailer.dart'; +import 'package:desktopapp/widgets/pages/settings/messages_settings.dart'; +import 'package:desktopapp/widgets/pages/settings/whitelist_settings.dart'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class Settings extends StatefulWidget { + const Settings({Key? key}) : super(key: key); + + @override + _SettingsState createState() => _SettingsState(); +} + +class _SettingsState extends State { + MailerSettings? mailerSettings; + Widget buildContent(BuildContext context, SharedPreferences prefs) { + return Padding( + padding: const EdgeInsets.all(16.0), + child: GridView( + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 2, mainAxisSpacing: 8.0, crossAxisSpacing: 8.0), + children: [ + MessagesSettings(settings: mailerSettings!, prefs: prefs), + WhitelistSettings(settings: mailerSettings!, prefs: prefs) + ], + ), + ); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(title: const Text('Settings')), + body: FutureBuilder( + future: SharedPreferences.getInstance(), + builder: (BuildContext context, + AsyncSnapshot snapshot) { + if (snapshot.hasData) { + final prefs = snapshot.data!; + mailerSettings = MailerSettings.loadSettings(prefs); + return buildContent(context, prefs); + } else { + return const Center(child: CircularProgressIndicator()); + } + })); + } +} diff --git a/lib/widgets/pages/settings/messages_settings.dart b/lib/widgets/pages/settings/messages_settings.dart new file mode 100644 index 0000000..87f6bb5 --- /dev/null +++ b/lib/widgets/pages/settings/messages_settings.dart @@ -0,0 +1,66 @@ +import 'package:desktopapp/utils/mailer.dart'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class MessagesSettings extends StatefulWidget { + const MessagesSettings( + {Key? key, required this.settings, required this.prefs}) + : super(key: key); + + final MailerSettings settings; + final SharedPreferences prefs; + + @override + _MessagesSettingsState createState() => _MessagesSettingsState(); +} + +class _MessagesSettingsState extends State { + final TextEditingController controller = TextEditingController(); + + @override + Widget build(BuildContext context) { + return Card( + child: Column( + children: [ + const Text('Messages :', style: TextStyle(fontSize: 24)), + Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + children: [ + Expanded(child: TextField(controller: controller)), + TextButton( + child: const Text('Ajouter'), + onPressed: () { + setState(() { + widget.settings.messages.add(controller.text); + controller.clear(); + widget.settings.saveSettings(widget.prefs); + }); + }, + ) + ], + ), + ), + Expanded( + child: ListView.builder( + itemCount: widget.settings.messages.length, + itemBuilder: (BuildContext context, int index) { + return ListTile( + title: Text(widget.settings.messages[index]), + leading: IconButton( + icon: const Icon(Icons.delete_rounded), + onPressed: () { + setState(() { + widget.settings.messages.removeAt(index); + widget.settings.saveSettings(widget.prefs); + }); + }, + ), + ); + }), + ) + ], + ), + ); + } +} diff --git a/lib/widgets/pages/settings/whitelist_settings.dart b/lib/widgets/pages/settings/whitelist_settings.dart new file mode 100644 index 0000000..8eec512 --- /dev/null +++ b/lib/widgets/pages/settings/whitelist_settings.dart @@ -0,0 +1,66 @@ +import 'package:desktopapp/utils/mailer.dart'; +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class WhitelistSettings extends StatefulWidget { + const WhitelistSettings( + {Key? key, required this.settings, required this.prefs}) + : super(key: key); + + final MailerSettings settings; + final SharedPreferences prefs; + + @override + _WhitelistSettingsState createState() => _WhitelistSettingsState(); +} + +class _WhitelistSettingsState extends State { + final TextEditingController controller = TextEditingController(); + + @override + Widget build(BuildContext context) { + return Card( + child: Column( + children: [ + const Text('Whitelist :', style: TextStyle(fontSize: 24)), + Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + children: [ + Expanded(child: TextField(controller: controller)), + TextButton( + child: const Text('Ajouter'), + onPressed: () { + setState(() { + widget.settings.whitelist.add(controller.text); + controller.clear(); + widget.settings.saveSettings(widget.prefs); + }); + }, + ) + ], + ), + ), + Expanded( + child: ListView.builder( + itemCount: widget.settings.whitelist.length, + itemBuilder: (BuildContext context, int index) { + return ListTile( + title: Text(widget.settings.whitelist[index]), + leading: IconButton( + icon: const Icon(Icons.delete_rounded), + onPressed: () { + setState(() { + widget.settings.whitelist.removeAt(index); + widget.settings.saveSettings(widget.prefs); + }); + }, + ), + ); + }), + ) + ], + ), + ); + } +}