From 531595e9e9bd2af0e817ce2a55876abe70539720 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 20 Jun 2022 16:10:46 -0700 Subject: [PATCH] remove notification debugging and linux notifier --- lib/notification_manager.dart | 111 ++++++++-------------------------- 1 file changed, 24 insertions(+), 87 deletions(-) diff --git a/lib/notification_manager.dart b/lib/notification_manager.dart index 10ab6ee4..2761d457 100644 --- a/lib/notification_manager.dart +++ b/lib/notification_manager.dart @@ -57,22 +57,33 @@ class WindowsNotificationManager implements NotificationsManager { } } -// LinuxNotificationsManager uses the desktop_notifications package to implement -// the standard dbus-powered linux desktop notifications. -class LinuxNotificationsManager implements NotificationsManager { - int previous_id = 0; - late linux_notifications.NotificationsClient client; - late Future Function(String, int) notificationSelectConvo; - late String assetsPath; +class NotificationPayload { + late String profileOnion; + late int convoId; - LinuxNotificationsManager(Future Function(String, int) notificationSelectConvo) { - this.client = linux_notifications.NotificationsClient(); - this.notificationSelectConvo = notificationSelectConvo; - scheduleMicrotask(() async { - assetsPath = await detectLinuxAssetsPath(); - }); + NotificationPayload(String po, int cid) { + profileOnion = po; + convoId = cid; } + NotificationPayload.fromJson(Map json) + : profileOnion = json['profileOnion'], + convoId = json['convoId']; + + Map toJson() => { + 'profileOnion': profileOnion, + 'convoId': convoId, + }; +} + +// FlutterLocalNotificationsPlugin based NotificationManager that handles MacOS and Linux +// TODO: Windows support is being worked on, check back and migrate to that too when it lands +class NixNotificationManager implements NotificationsManager { + late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; + late Future Function(String, int) notificationSelectConvo; + late String linuxAssetsPath; + + // Cwtch can install in non flutter supported ways on linux, this code detects where the assets are on Linux Future detectLinuxAssetsPath() async { var devStat = FileStat.stat("assets"); @@ -92,116 +103,42 @@ class LinuxNotificationsManager implements NotificationsManager { return ""; } - Future notify(String message, String profile, int conversationId) async { - var iconPath = Uri.file(path.join(assetsPath, "assets/knott.png")); - client.notify(message, appName: "cwtch", appIcon: iconPath.toString(), replacesId: this.previous_id).then((linux_notifications.Notification value) async { - previous_id = value.id; - if ((await value.closeReason) == linux_notifications.NotificationClosedReason.dismissed) { - this.notificationSelectConvo(profile, conversationId); - } - }); - } -} - -class NotificationPayload { - late String profileOnion; - late int convoId; - - NotificationPayload(String po, int cid) { - profileOnion = po; - convoId = cid; - } - - NotificationPayload.fromJson(Map json) - : profileOnion = json['profileOnion'], - convoId = json['convoId']; - - Map toJson() => { - 'profileOnion': profileOnion, - 'convoId': convoId, - }; -} - -// FlutterLocalNotificationsPlugin based NotificationManager that handles MacOS -// Todo: work with author to allow settings of asset_path so we can use this for Linux and deprecate the LinuxNotificationManager -// Todo: it can also handle Android, do we want to migrate away from our manual solution? -class NixNotificationManager implements NotificationsManager { - late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; - late Future Function(String, int) notificationSelectConvo; - late String linuxAssetsPath; - - - // Cwtch can install in non flutter supported ways on linux, this code detects where the assets are on Linux - Future detectLinuxAssetsPath() async { - //var devStat = FileStat.stat("assets"); - //var localStat = FileStat.stat("data/flutter_assets"); - var homeStat = FileStat.stat((Platform.environment["HOME"] ?? "") + "/.local/share/cwtch/data/flutter_assets"); - var rootStat = FileStat.stat("/usr/share/cwtch/data/flutter_assets"); - - /*if ((await devStat).type == FileSystemEntityType.directory) { - return Directory.current.path; //appPath; - } else if ((await localStat).type == FileSystemEntityType.directory) { - return path.join(Directory.current.path, "data/flutter_assets/"); - } else */if ((await homeStat).type == FileSystemEntityType.directory) { - return (Platform.environment["HOME"] ?? "") + "/.local/share/cwtch/data/flutter_assets/"; - } else if ((await rootStat).type == FileSystemEntityType.directory) { - return "/usr/share/cwtch/data/flutter_assets/"; - } - return ""; - } - NixNotificationManager(Future Function(String, int) notificationSelectConvo) { this.notificationSelectConvo = notificationSelectConvo; flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); - - scheduleMicrotask(() async { if (Platform.isLinux) { linuxAssetsPath = await detectLinuxAssetsPath(); - print("NixNotificationManager found LinuxAssetsPath!: $linuxAssetsPath"); } else { linuxAssetsPath = ""; } final MacOSInitializationSettings initializationSettingsMacOS = MacOSInitializationSettings(defaultPresentSound: false); var linuxIcon = FilePathLinuxIcon( path.join(linuxAssetsPath, 'assets/knott.png')); - print("NixNotificationManager make linux settings"); final LinuxInitializationSettings initializationSettingsLinux = LinuxInitializationSettings(defaultActionName: 'Open notification', defaultIcon: linuxIcon, defaultSuppressSound: true); - print("NixNotificationManager InitializationSettings"); - final InitializationSettings initializationSettings = InitializationSettings(android: null, iOS: null, macOS: initializationSettingsMacOS, linux: initializationSettingsLinux); - print("NixNotificationManager mac req perms"); - flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation()?.requestPermissions( alert: true, badge: false, sound: false, ); - print("NixNotificationManager initialize..."); await flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: selectNotification); - print("NixNotificationManager initialized!!!"); }); } Future notify(String message, String profile, int conversationId) async { - print("notify if !globalAppState.focus so do? ${!globalAppState.focus}"); if (!globalAppState.focus) { - - print("do notify!"); - // Warning: Only use title field on Linux, body field will render links as clickable await flutterLocalNotificationsPlugin.show(0, message, '', NotificationDetails(linux: LinuxNotificationDetails(suppressSound: true, category: LinuxNotificationCategory.imReceived(), icon: FilePathLinuxIcon(path.join(linuxAssetsPath, 'assets/knott.png')))), payload: jsonEncode(NotificationPayload(profile, conversationId))); - - print("done notify"); } }