From f40c3fdff9b7733a05c252aa45e7c4d1e6645056 Mon Sep 17 00:00:00 2001 From: Cranyozen <61766249+cranyozen@users.noreply.github.com> Date: Sun, 31 Mar 2024 14:38:26 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BB=BF=F0=9F=92=BE=20Feat:=20Simple=20notifi?= =?UTF-8?q?cation=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kitx_mobile/lib/services/devices_service.dart | 6 ++ kitx_mobile/lib/services/notification.dart | 57 +++++++++++++++---- kitx_mobile/lib/utils/translation/en_us.dart | 1 + kitx_mobile/lib/utils/translation/zh_cn.dart | 1 + kitx_mobile/pubspec.lock | 8 +++ 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/kitx_mobile/lib/services/devices_service.dart b/kitx_mobile/lib/services/devices_service.dart index 245ddb2..41699a2 100644 --- a/kitx_mobile/lib/services/devices_service.dart +++ b/kitx_mobile/lib/services/devices_service.dart @@ -139,6 +139,12 @@ class DeviceService implements Service { } deviceInfoList.refresh(); + + // Update notification service. + instances.notificationService.updateStatusNotification( + deviceCount: deviceInfoList.length, + serviceStatus: serviceStatus.value, + ); }); serviceStatus.value = ServiceStatus.running; diff --git a/kitx_mobile/lib/services/notification.dart b/kitx_mobile/lib/services/notification.dart index cabab19..fd599fd 100644 --- a/kitx_mobile/lib/services/notification.dart +++ b/kitx_mobile/lib/services/notification.dart @@ -1,7 +1,9 @@ import 'package:awesome_notifications/awesome_notifications.dart'; +import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:kitx_mobile/services/public/service_status.dart'; import 'package:kitx_mobile/utils/extensions/service_status_to_string.dart'; +import 'package:kitx_mobile/instances.dart'; // import 'package:flutter/material.dart'; /// [NotificationService] class @@ -23,12 +25,34 @@ class NotificationService { channelKey: statusChannelKey, channelName: 'KitX Status Notifications', channelDescription: 'KitX status', - locked: true, + locked: true, // Prevents the user from deleting the channel + playSound: false, // Do NOT play sound when the notification is displayed + enableVibration: false, // Do NOT vibrate when the notification is displayed + onlyAlertOnce: true, // Only alert once // defaultColor: Color(0xFF9D50DD), // ledColor: Colors.white, ), ], ); + AwesomeNotifications().setListeners(onActionReceivedMethod: onActionReceivedMethod); + } + + /// On action received method + @pragma('vm:entry-point') + static Future onActionReceivedMethod(ReceivedAction receivedAction) async { + var key = receivedAction.buttonKeyPressed; + if (key == 'action_view_button') { + if (instances.devicesService.serviceStatus == ServiceStatus.running) { + // Stop service + instances.shutdownDevicesServer(); + } else { + // Start service + instances.restartDevicesServer(); + } + } else if (key == 'action_view_exit') { + // Exit app + SystemNavigator.pop(); // Probably not working on iOS (by Copilot) + } } /// Update status notification @@ -37,15 +61,26 @@ class NotificationService { required ServiceStatus serviceStatus, }) async { AwesomeNotifications().createNotification( - content: NotificationContent( - id: statusNotificationId, - channelKey: statusChannelKey, - title: 'NotificationService_StatusNotificationTitle'.trParams({"status": serviceStatus.toText()}), - body: 'NotificationService_StatusNotificationBody'.trParams({'device_count': deviceCount.toString()}), - locked: true, - autoDismissible: false, - category: NotificationCategory.Status, - ), - ); + content: NotificationContent( + id: statusNotificationId, + channelKey: statusChannelKey, + title: 'NotificationService_StatusNotificationTitle'.trParams({"status": serviceStatus.toText()}), + body: 'NotificationService_StatusNotificationBody'.trParams({'device_count': deviceCount.toString()}), + locked: true, + autoDismissible: false, + category: NotificationCategory.Status, + ), + actionButtons: [ + NotificationActionButton( + key: 'action_view_button', + label: (serviceStatus == ServiceStatus.running) ? 'Public_Stop'.tr : 'Public_Launch'.tr, + actionType: ActionType.SilentAction, + ), + NotificationActionButton( + key: 'action_view_exit', + label: 'Public_Quit'.tr, + actionType: ActionType.SilentAction, + ), + ]); } } diff --git a/kitx_mobile/lib/utils/translation/en_us.dart b/kitx_mobile/lib/utils/translation/en_us.dart index 61e662a..e4ce8b4 100644 --- a/kitx_mobile/lib/utils/translation/en_us.dart +++ b/kitx_mobile/lib/utils/translation/en_us.dart @@ -18,6 +18,7 @@ const Map en_us = { 'Public_Stopping': 'Stopping', 'Public_Running': 'Running', 'Public_Pending': 'Pending', + 'Public_Quit': 'Quit', 'NotificationService_StatusNotificationTitle': 'KitX @status', 'NotificationService_StatusNotificationBody': '@device_count Devices Online', 'Drawer_Title': 'KitX', diff --git a/kitx_mobile/lib/utils/translation/zh_cn.dart b/kitx_mobile/lib/utils/translation/zh_cn.dart index 5fa0b60..bd68ee9 100644 --- a/kitx_mobile/lib/utils/translation/zh_cn.dart +++ b/kitx_mobile/lib/utils/translation/zh_cn.dart @@ -18,6 +18,7 @@ const Map zh_cn = { 'Public_Stopping': '停止中', 'Public_Running': '运行中', 'Public_Pending': '等待中', + 'Public_Quit': '退出', 'NotificationService_StatusNotificationTitle': 'KitX @status', 'NotificationService_StatusNotificationBody': '当前设备数: @device_count', 'Drawer_Title': 'KitX', diff --git a/kitx_mobile/pubspec.lock b/kitx_mobile/pubspec.lock index 471da4b..57454ef 100644 --- a/kitx_mobile/pubspec.lock +++ b/kitx_mobile/pubspec.lock @@ -33,6 +33,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.11.0" + awesome_notifications: + dependency: "direct main" + description: + name: awesome_notifications + sha256: "2b430c75cc879d6cfd52bb6eb2b5c1591ed425347816408cdcbd3f6916bba14c" + url: "https://pub.dev" + source: hosted + version: "0.7.4+1" badges: dependency: "direct main" description: