diff --git a/lib/utils/mailer.dart b/lib/utils/mailer.dart index 0f2c8ce..8ba20e8 100644 --- a/lib/utils/mailer.dart +++ b/lib/utils/mailer.dart @@ -4,7 +4,9 @@ import 'dart:math'; import 'package:desktopapp/classes/stream_element.dart'; import 'package:desktopapp/utils/logger.dart'; 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'; class Mailer { static final List customMessages = [ @@ -17,6 +19,7 @@ class Mailer { /// High level mail API example static Future connect({ + required BuildContext context, required String email, required String password, required StreamController streamController, @@ -37,9 +40,11 @@ class Mailer { Logger.log(LoggerType.info, Mailer, 'Connected to $email'); streamController .add(StreamElement(StreamElementType.log, 'Connected to $email')); - await _mailClient!.selectInbox(); - streamController.add( - StreamElement(StreamElementType.log, 'INBOX selected for $email')); + var mailboxes = await _mailClient!.listMailboxes(); + var mailbox = await MailBoxSelect.show(context, mailboxes: mailboxes); + 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(', ')}')); @@ -49,9 +54,9 @@ class Mailer { await _mailClient!.startPolling(const Duration(seconds: 1)); _startDebugProfiler(streamController); return true; - } on MailException catch (e) { - streamController.add(StreamElement(StreamElementType.log, e.toString())); - Logger.log(LoggerType.error, Mailer, 'High level API failed with $e'); + } catch (e) { + streamController + .add(StreamElement(StreamElementType.error, e.toString())); return false; } } diff --git a/lib/widgets/pages/home.dart b/lib/widgets/pages/home.dart index 4cfc12b..d228c14 100644 --- a/lib/widgets/pages/home.dart +++ b/lib/widgets/pages/home.dart @@ -22,6 +22,8 @@ class _HomePageState extends State { final StreamController controller = StreamController(); + final ScrollController _scrollController = ScrollController(); + Widget buildContent(BuildContext context, SharedPreferences prefs) { return Column(children: [ if (!connected) @@ -30,6 +32,7 @@ class _HomePageState extends State { prefs: prefs, onValid: (String email, String password) async { var value = await Mailer.connect( + context: context, email: email, password: password, streamController: controller); @@ -75,7 +78,10 @@ class _HomePageState extends State { } return ListView.builder( itemCount: streamElements.length, + controller: _scrollController, itemBuilder: (BuildContext context, int index) { + _scrollController + .jumpTo(_scrollController.position.maxScrollExtent); return StreamElementCard( streamElement: streamElements[index]); }, diff --git a/lib/widgets/popups/mailbox_select.dart b/lib/widgets/popups/mailbox_select.dart new file mode 100644 index 0000000..4f48e87 --- /dev/null +++ b/lib/widgets/popups/mailbox_select.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; +import 'package:enough_mail/enough_mail.dart'; + +class MailBoxSelect extends StatefulWidget { + static Future show(BuildContext context, + {required List mailboxes}) async { + return await showDialog( + context: context, + builder: (context) => MailBoxSelect(mailBoxes: mailboxes), + ); + } + + const MailBoxSelect({Key? key, required this.mailBoxes}) : super(key: key); + + final List mailBoxes; + + @override + _MailBoxSelectState createState() => _MailBoxSelectState(); +} + +class _MailBoxSelectState extends State { + Mailbox? _selectedMailbox; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Boîte de réception'), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.spaceAround, + children: [ + DropdownButton( + value: _selectedMailbox, + items: widget.mailBoxes.map((Mailbox mailBox) { + return DropdownMenuItem( + value: mailBox, + child: Text(mailBox.name), + ); + }).toList(), + onChanged: (Mailbox? value) { + if (value != null) { + setState(() => _selectedMailbox = value); + } + }), + TextButton( + onPressed: _selectedMailbox == null + ? null + : () { + Navigator.pop(context, _selectedMailbox!); + }, + child: const Text('Valider')) + ], + ), + ), + ); + } +}