From fe32209e30c0129f6aca20459a8a6984ecb81816 Mon Sep 17 00:00:00 2001 From: Paulis Gributs Date: Wed, 1 Mar 2023 14:09:45 +0000 Subject: [PATCH] Add local notification when closing Instead of showing pop-up dialogue --- lib/main.dart | 5 ++++ lib/ui/window_decorations.dart | 25 +++++-------------- pubspec.lock | 8 ++++++ pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 +++ windows/flutter/generated_plugins.cmake | 1 + 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 287d33f..3cc9825 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -7,6 +7,7 @@ import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/services.dart'; import 'package:flutter_foreground_task/flutter_foreground_task.dart'; import 'package:intl/intl.dart'; +import 'package:local_notifier/local_notifier.dart'; import 'package:negate/logger/android_logger.dart'; import 'package:negate/logger/logger_factory.dart'; import 'package:negate/sentiment_analysis.dart'; @@ -85,6 +86,10 @@ Future main() async { // as desktop hooks require their own constantly running thread // otherwise the hooks will not be called await loggerUI.initSystemTray(); + await localNotifier.setup( + appName: 'Negate', + shortcutPolicy: ShortcutPolicy.requireCreate, + ); await Isolate.spawn(LoggerFactory.startLoggerFactory, TfliteRequest(rPort.sendPort, dbString, tfp, prefs)); } diff --git a/lib/ui/window_decorations.dart b/lib/ui/window_decorations.dart index 096de61..1e89686 100644 --- a/lib/ui/window_decorations.dart +++ b/lib/ui/window_decorations.dart @@ -1,5 +1,6 @@ import 'package:bitsdojo_window/bitsdojo_window.dart'; import 'package:flutter/material.dart'; +import 'package:local_notifier/local_notifier.dart'; class WindowButtons extends StatelessWidget { WindowButtons({Key? key}) : super(key: key); @@ -29,26 +30,12 @@ class WindowButtons extends StatelessWidget { CloseWindowButton( colors: closeButtonColors, onPressed: () { - showDialog( - context: context, - barrierDismissible: false, - builder: (BuildContext context) { - return AlertDialog( - title: const Text('Exit Program?'), - content: const Text( - ('The window will be hidden, to exit the program you can use the system menu.')), - actions: [ - TextButton( - child: const Text('OK'), - onPressed: () { - Navigator.of(context).pop(); - appWindow.hide(); - }, - ), - ], - ); - }, + appWindow.hide(); + LocalNotification notification = LocalNotification( + title: "Negate", + body: "running in the background", ); + notification.show(); }, ), ], diff --git a/pubspec.lock b/pubspec.lock index 050db4f..a407e21 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -544,6 +544,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + local_notifier: + dependency: "direct main" + description: + name: local_notifier + sha256: cc855aa6362c8840e3d3b35b1c3b058a3a8becdb2b03d5a9aa3f3a1e861f0a03 + url: "https://pub.dev" + source: hosted + version: "0.1.5" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e714983..908c9f6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -66,6 +66,7 @@ dependencies: file_picker: ^5.2.5 google_mlkit_language_id: ^0.5.0 google_mlkit_translation: ^0.5.0 + local_notifier: ^0.1.5 dev_dependencies: drift_dev: ^2.4.0 diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index b636ad0..e501bcc 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { registry->GetRegistrarForPlugin("DynamicColorPluginCApi")); FileSaverPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("FileSaverPlugin")); + LocalNotifierPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("LocalNotifierPlugin")); PermissionHandlerWindowsPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin")); Sqlite3FlutterLibsPluginRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 7e0ee7f..0f1c607 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST bitsdojo_window_windows dynamic_color file_saver + local_notifier permission_handler_windows sqlite3_flutter_libs system_tray