1
0
Fork 0

Begin add settings

This commit is contained in:
Florian RICHER (MrDev023) 2022-04-26 22:51:48 +02:00
parent 96a8f41525
commit 2d8cb121b5
6 changed files with 229 additions and 15 deletions

View file

@ -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 = <String, WidgetBuilder>{
Routes.streamlabsTutorial.path: (context) => TutorialPage(),
Routes.home.path: (context) => const HomePage(),
Routes.mailSettings.path: (context) => const Settings(),
};
var initialRoute = Routes.home.path;

View file

@ -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<String> customMessages = [
class MailerSettings {
static final List<String> defaultMessages = [
'Un gueux vient de commander de la HOLY',
'Un nouveau gueux vient de commander de la HOLY'
];
static final List<String> whiteListEmail = ['louis@holyenergy.fr'];
static final List<String> 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<String> messages;
final List<String> whitelist;
Future<void> 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<String> whitelist = [email, ...whiteListEmail];
streamController.add(StreamElement(
StreamElementType.log, 'Listen for emails: ${whitelist.join(', ')}'));
var mailerSettings =
MailerSettings.loadSettings(await SharedPreferences.getInstance());
List<String> whitelist = [email, ...mailerSettings.whitelist];
streamController.add(StreamElement(StreamElementType.log,
"Listen for emails: ${whitelist.join(', ')}\nMessages : ${mailerSettings.messages.join(', ')}"));
_mailClient!.eventBus.on<ImapEvent>().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<String> 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<void> _onMailEvent(
MailLoadEvent mailLoadEvent, StreamController streamController) async {
final message =
customMessages[Random.secure().nextInt(customMessages.length)];
static Future<void> _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));
}

View file

@ -27,7 +27,7 @@ class _HomePageState extends State<HomePage> {
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<HomePage> {
setState(() {
connected = value;
});
})
}),
TextButton(
onPressed: () async {
Navigator.of(context).pushNamed(Routes.mailSettings.path);
},
child: const Text('Paramètres')),
]),
if (connected)
Row(

View file

@ -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<Settings> {
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<SharedPreferences> snapshot) {
if (snapshot.hasData) {
final prefs = snapshot.data!;
mailerSettings = MailerSettings.loadSettings(prefs);
return buildContent(context, prefs);
} else {
return const Center(child: CircularProgressIndicator());
}
}));
}
}

View file

@ -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<MessagesSettings> {
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);
});
},
),
);
}),
)
],
),
);
}
}

View file

@ -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<WhitelistSettings> {
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);
});
},
),
);
}),
)
],
),
);
}
}