add desktoasts windows notifications
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Dan Ballard 2022-01-24 16:03:46 -08:00
parent 598251a624
commit c838176e3b
6 changed files with 58 additions and 9 deletions

View File

@ -70,7 +70,7 @@ class FlwtchState extends State<Flwtch> {
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(), globalAppState, globalServersList);
cwtch = CwtchFfi(cwtchNotifier);
} else {
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, NullNotificationsManager(), globalAppState, globalServersList);
var cwtchNotifier = new CwtchNotifier(profs, globalSettings, globalErrorHandler, globalTorStatus, newDesktopNotificationsManager(), globalAppState, globalServersList);
cwtch = CwtchFfi(cwtchNotifier);
}
print("initState: invoking cwtch.Start()");

View File

@ -1,6 +1,11 @@
import 'dart:io';
import 'package:desktoasts/desktoasts.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:path/path.dart' as path;
import 'config.dart';
// NotificationsManager provides a wrapper around platform specific notifications logic.
abstract class NotificationsManager {
Future<void> notify(String message);
@ -26,15 +31,47 @@ class LinuxNotificationsManager implements NotificationsManager {
}
}
// Windows Notification Manager uses https://pub.dev/packages/desktoasts to implement
// windows notifications
class WindowsNotificationManager implements NotificationsManager {
late ToastService service;
WindowsNotificationManager() {
service = new ToastService(
appName: 'Cwtch',
companyName: 'Open Privacy Research Society',
productName: 'Cwtch',
);
}
Future<void> notify(String message) async {
Toast toast = new Toast(
type: ToastType.text01,
title: 'Cwtch',
subtitle: message,
);
service.show(toast);
}
}
NotificationsManager newDesktopNotificationsManager() {
try {
// Test that we can actually access DBUS. Otherwise return a null
// notifications manager...
NotificationsClient client = NotificationsClient();
client.getCapabilities();
return LinuxNotificationsManager(client);
} catch (e) {
print("Attempted to access DBUS for notifications but failed. Switching off notifications.");
if (Platform.isLinux) {
try {
// Test that we can actually access DBUS. Otherwise return a null
// notifications manager...
NotificationsClient client = NotificationsClient();
client.getCapabilities();
return LinuxNotificationsManager(client);
} catch (e) {
EnvironmentConfig.debugLog(
"Attempted to access DBUS for notifications but failed. Switching off notifications.");
}
} else if (Platform.isWindows) {
try {
return WindowsNotificationManager();
} catch (e) {
EnvironmentConfig.debugLog("Failed to create Windows desktoasts notification manager");
}
}
return NullNotificationsManager();
}

View File

@ -78,6 +78,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.2"
desktoasts:
dependency: "direct main"
description:
name: desktoasts
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.2"
desktop_notifications:
dependency: "direct main"
description:

View File

@ -44,6 +44,7 @@ dependencies:
file_picker: ^4.3.2
file_picker_desktop: ^1.1.0
url_launcher: ^6.0.12
desktoasts: ^0.0.2
dev_dependencies:
msix: ^2.1.3

View File

@ -6,9 +6,12 @@
#include "generated_plugin_registrant.h"
#include <desktoasts/desktoasts_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
DesktoastsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("DesktoastsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}

View File

@ -3,6 +3,7 @@
#
list(APPEND FLUTTER_PLUGIN_LIST
desktoasts
url_launcher_windows
)