Add mailbox Select
This commit is contained in:
parent
ef3379f1d9
commit
8372769fbb
3 changed files with 76 additions and 6 deletions
|
@ -4,7 +4,9 @@ import 'dart:math';
|
||||||
import 'package:desktopapp/classes/stream_element.dart';
|
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:desktopapp/utils/requests.dart';
|
||||||
|
import 'package:desktopapp/widgets/popups/mailbox_select.dart';
|
||||||
import 'package:enough_mail/enough_mail.dart';
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
|
||||||
class Mailer {
|
class Mailer {
|
||||||
static final List<String> customMessages = [
|
static final List<String> customMessages = [
|
||||||
|
@ -17,6 +19,7 @@ class Mailer {
|
||||||
|
|
||||||
/// High level mail API example
|
/// High level mail API example
|
||||||
static Future<bool> connect({
|
static Future<bool> connect({
|
||||||
|
required BuildContext context,
|
||||||
required String email,
|
required String email,
|
||||||
required String password,
|
required String password,
|
||||||
required StreamController<StreamElement> streamController,
|
required StreamController<StreamElement> streamController,
|
||||||
|
@ -37,9 +40,11 @@ class Mailer {
|
||||||
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
|
Logger.log(LoggerType.info, Mailer, 'Connected to $email');
|
||||||
streamController
|
streamController
|
||||||
.add(StreamElement(StreamElementType.log, 'Connected to $email'));
|
.add(StreamElement(StreamElementType.log, 'Connected to $email'));
|
||||||
await _mailClient!.selectInbox();
|
var mailboxes = await _mailClient!.listMailboxes();
|
||||||
streamController.add(
|
var mailbox = await MailBoxSelect.show(context, mailboxes: mailboxes);
|
||||||
StreamElement(StreamElementType.log, 'INBOX selected for $email'));
|
await _mailClient!.selectMailbox(mailbox!);
|
||||||
|
streamController.add(StreamElement(
|
||||||
|
StreamElementType.log, '${mailbox.name} selected for $email'));
|
||||||
List<String> whitelist = [email, ...whiteListEmail];
|
List<String> whitelist = [email, ...whiteListEmail];
|
||||||
streamController.add(StreamElement(
|
streamController.add(StreamElement(
|
||||||
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
|
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
|
||||||
|
@ -49,9 +54,9 @@ class Mailer {
|
||||||
await _mailClient!.startPolling(const Duration(seconds: 1));
|
await _mailClient!.startPolling(const Duration(seconds: 1));
|
||||||
_startDebugProfiler(streamController);
|
_startDebugProfiler(streamController);
|
||||||
return true;
|
return true;
|
||||||
} on MailException catch (e) {
|
} catch (e) {
|
||||||
streamController.add(StreamElement(StreamElementType.log, e.toString()));
|
streamController
|
||||||
Logger.log(LoggerType.error, Mailer, 'High level API failed with $e');
|
.add(StreamElement(StreamElementType.error, e.toString()));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ class _HomePageState extends State<HomePage> {
|
||||||
final StreamController<StreamElement> controller =
|
final StreamController<StreamElement> controller =
|
||||||
StreamController<StreamElement>();
|
StreamController<StreamElement>();
|
||||||
|
|
||||||
|
final ScrollController _scrollController = ScrollController();
|
||||||
|
|
||||||
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
Widget buildContent(BuildContext context, SharedPreferences prefs) {
|
||||||
return Column(children: [
|
return Column(children: [
|
||||||
if (!connected)
|
if (!connected)
|
||||||
|
@ -30,6 +32,7 @@ class _HomePageState extends State<HomePage> {
|
||||||
prefs: prefs,
|
prefs: prefs,
|
||||||
onValid: (String email, String password) async {
|
onValid: (String email, String password) async {
|
||||||
var value = await Mailer.connect(
|
var value = await Mailer.connect(
|
||||||
|
context: context,
|
||||||
email: email,
|
email: email,
|
||||||
password: password,
|
password: password,
|
||||||
streamController: controller);
|
streamController: controller);
|
||||||
|
@ -75,7 +78,10 @@ class _HomePageState extends State<HomePage> {
|
||||||
}
|
}
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemCount: streamElements.length,
|
itemCount: streamElements.length,
|
||||||
|
controller: _scrollController,
|
||||||
itemBuilder: (BuildContext context, int index) {
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
_scrollController
|
||||||
|
.jumpTo(_scrollController.position.maxScrollExtent);
|
||||||
return StreamElementCard(
|
return StreamElementCard(
|
||||||
streamElement: streamElements[index]);
|
streamElement: streamElements[index]);
|
||||||
},
|
},
|
||||||
|
|
59
lib/widgets/popups/mailbox_select.dart
Normal file
59
lib/widgets/popups/mailbox_select.dart
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
|
|
||||||
|
class MailBoxSelect extends StatefulWidget {
|
||||||
|
static Future<Mailbox?> show(BuildContext context,
|
||||||
|
{required List<Mailbox> mailboxes}) async {
|
||||||
|
return await showDialog<Mailbox>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => MailBoxSelect(mailBoxes: mailboxes),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const MailBoxSelect({Key? key, required this.mailBoxes}) : super(key: key);
|
||||||
|
|
||||||
|
final List<Mailbox> mailBoxes;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_MailBoxSelectState createState() => _MailBoxSelectState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MailBoxSelectState extends State<MailBoxSelect> {
|
||||||
|
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<Mailbox>(
|
||||||
|
value: _selectedMailbox,
|
||||||
|
items: widget.mailBoxes.map((Mailbox mailBox) {
|
||||||
|
return DropdownMenuItem<Mailbox>(
|
||||||
|
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'))
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue