Skip to content

Commit

Permalink
💾 Feat: Simple notification service
Browse files Browse the repository at this point in the history
  • Loading branch information
Cranyozen committed Mar 31, 2024
1 parent 0796c26 commit f40c3fd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
6 changes: 6 additions & 0 deletions kitx_mobile/lib/services/devices_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
57 changes: 46 additions & 11 deletions kitx_mobile/lib/services/notification.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<void> 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
Expand All @@ -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,
),
]);
}
}
1 change: 1 addition & 0 deletions kitx_mobile/lib/utils/translation/en_us.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Map<String, String> 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',
Expand Down
1 change: 1 addition & 0 deletions kitx_mobile/lib/utils/translation/zh_cn.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Map<String, String> zh_cn = {
'Public_Stopping': '停止中',
'Public_Running': '运行中',
'Public_Pending': '等待中',
'Public_Quit': '退出',
'NotificationService_StatusNotificationTitle': 'KitX @status',
'NotificationService_StatusNotificationBody': '当前设备数: @device_count',
'Drawer_Title': 'KitX',
Expand Down
8 changes: 8 additions & 0 deletions kitx_mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f40c3fd

Please sign in to comment.